Search code examples
catoi

Question about compile error of atoi in c language


The problem is that the line with "atoi" can not be compiled. If I try to debug my code, this statement comes out "Unhandled exception at 0x7C17F2F6 (ucrtbased.dll) in A5(Dynamic Memory allocation).exe: An invalid parameter was passed to a function that considers invalid parameters fatal."

I think I didn't missed any single thing for the atoi part. I am using visual studio, and would that be the problem? Shall I change the program??

This is the code:

#include <stdio.h>
#include <stdlib.h>

double arith_seq(double*, int, int);

int main(int argc, char* argv[])
{
double* in;
int num;
num = atoi(argv[1]);

in = (double*)malloc((num+1) * sizeof(double));

if (in == NULL)
{
    num = 1;

    arith_seq(&in, num, 0.1);
}
//if there is no input

else
{
    in[0] = 0;

    arith_seq(&in, num, 0.1);
}
//in normal case

printf("My last array element is : %f", in[num - 1]);
//print the last element of the array

free(in);
return 0;
}

double arith_seq(double* parr, int size, int com)
{
for (int i = 1; i < size; i++)
{
    parr[i] += parr[i - 1] + com;
}
return *parr;
}

Solution

  • There are multiple things wrong.

    1. You access argv[1] without checking argc first to know if there even are any arguments stored in argv
    2. double arith_seq(double* parr, int size, int com) expects a double pointer as first argument. You are passing a pointer to a double pointer at multiple places (e.g. arith_seq(&in, num, 0.1) in has type double*, you are passing the address of that)
    3. double arith_seq(double* parr, int size, int com) expects an int as third argument, you are passing a double at multiple places (e.g. arith_seq(in, num, 0.1), 0.1 is not an int). I don't think you want to do that.
    4. malloc expects a size_t argument, but you are passing (num + 1) * sizeof(double) . What if (num + 1) is negative? That will lead to some "interesting" behaviour (Think about what unsigned value -1 represents for example).
    5. You check if malloc returned a NULL pointer (in == NULL), but still go ahead and call arith_seq, which accesses elements of in. You are not allowed to dereference a NULL pointer.
    6. You refer to in[num-1] as the last array element, but actually in[num] is the last elelement. Remember, you allocated an array of num+1 elelements.
    7. In arith_seq you do parr[i] += parr[i-1] + com, which is equal to parr[i] = parr[i] + parr[i-1] + com. But parr[i] has not been initialized anywhere in your code and contains garbage data. This garbage data propagates through the entire array in your loop.

    I recommend to start over with that code. I am not exactly sure what you intend to do with the code, so i can't fully fix it (this fixes 1 and 2, for the rest i don't even know what your original intention was), but this atleast doesnt crash:

    #include <stdio.h>
    #include <stdlib.h>
    
    double arith_seq(double*, int, int);
    
    int main(int argc, char* argv[])
    {
        double* in;
        int num;
        if(argc <= 1){
            return(1);
        }
        num = atoi(argv[1]);
        printf("%d\n", num);
        in = (double*)malloc((num+1) * sizeof(double));
        if (in == NULL)
        {
            return(1);
        }
        in[0] = 0.0f;
        arith_seq(in, num, 0.1);
    
        printf("My last array element is : %f", in[num]);
    
        free(in);
        return(0);
    }
    
    double arith_seq(double* parr, int size, int com)
    {
        for (int i = 1; i < size; i++)
        {
            parr[i] += parr[i-1] + com;
        }
        return *parr;
    }