0

I am having an issue where some global char pointer arrays that I am trying to initialize become full of garbage data after the function that I initialized them in goes out of scope.

char *dept_vals[255];
char *num_vals[255];
char *day_vals[255];
char *bldg_vals[255];
char *instr_vals[255];
int start_vals[255];
int end_vals[255];
int sect_vals[255];
int room_vals[255];

int idx;

static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
    int i = 0;
    while(i < argc)
    {
        dept_vals[idx] = argv[i++];
        num_vals[idx] = argv[i++];
        start_vals[idx] = atoi(argv[i++]);
        end_vals[idx] = atoi(argv[i++]);
        day_vals[idx] = argv[i++];
        sect_vals[idx] = atoi(argv[i++]);
        bldg_vals[idx] = argv[i++];
        room_vals[idx] = atoi(argv[i++]);
        instr_vals[idx] = argv[i++];

        idx++;

    }
    return 0;
}

When I print the values in a different function, the contents are incorrect. However, the values in the integer arrays that I initialized in the same function have the right values. I suspect that the way I am initializing the char pointer arrays is causing unexpected behavior, but I'm not completely sure what is the correct way to initialize them in this situation.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
  • You put this condition `i < argc` but you never check it again inside the loop, though you are incrementing the `i` 9 times, did you check that `argc % 9 == 0`? – Iharob Al Asimi Jan 12 '15 at 18:56
  • are you including `stdlib.h` yesterday I saw a case where that was the problem. – Iharob Al Asimi Jan 12 '15 at 18:58
  • I understand you to mean that the integer values are correct but the `char*` results are not. Show us the way you are printing the `char*` results. – Weather Vane Jan 12 '15 at 19:05
  • 2
    '=' only copies the pointers. If you want the actual strings, you'll have to allocate space for them with malloc(), and strcpy() them into it. – Lee Daniel Crocker Jan 12 '15 at 19:07
  • Yes, I did check and argc % 9 == 0. I actually forgot to explicitly include stdlib.h, but I think it must have been included in another one of my header files because atoi works fine. Explicitly including it did not change anything. – 36mbMonster Jan 12 '15 at 19:08
  • What @LeeDanielCrocker said. Alternatively, you can just keep the pointers, but you must make sure that the memory chunks pointed to are still in scope and valid when you print them. – M Oehm Jan 12 '15 at 19:10
  • @LeeDanielCrocker the arrays are char pointer. – Weather Vane Jan 12 '15 at 19:11

2 Answers2

0

Without knowing how you call the function, it just guessing.

If you use gcc try to replace:

... = argv[i++];

with

... = argv[i] ?strdup(argv[i]) :NULL; ++i;

and compile using with the options -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED.

strdup() duplicates the "string" being passed.

alk
  • 69,737
  • 10
  • 105
  • 255
0

Code like

dept_vals[idx] = argv[i++];

copies the address of argv[i] into dept_vals[idx], but as you point out, that goes out of scope when you return. You want to copy the actual string:

dept_vals[idx] = malloc(strlen(argv[i]) + 1);
strcpy(dept_vals[idx], argv[i]);
++i;
Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55