Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > malloc() and free() and linked list

Reply
Thread Tools

malloc() and free() and linked list

 
 
Kurt
Guest
Posts: n/a
 
      12-16-2004
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
 
Reply With Quote
 
 
 
 
Bas Wassink
Guest
Posts: n/a
 
      12-16-2004
On Thu, 16 Dec 2004 02:56:21 -0800, Kurt wrote:

> typedef struct rule {
> char ip[IP_SIZE];
> struct rule *next;
> } rule_list;


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


Hi,

You're assigning a pointer (returned by malloc) to an array of char, that
won't work. You should change the type of ip in your struct to a pointer
to char.


 
Reply With Quote
 
 
 
 
Alex Fraser
Guest
Posts: n/a
 
      12-16-2004
"Kurt" <> wrote in message
news: om...
> 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);


You have already allocated space for it - it's part of the struct. It's an
array, not a pointer. The reason the commented out line gives an error is
that you are trying to assign to an array, which you cannot do in C.

If you declared ip as type pointer to char, then you would need to allocate
space seperately (and free it later), but when the maximum size is known and
small (as in this case), it's simpler to do as you did, and declare it as an
array.

> 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);


Right, you didn't allocate it, so you don't need to free it.

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


Apart from the notes above, all looks good to me (although I wouldn't bother
with the typedef, but that's just a style issue).

Alex


 
Reply With Quote
 
Al Bowers
Guest
Posts: n/a
 
      12-16-2004


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;


In the typedef, you have allocated space for the member ip with:
char ip[IP_SIZE];
The question is what is IP_SIZE?

> 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));


Correct. tmp->ip is an array IP_SIZE characters. You do not
need to dynamically allocate space.

> /* 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);
Mignt want to use function strncpy.
strncpy(tmp->ip,ip,IP_SIZE);
tmp->ip[IP_SIZE-1] = '\0';


> 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);


Yes, Since you have not dynamically allocated the ip member.
You do not need to free it.

> tmp = head->next;
> free(head);
> head = tmp;
> }
> printf("list freed!...");
> fflush(stdout);
>
> }
>
> What's still wrong here?


Here is the corrected code ans usage:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define IP_SIZE 32

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


int add_rule(rule_list **head, const char* ip)
{
rule_list *tmp;

tmp = malloc(sizeof *tmp);
if(tmp)
{
strncpy(tmp->ip, ip, IP_SIZE);
tmp->ip[IP_SIZE-1] = '\0';
tmp->next = *head;
*head = tmp;
}
return tmp?1:0;
}

void freelist(rule_list **head)
{
rule_list *tmp;

while (*head != NULL)
{
tmp = (*head)->next;
free(*head);
*head = tmp;
}
return;
}

int main(void)
{
rule_list *head = NULL, *tmp;

add_rule(&head,"192.168.001.001");
add_rule(&head,"192.168.001.007");

puts("The ips in the list are:");
for(tmp = head; tmp; tmp = tmp->next)
puts(tmp->ip);
freelist(&head);
return 0;
}

--
Al Bowers
Tampa, Fl USA
mailto: (remove the x to send email)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Airplane Program with Linked Lists. The linked list portion is veryconfusing to me. jawdoc C++ 9 03-10-2008 03:38 AM
Linked list within a linked list joshd C++ 12 10-02-2006 08:57 AM
Linked list, New try (was:Linked list, no out put,help) fool C Programming 14 07-03-2006 12:29 AM
Generating a char* from a linked list of linked lists Chris Ritchey C++ 7 07-10-2003 10:12 PM
Generating a char* from a linked list of linked lists Chris Ritchey C Programming 7 07-10-2003 10:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57