473,388 Members | 1,209 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

malloc() and free() and linked list

Hi all,

I know there's been a lot of articles and posts because of this malloc
and free combination and I've read quite a lot of them. Still I can't
get my program to work:

My Structure:
typedef struct rule {
char ip[IP_SIZE];
struct rule *next;
} rule_list;

My Function to add a rule to the list of rules:
void add_rule(rule_list **head, char* ip)
{
rule_list *tmp;

if ((tmp = malloc(sizeof(*tmp))) == NULL)
{
syslog(LOG_ERR, "malloc for inserting a new rule crashed! Out of
memory?");
exit(EXIT_FAILURE);
}

/* This is what doesn't work because the
compiler says incompatible types in assignment */
//tmp->ip = malloc(strlen(ip));

/* This would work (without the statement above, but without
allocating space for this member of the struct I can't free it later
on */
strcpy(tmp->ip, ip);

tmp->next = *head;
*head = tmp;
}
My Function to free the whole list including all members:
void freelist(rule_list *head)
{
rule_list *tmp;

printf("freeing the list...");
fflush(stdout);

while (head != NULL) {
printf("head is not null freeing its members...");
fflush(stdout);

/* This will crash on me any time I run it, since I haven't
allocated memory for it with malloc */
if (head->ip != NULL) free(head->ip);

tmp = head->next;
free(head);
head = tmp;
}
printf("list freed!...");
fflush(stdout);

}

I only made it so far:
1. I don't free() the member ip of the struct when freeing the whole
list
--> This will eat up more and more memory while the program is running

2. I try to free() the memory and it won't work --> crash!

What's still wrong here?
Thx a lot,
Kurt
Nov 14 '05 #1
1 4231
On Thu, 16 Dec 2004 02:56:03 -0800, Kurt wrote:
Hi all,

I know there's been a lot of articles and posts because of this malloc
and free combination and I've read quite a lot of them. Still I can't
get my program to work:

My Structure:
typedef struct rule {
char ip[IP_SIZE];
struct rule *next;
} rule_list;

My Function to add a rule to the list of rules:
void add_rule(rule_list **head, char* ip)
{
rule_list *tmp;

if ((tmp = malloc(sizeof(*tmp))) == NULL)
Excellent, I seems you have been reading up on this. :-)
You don't need the inner parentheses there i.e. sizeof *tmp is fine, but
you can put them in if you like
{
syslog(LOG_ERR, "malloc for inserting a new rule crashed! Out of
memory?");
Saying "crashed" isn't quite right. Crashed suggests something like a seg
fault whereas what happened here is that malloc() simply failed to perform
the requested operation in a well defined way saying that it can't or
won't do that.
exit(EXIT_FAILURE);
}
}
/* This is what doesn't work because the
compiler says incompatible types in assignment */
//tmp->ip = malloc(strlen(ip));


You can't assign to an array in C. The array you want is being created by
malloc(), make the ip structure member a pointer so it can point to that
array.

There is another problem here in that strlen(ip) doesn't account for the
terminating null character that every string requires. Make that
strlen(ip)+1. Don't forget to test the return value of this malloc() for
null.

Lawrence
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have...
10
by: pembed2003 | last post by:
Hi, If I have the folllowing: char* p = malloc(5); memset(p,-1,5); *p = 0; printf("%d\n",strlen(p)); free(p); It will print 0. Is there a way to retrive the initial size of memory
13
by: John | last post by:
In the course of an assignment, I learned the hard way that I shouldn't try to free a malloc'd member of a malloc'd structure after having freed that structure (i.e., free( structure ); free(...
42
by: Joris Adriaenssens | last post by:
This is my first posting, please excuse me if it is off-topic. I'm learning to program in C. It's been almost ten years I've been programming and a lot of things have changed apparently. I...
3
by: Kurt | last post by:
Hi all, I know there's been a lot of articles and posts because of this malloc and free combination and I've read quite a lot of them. Still I can't get my program to work: My Structure:...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
49
by: Karsten Jung | last post by:
Hello together, We have to make our own implementation of malloc() under Linux. Does anybody can give me a hint where to start? Our implementation should only work under Linux. Thanks ...
6
by: Jack | last post by:
In the following code: char *p; while(1){ p = malloc(10*sizeof(*p)); //Do something with p
48
by: avasilev | last post by:
Hi all, my question is: if i allocate some memory with malloc() and later free it (using free()), is there a possibility that a consequent malloc() will allocate memort at the same starting...
9
by: dreiko466 | last post by:
(sorry about my english) Hello, i am a newbie in C and i am trying to understand how to use free(...) and malloc(...) functions. In the following programm i successfully create a point of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.