Re: Linked List & Dynamic Memory Allocation
- From: one-trick-pony <worldofpain.aamir@xxxxxxxxx>
- Date: Wed, 29 Aug 2007 06:13:59 -0700
I think my book has an error. Code is allocating memory with malloc
and I don't see a free call to match it.
void enterdata()
{
struct address *info;
for( ; ; )
{
info = (struct address *)malloc(sizeof(struct address));
// check here to see if memory was allocated
gets(info->name);
// if user enters null break out of for loop
gets(info->location);
// call another function here to create a link list in sorted
order(sorting based on name)
}
}
void printlist()
{
// print all the data user has entered
}
I don't see a free call anywhere. You guys mentioned to put free call
for every call to malloc but when I do that inside the for loop I end
up losing my data and when I try to print the list program crashes. I
guess my question is where to put the free call? And how would I know
how many times user entered data to match the number of malloc calls?
My guess is because it is only one pointer that is using malloc. By
calling free(info) will reclaim memory for ALL the malloc calls made.
But the only thing throwing me off is you guys mentioned free for
every malloc. Please help.
.
- Follow-Ups:
- Re: Linked List & Dynamic Memory Allocation
- From: Joseph M . Newcomer
- Re: Linked List & Dynamic Memory Allocation
- From: Scott McPhillips [MVP]
- Re: Linked List & Dynamic Memory Allocation
- References:
- Linked List & Dynamic Memory Allocation
- From: one-trick-pony
- Re: Linked List & Dynamic Memory Allocation
- From: Jonathan Wood
- Re: Linked List & Dynamic Memory Allocation
- From: one-trick-pony
- Re: Linked List & Dynamic Memory Allocation
- From: Dan Bloomquist
- Re: Linked List & Dynamic Memory Allocation
- From: one-trick-pony
- Re: Linked List & Dynamic Memory Allocation
- From: Dan Bloomquist
- Re: Linked List & Dynamic Memory Allocation
- From: one-trick-pony
- Re: Linked List & Dynamic Memory Allocation
- From: Nobody
- Re: Linked List & Dynamic Memory Allocation
- From: Joseph M . Newcomer
- Re: Linked List & Dynamic Memory Allocation
- From: one-trick-pony
- Re: Linked List & Dynamic Memory Allocation
- From: Joseph M . Newcomer
- Linked List & Dynamic Memory Allocation
- Prev by Date: Re: SystemParametersInfo(SPI_GETWORKAREA,0,&o_Rect,0);
- Next by Date: Re: Install Debug DLL's for remote debugging
- Previous by thread: Re: Linked List & Dynamic Memory Allocation
- Next by thread: Re: Linked List & Dynamic Memory Allocation
- Index(es):
Relevant Pages
|