I have problems with dynamic memory allocation.
Below is just the test code I played with to try to fix the problem (this is the same problem in my current project code, this is just an easier way to show this).
#include<stdlib.h> #include<stdio.h> #include<assert.h> int main(){ int x = 5; int *ptr = (int*) malloc(sizeof(int)); assert(ptr != NULL); ptr = &x; printf("x = %d\n",x); *ptr = 3; printf("x = %d\n",x); free(ptr); return 0; }
The program compiles in order and when I start, I get the correct outputs printed "x = 5 x = 3" But then I get the error message:
glibc detected ./dnam: free(): invalid pointer: 0xbfccf698
dnam is the name of the test program. From what I read about the error, it is supposedly caused by freeing up memory that you don't have malloc / calloc / realloc'd.
This error message is followed by backtracking and a memory card. At the end of the memory card, they told me that the program was interrupted (the kernel is reset).
Lukas source share