0

I am expecting names from the command line and I need to hold them in an array. The multiple ways I've tried aren't working (I'm using strcpy) and I'm getting the following valgrind errors:

Use of uninitialised value of size 8
==23027==    at 0x4C2E1E0: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23027==    by 0x400F90: main (pt-cruisers.c:52)
==23027==
==23027== Invalid write of size 1
==23027==    at 0x4C2E1E0: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23027==    by 0x400F90: main (pt-cruisers.c:52)
==23027==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

Here's the code snippet:

        char *racern[argc][20];
        Racer *racers[argc];
        pthread_t threads[argc];
        int rc;
        void *returnv;
        for(int i = 1; i < argc; i++){
                strcpy(racern[i-1], argv[i]); <-line 52
        }

I know that the length of the names will not exceed 20 characters. I used argc to know how many names are going to be on the command line. I'm assuming I'm not giving enough memory however even when I gave a large arbitrary amount and tested with 3 names I still got the same issues. Any help is appreciated!

Cody P
  • 3
  • 2
  • suggest using: #define MAX_NAME_LEN (20) then, char racern[argc][MAX_NAME_LEN] = {{'\0'}}; then strncpy( racern[i=1], argv[i], min(MAX_NAME_LEN-1, strlen(argv[i]) ); – user3629249 Dec 07 '14 at 03:11
  • Helped fix the issue but I want to logic check something. If I do racern[argc][MAX_NAME_LEN], that is saying I'm creating an array with argc spots of size MAX_NAME_LEN? I reduced the errors but I keep getting conditional jumps on a strlen on an argument in another function (the names) which is telling me that it must be getting null. – Cody P Dec 07 '14 at 03:45

1 Answers1

2

I think you want to change: From: char *racern[argc][20]; To: char racern[argc][20];

TonyB
  • 927
  • 6
  • 13