c - Weird seg faults on consecutive calls to the same array -
I've really worked hard to find a solution on this but I can not think enough keywords.
Currently I'm having trouble with the concept behind makinggings and it is used with triple pointers (I do not know whether *** Afu means it is easy as a concept Is not ** foo or * foo). So I created my own:
const char ** makeargv (char * string, int * numargs) {string = string + strspn (string, delims); Four * copy = maulok (string (string) + 1); Int i; Strcpy (copy, string); Int notebooks; If (strtok (copy, delims)! = NULL) {for (numtokens = 1; strtok (NULL, delims)! = NULL; numtokens ++} {}} strcpy (copy, string); Const char * Results [numtokens + 1]; Results [0] = Stroke (copy, Dalmis); For (i = 1; i
This is where it breaks:
void parse_file (char * filename) {char * line = malloc (160 * sizeof () Char)); File * fp = file_open (filename); Int i = 0; Int digit = 0; Int * Notifier = & amp; Numargs; While ((line = file_gateline (line, fp)) = null) {if (strangle (line) == 1) {continue; } Const char ** args = makeargv (line, indicator); Printf ("% s \ n", args [0]); Printf ("% s \ n", args [1]); / * It prints the RGS [0], but then the reasoning [1] causes the SEG fault even if I change ARG [1] with any other argument [0], then it is still Also causes a mistake / fclose (fpose); free line); }
I have an array of strings working. However, when I try to print the wire in the array, I can only print 1 of my choice and then make this mistake for any subsequent call. IRG [3] = {"yes", "no", "may be"}, if I say argv [0], then it will let me say "yes", but any other call (even if i then argv [0]) does not work and is due to a segfault. I can call any element from the array, but once I call one, the work is finished to work.
Please help? D: It's in C.
const char * result [numtokens + 1];
This array "result" is a local variable, it is only available within "macaragua".
You want to use a better malloc:
result = malloc (numtokens + 1)
and I believe That your code has a memory leak, you will not be able to free up memory for "four * copy"
four = copy = maulok (string) + 1);
Comments
Post a Comment