malloc/free question
My question is regarding C, specifically the gcc compiler.
Essentially, I did the following:
Code:
void *ptr = malloc( (sizeof(int)*2) + (sizeof(char)*STRLEN) );
void *p = ptr;
*(int*)p = 1;
p += sizeof(int);
*(int*)p = 2;
p += sizeof(int);
strcpy((char*)p, "hello");
// did some stuff
free( ptr );
Did I free the memory properly?
I know the first integer is freed, but I accidentily printed the values of all these locations and the second integer's and string's values were still intact.
Last edited by c.reilly; February 18th, 2008 at 06:28 AM.
Reason: Added frowny face
Visual C++ 2008 Express Edition
Windows Vista