problems with free()
leo mueller <[email protected]>
| Newsgroups | org.kernel.vger.linux-c-programming |
|---|---|
| Message-ID | <[email protected]> |
hi all, i got a little problem with free() ... i am allocating mem for a special struct and putting it afterwards into a linked list. allocating works fine, but freeing not. i'm tracking the program with htop. after allocating, the percentage of mem usage stays constant ... the test-file is attached. any help is appreciated. big thanks :)
test.c
(text/x-csrc, 3.5 KB)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
typedef unsigned long long nodeinf;
typedef unsigned long long hashinf;
struct node {
hashinf hash;
nodeinf info;
struct node *parent;
struct levelcache *childs;
};
struct levelcache {
unsigned int size;
unsigned int last;
struct node **list;
};
struct cache {
struct levelcache *root;
struct node *last_node;
};
struct stackframe {
struct levelcache *lc;
struct stackframe *next;
};
void release();
inline void push_to_stack(struct levelcache *);
void free_stack();
inline void add_to_list(struct node *);
struct stackframe *sfalloc();
struct node *nalloc();
struct levelcache *lcalloc(int);
void lcrealloc(struct levelcache *, int);
struct stackframe *garbage;
/*
* Pushes element to garbage stack
*/
inline void push_to_stack(struct levelcache *l)
{
struct stackframe *sf;
sf = sfalloc();
sf->lc = l;
sf->next = garbage;
garbage = sf;
}
/*
* Frees garbage stack
*/
void free_stack()
{
int i;
int j = 0;
void *rc;
struct stackframe *sf;
while(garbage != NULL){
sf = garbage->next;
garbage->next = NULL;
for(i = 0; i < garbage->lc->size; ++i){
if(garbage->lc->list[i] != NULL){
j++;
printf("free adr: 0x%x\n", (unsigned int) garbage->lc->list[i]);
rc = realloc(garbage->lc->list[i], 0);
printf("free adr: 0x%x\n", (unsigned int) rc);
}
}
free(garbage->lc->list);
free(garbage->lc);
free(garbage);
garbage = sf;
}
printf("%d elem freed.\n", j);
}
/*
* Allocation of a stackframe
*/
struct stackframe *sfalloc()
{
struct stackframe *sf = (struct stackframe *) malloc(sizeof(struct stackframe));
if(!sf){
fprintf(stderr, "error: malloc stackframe\n");
release();
exit(1);
}
memset(sf, 0, sizeof(struct stackframe));
return sf;
}
/*
* Allocation of a single node
*/
struct node *nalloc()
{
struct node *n = (struct node *) malloc(sizeof(struct node));
if(!n){
fprintf(stderr, "error: malloc node\n");
release();
exit(1);
}
memset(n, 0, sizeof(struct node));
return n;
}
/*
* Allocation of a levelcache element
*/
struct levelcache *lcalloc(int n)
{
int i;
struct levelcache *l;
if(!n)
return NULL;
l = (struct levelcache *) malloc(sizeof(struct levelcache));
if(!l){
fprintf(stderr, "error: malloc levelcache\n");
release();
exit(1);
}
l->list = (struct node **) malloc(sizeof(struct node *) * n);
if(!l->list){
fprintf(stderr, "error: malloc node list\n");
release();
exit(1);
}
l->size = n;
l->last = 0;
for(i = 0; i < l->size; ++i)
l->list[i] = NULL;
return l;
}
/*
* Drops our whole cache
*/
void release()
{
free_stack();
}
/*
* Main routine
*/
int main(int argc, char **argv)
{
int i, j;
struct levelcache *l;
garbage = NULL;
for(i = 0; i < 200; ++i){
l = lcalloc(2000);
for(j = 0; j < l->size; ++j){
l->list[j] = nalloc();
l->list[j]->hash = j;
l->list[j]->info = j;
printf("alloc adr: 0x%x\n", (unsigned int) l->list[j]);
}
push_to_stack(l);
}
release();
sleep(5);
return 0;
}