pointers - c - realloc() on tokenized array: signal SIGABRT error -
on line 56, i'm trying resize array:
tokenarray = (char**) realloc(tokenarray, toksize * (sizeof(char)));
i error:
(11972,0x7fff7ca4f300) malloc: * error object 0x100105598: incorrect checksum freed object - object modified after being freed. * set breakpoint in malloc_error_break debug
this programming assignment class, have been instructed dynamically allocated arrays, , expand needed. have searched extensively thread on same isn't advanced me understand, no luck yet... can help. thanks! here code:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define max_row_size 81 void strinput(char str[], int numelem); int main(int argc, const char * argv[]) { printf("enter string of number of integers separated spaces or tabs.\n"); printf("maximum string length 80 characters.\n"); printf("enter empty string conclude input.\n"); int arrsize = 10, toksize = 10, = 0, j = 0; char** inputarray = malloc(arrsize * (sizeof(char))); char** tokenarray = malloc(toksize * (sizeof(char))); { inputarray[i] = malloc(max_row_size * sizeof(int)); strinput(inputarray[i], arrsize); if ((inputarray[i][0] != '\0') && (i == (arrsize - 1))) { arrsize = arrsize * 2; inputarray = (char**) realloc(inputarray, arrsize * (sizeof(char))); } while (inputarray[i][j] != '\0') { printf("%c", inputarray[i][j]); j++; } j = 0; i++; } while (inputarray[i-1][0] != '\0'); = 0; while (inputarray[i][0] != '\0') { if ((tokenarray[j] = strtok(inputarray[i], " \t"))) j++; while ((tokenarray[j] = strtok(null, " \t"))) { if (j == (toksize - 1)) { toksize = 2 * toksize; //this line error tokenarray = (char**) realloc(tokenarray, toksize * (sizeof(char))); } j++; } i++; } printf("printing tokenized arrays: "); (i = 0; < j; i++) printf("%s ", tokenarray[i]); free(inputarray); free(tokenarray); return 0; } void strinput(char str[], int numelem) { int j, k = 0; j = k; while ((str[k] = getchar()) != '\n') { k++; } if (str[k] == '\n') str[k] = '\0'; }
1. not cast result of malloc
, friends.
it's useless @ best , dangerous @ worst.
2. mind size malloc
'ing.
char** inputarray = malloc(arrsize * (sizeof(char)));
this makes no sense, , accidental. rule of thumb, type malloc
'ing , pointer you're pointing @ resulting storage should differ 1 indirection. i.e :
char** inputarray = malloc(arrsize * sizeof(char*)); // ^^ double-pointer vs single pointer ^
better rule of thumb yet, let compiler figure out. sizeof
can deduce type should measure expression.
char **inputarray = malloc(arrsize * sizeof(*inputarray));
this works because operand of sizeof
unevaluated context. pointer won't dereferenced, type deduced.
side note : sizeof
's parentheses not needed around expression, i've left them clarity. remove them once you're comfortable it.
3. check allocation successful.
malloc
, friends return null
in case of trouble. should check that.
4. fix realloc
inputarray = realloc(inputarray, /*...*/);
this wrong. mentioned above, if realloc
fails, return null
and nothing else. means inputarray
still points @ previous storage. is, until trump pointer null
realloc
returned, , leak said storage. oops.
always store, check, , then assign result of realloc
.
char **inputarray_ = realloc(inputarray, /*...*/); if(!inputarray_) { /* allocation failure, handle , break out */ } inputarray = inputarray_;
Comments
Post a Comment