Rany Albeg
July 1st, 2011, 10:02 PM
It has been a long time since I wrote something in C, and I'm trying to build a vector.
I wrote its constructor and its 'add' method.
Now I want to test it and free the allocated memory after using this vector, but I keep getting a segfault.
I guess my 'free' method is wrong so I need some help with this game of pointers.
Thanks in advance.
/**** The vector type ****/
typedef struct _rds_vector_t
{
void** objs; // the vector
size_t size; // the size of the vector
size_t numalloc; // number of allocated objects.
} rds_vector_t;
/**** The constructor ****/
rds_vector_t* rds_vector_new()
{
rds_vector_t* new_vec;
if((new_vec = (rds_vector_t*)malloc(sizeof(rds_vector_t))) == NULL)
rds_vector_failure(__LINE__, "malloc failed.");
new_vec->objs = NULL;
new_vec->size = new_vec->numalloc = 0;
return new_vec;
}
/**** Add method ****/
void rds_vector_add(rds_vector_t* vec, void* obj)
{
if(!vec->size)
{
if((vec->objs = (void**)malloc(sizeof(void*))) == NULL)
rds_vector_failure(__LINE__, "malloc failed.");
vec->objs[0] = obj;
vec->size = vec->numalloc = 1;
return;
}
if (vec->size == vec->numalloc)
rds_vector_amortization(vec);
vec->objs[vec->numalloc++] = obj;
return;
}
/**** free method ****/
void rds_vector_free(rds_vector_t* vec)
{
size_t na = vec->numalloc, i;
for(i=0; i<na; ++i)
free(vec->objs[i]);
free(vec->objs);
}
/**** main.c ****/
#include "rds_vector.h"
int main(int argc, char** argv)
{
rds_vector_t* vec = rds_vector_new();
char* arr0= "hello0";
rds_vector_add(vec, arr0);
rds_vector_free(vec);
return EXIT_SUCCESS;
}
Have a good day!
I wrote its constructor and its 'add' method.
Now I want to test it and free the allocated memory after using this vector, but I keep getting a segfault.
I guess my 'free' method is wrong so I need some help with this game of pointers.
Thanks in advance.
/**** The vector type ****/
typedef struct _rds_vector_t
{
void** objs; // the vector
size_t size; // the size of the vector
size_t numalloc; // number of allocated objects.
} rds_vector_t;
/**** The constructor ****/
rds_vector_t* rds_vector_new()
{
rds_vector_t* new_vec;
if((new_vec = (rds_vector_t*)malloc(sizeof(rds_vector_t))) == NULL)
rds_vector_failure(__LINE__, "malloc failed.");
new_vec->objs = NULL;
new_vec->size = new_vec->numalloc = 0;
return new_vec;
}
/**** Add method ****/
void rds_vector_add(rds_vector_t* vec, void* obj)
{
if(!vec->size)
{
if((vec->objs = (void**)malloc(sizeof(void*))) == NULL)
rds_vector_failure(__LINE__, "malloc failed.");
vec->objs[0] = obj;
vec->size = vec->numalloc = 1;
return;
}
if (vec->size == vec->numalloc)
rds_vector_amortization(vec);
vec->objs[vec->numalloc++] = obj;
return;
}
/**** free method ****/
void rds_vector_free(rds_vector_t* vec)
{
size_t na = vec->numalloc, i;
for(i=0; i<na; ++i)
free(vec->objs[i]);
free(vec->objs);
}
/**** main.c ****/
#include "rds_vector.h"
int main(int argc, char** argv)
{
rds_vector_t* vec = rds_vector_new();
char* arr0= "hello0";
rds_vector_add(vec, arr0);
rds_vector_free(vec);
return EXIT_SUCCESS;
}
Have a good day!