RES: [Dev-C++] Single Linked List-IV
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <and...@su...> - 2003-06-06 13:00:13
|
Crucial help!!!
Terrific!
Thank you
André
-----Mensagem original-----
De: Per Westermark [mailto:pw...@ia...]
Enviada em: sexta-feira, 6 de junho de 2003 09:44
Para: André Macário Barros
Cc: Dev-Cpp-Users
Assunto: Re: [Dev-C++] Single Linked List-IV
On Fri, 6 Jun 2003, André Macário 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=NULL, *start=NULL, *last=NULL;
> int i;
>
> for(i=0; 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 = start;
> start = start->next;
> free(info);
> }
> system("PAUSE");
> return(0);
> }
>
> void slstore(struct address *i, struct address **start_arg,
> struct address **last_arg)
> {
}
> if(*last_arg==NULL) *start_arg=i;
> else (*last_arg)->next=i;
> i->next=NULL;
> *last_arg=i;
> }
>
> 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 = *start_arg;
> while(info)
> {
> display(info);
> info = info->next;
> }
> }
>
> void init(struct address **arg)
> {
> *arg= malloc(1*sizeof(struct address));
> if(!(*arg))
> {
> printf("sem memória\n");
> system("PAUSE");
> exit(EXIT_FAILURE);
> }
> }
>
> ==>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 = start;
> start = 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=NULL;
>
> while (*arg)
> {
> info = *arg;
> *arg = (*arg)->next;
> free(info);
> }
> }
PWM: Yes, but you might consider adding a line
PWM: *arg = 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
>
|