Results 1 to 4 of 4
HI,
I allocated void pointer using malloc. Please let me know how to free this memory?
typedef struct_t node {
int a;
char b;
void *c;
void *d;
}node_t;
node_t ...
- 02-19-2010 #1Just Joined!
- Join Date
- Feb 2010
- Posts
- 2
How to free the void pointer
HI,
I allocated void pointer using malloc. Please let me know how to free this memory?
typedef struct_t node {
int a;
char b;
void *c;
void *d;
}node_t;
node_t *dp;
in my program, I assigned some head to the pointer. I am allocating like this.
dp = phead.
dp->c = (void*)malloc(10);
dp->d = (void*)malloc(20);
Now I have to free the memory. I used at last in the program like this.
if (dp->c) free(dp->c)
if (dp->d) free(dp->d)
But when I executing it is getting crashed. Let me know the solution for this.
- 02-19-2010 #2
You should be able to free the allocated memory. Maybe its something else, did you try running your program in a debugger?
Make mine Arch Linux
- 02-19-2010 #3Just Joined!
- Join Date
- Apr 2008
- Posts
- 4
dp = phead;
dp->c = (void*)malloc(10);
dp->d = (void*)malloc(20);
Does this 'phead' pointer variable really have allocated memory?
Check that! If it is incorrect or missing, then it is likely that you get this kind of behaviour.
- 02-19-2010 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Most likely something in your program has "walked on" the pointers, resulting in corrupted memory, hence the crash. You need to run your program in the debugger and set a watch point on dp or phead to see when it is modified. Also, if something deallocated phead, or phead is a pointer to an automatic variable that went out of scope, then you will get the same results.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote