Re: [Dev-C++] Single Linked List Problem-II
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-06-06 07:57:46
|
Before going for how to use free(), let's have a look at how you use
malloc()...
See below:
/Per W
On Thu, 5 Jun 2003, Andr=E9 Mac=E1rio Barros wrote:
>
> I forgot the "int i;" variable declaration, ok?
> And this is a "C" program (aswering for the orientation "allways say if y=
ou
> are working
> with a C or C++ program").
>
> -----Mensagem original-----
> De: Andr=E9 Mac=E1rio Barros [mailto:and...@su...]
> Enviada em: quinta-feira, 5 de junho de 2003 22:58
> Para: Dev-Cpp-Users
> Assunto: Single Linked List Problem
>
>
> Dear Users,
>
> I need to discover a way to use the "free" command to correctly avoid 5
> records dinamically allocated in a simple Single Linked List program.
> Here are some pieces of it:
>
> ...
>
> #include <stdio.h>
> #include <stdlib.h>
>
> #define MAX 5
>
> struct address
> {
> char name[40];
> char phone[20];
> struct address *next;
> };
>
> void slstore(struct address *, struct address **, struct address **);
>
> ...
> int main()
> {
>
> struct address *info, *start, *last;
>
PWM: Here you allocate a buffer to start
> init(&start);
PWM: And here you trow away your reference to it by setting start to NULL
> start=3DNULL;
PWM: Here you allocate a buffer to last
> init(&last);
PWM: And here the second buffer is thrown away
> last=3DNULL;
> for(i=3D0; i<MAX; i++)
> {
> init(&info);
> printf("Enter name: ");
> gets(info->name);
> printf("Enter phone: ");
> gets(info->phone);
> slstore(info, &start, &last);
> }
> ...
> system("PAUSE");
> return(0);
> }
>
> void slstore(struct address *i, struct address **start_arg,
> struct address **last_arg)
> {
> if(*last_arg=3D=3DNULL) *start_arg=3Di;
> else (*last_arg)->next=3Di;
> i->next=3DNULL;
> *last_arg=3Di;
> }
>
> void init(struct address **arg)
> {
> *arg=3D malloc(1*sizeof(struct address));
> if(!(*arg))
> {
> printf("sem mem=F3ria\n");
> system("PAUSE");
> exit(EXIT_FAILURE);
> }
PWM: Should initialize the allocated address too:
*arg->next =3D NULL;
*arg->name[0] =3D '\0';
*arg->phone[0] =3D '\0';
> }
>
PWM: Try something like:
// No need to allocate anything until you read in entries.
start =3D last =3D NULL;
for (...) {
init(&info);
...
if (!start) {
start =3D last =3D info;
} else {
last->next =3D info;
last =3D info;
}
}
PWM: To then release everything do:
while (start) {
info =3D start;
start =3D start->next;
free(info);
}
> ***
> The slstore function stores the new element in the last position. It=B4s
> "next" record is filled
> with the NULL address to mean that there aren=B4t more elements in the li=
st.
> At the end of the program 5 memory spaces of 64 bytes each one, are
> allocated for each element.
> How can I use the free command to return the memory for the computer?
>
> Thanx
> Andre
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Etnus, makers of TotalView, The best
> thread debugger on the planet. Designed with thread debugging features
> you've never dreamed of, try TotalView 6 free at www.etnus.com.
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|