0
votes

I am working on this project and when I run valgrind on this line of code

  int numPointers;
  numPointers = atoi(argv[NUM_POINTERS_VALUE]);

I get a valgrind error of

Invalid read of size 1 [PID: 8979] Address 0x0 is not stack'd, malloc'd or (recently) freed

I was wondering what is going on here and if there is a way to fix it

1
NUM_POINTERS_VALUE should always be < argc - Axalo
What does the macro NUM_POINTERS_VALUE contain and what command do you execute to run valgrind ? - Santosh A
@Axalo it is. In this case, my argc is 2 while NUM_POINTERS_VALUE = 1 - Ryan Newman
@SantoshA I am right clicking on my project and profiling as valgrind (the way my professors taught us) - Ryan Newman
What is the value you are passing through command line arguement? - Gopi

1 Answers

2
votes

When you are using command line arguments it is always a good practice to use

   int main()
   { 
     if(argc != <required number of argument>)
     {
       printf("Fewer arguments in the input\n");
       return 1;
     }
     // Do your stuff
   }

Later

if(argc[1] != NULL)
numPointers = atoi(argv[1]);

Because atoi(NULL) results in undefined behavior leading to crash.