From: leo mueller <llug.dan@googlemail.com>
To: linux-c-programming@vger.kernel.org
Subject: problems with free()
Date: Tue, 28 Apr 2009 11:20:29 +0200 [thread overview]
Message-ID: <c93f91ee0904280220x663ff91fibeca0886af04458f@mail.gmail.com> (raw)
In-Reply-To: <c93f91ee0904280218o2f844cd0u2262db407ca8f468@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 344 bytes --]
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 :)
[-- Attachment #2: test.c --]
[-- Type: text/x-csrc, Size: 3551 bytes --]
#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;
}
next parent reply other threads:[~2009-04-28 9:20 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <c93f91ee0904280218o2f844cd0u2262db407ca8f468@mail.gmail.com>
2009-04-28 9:20 ` leo mueller [this message]
[not found] ` <6eee1c40904280341n4e332026i7d9800fd4f8059b4@mail.gmail.com>
2009-04-28 11:10 ` problems with free() leo mueller
2009-04-28 14:48 ` Trevor Woollacott [ MTN - Innovation Centre ]
2009-04-28 11:22 ` Bert Wesarg
2009-04-28 12:52 ` leo mueller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c93f91ee0904280220x663ff91fibeca0886af04458f@mail.gmail.com \
--to=llug.dan@googlemail.com \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).