int size_of_daten = 5;
char *data[size_of_daten];
char *normal_Pointer = (char*)malloc(sizeof(char) * 100);
int i;
for(i=0; i<size_of_daten; i++) {
data[i] = (char*)malloc(sizeof(char) * 100);
}
data[0] = "0";
data[1] = "1";
data[2] = "2";
data[3] = "3";
data[4] = "4";
printf("data[2] %s\n",data[2]);
strcpy(normal_Pointer,data[2]);
for(i=0; i<size_of_daten; i++) {
free(data[i]);
}
free(data);
I just tried this… even I freed the array as I malloced it… also I copied the value of data[2] and didn’t point at it… so that shouldn’t be the problem…
You’re trying to copy the strings
"0","1", etc. into yourdataarray: you can’t just use=to copy strings, you’ll need to use a string library method likestrcpy.Once you assign to your array elements those literal strings, e.g.:
data[0]= "0";
, the array elements no longer point to the memory that you’ve allocated, they point to memory that doesn’t belong to you, and you can’t usefree. You’ve lost the references to your memory blocks frommalloc, causing a memory leak.Furthermore, you can’t do
free(data);because it wasn’t allocated usingmalloc: it’s an array allocated on the stack.