N
Nick Austin
I hope you don't mind me butting in, but I have an issue that seems
somewhat related... I'm implementing a variable-length array/stack,
and free() is giving me some interesting issues. It looks something
like this...
What is in "vararray.h"
/* Just including this for clarity */
void va_init( va_vararray v )
How is va_vararray declared? I assume that it is a typedef.
{
v->size = INITIAL_SIZE;
Ah. For this to be valid I guess that va_vararray is typedefed
as a pointer to a structure.
v->index = 0;
v->contents = malloc( INITIAL_SIZE * sizeof( va_ptr ) );
}
int main()
{
va_vararray v;
In which case, based on my assumptions, this just defines a pointer
to a structure. Memory for the structure itself is never allocated.
Therefore, assuming that va_vararray is a pointer, you need to add:
v = malloc( sizeof *v );
va_init( v );
printf( "Malloc'd: %x\n", (int) v->contents );
printf( "Doing stuff...\n" );
/* Pushes and pops a few elements as a test --no array-expansion
happening,
yet, no further malloc()'s called. */
printf( "Freeing...\n" );
free( v->contents );
printf( "Freed\n" );
return 0;
}
Nick.