|
From: Per W. <pw...@ia...> - 2003-06-06 12:45:05
|
On Fri, 6 Jun 2003, Andr=E9 Mac=E1rio Barros wrote:
> Dear users,
> Please consider this (modificated) Single Linked List Code:
>
>
> #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 **);
> void display(struct address *);
> void list(struct address **);
> void init(struct address **);
>
> int main()
> {
> struct address *info=3DNULL, *start=3DNULL, *last=3DNULL;
> int i;
>
> for(i=3D0; i<MAX; i++)
> {
> init(&info);
> printf("Enter name: ");
> gets(info->name);
> printf("Entre com phone: ");
> gets(info->phone);
> slstore(info, &start, &last);
> }
> printf("List Recorded:\n");
> list(&start);
> while (start)
> {
> info =3D start;
> start =3D start->next;
> free(info);
> }
> 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 display(struct address *info)
> {
> printf("Actual address-->%p\n", info);
> printf("%s\n", info->name);
> printf("%s\n", info->phone);
> printf("Next address-->%p\n", info->next);
> printf("\n");
> system("PAUSE");
> }
>
> void list(struct address **start_arg)
> {
> struct address *info;
>
PWM: Don't allocate any info - the list already contains the entries, and
PWM: you only want to use info as a pointer poiting to the entries
PWM: available in the list.
PWM: Remove this line.> init(&info);
> info =3D *start_arg;
> while(info)
> {
> display(info);
> info =3D info->next;
> }
> }
>
> void init(struct address **arg)
> {
> *arg=3D malloc(1*sizeof(struct address));
> if(!(*arg))
> {
> printf("sem mem=F3ria\n");
> system("PAUSE");
> exit(EXIT_FAILURE);
> }
> }
>
> =3D=3D>My questions:
> A) One example of addresses that are printed on the screen are:
> 002224C8
> 00222510
> 00222558
> 002225A0
> 002225E8
> The distance between theese addresses is 72 bytes, not 64 as expected
> because the size of the struct address.
> WHY?
PWM: Because the memory library have to allocate some extra space for
book-keeping. Normally malloc() will store the size of the memory block (4
byte) and a pointer to the previously allocated block (4 byte).
>
> B) Could the final loop:
> while (start)
> {
> info =3D start;
> start =3D start->next;
> free(info);
> }
> be changed by a function call for a function like
>
> free_ptr(&start);
>
> void free_ptr(struct address **arg)
> {
> struct address *info=3DNULL;
>
> while (*arg)
> {
> info =3D *arg;
> *arg =3D (*arg)->next;
> free(info);
> }
> }
PWM: Yes, but you might consider adding a line
PWM: *arg =3D NULL;
PWM: to make sure that the caller knows that start doesn't point to any
PWM: elements anymore.
>
> Thanks
> 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
>
|