home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11389 < prev    next >
Encoding:
Text File  |  1992-07-20  |  2.5 KB  |  82 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!sun-barr!cs.utexas.edu!asuvax!ennews!envmsa.eas.asu.edu!ptran
  3. From: ptran@envmsa.eas.asu.edu (Phi-Long Tran)
  4. Subject: Re: structure question
  5. Message-ID: <21JUL199200390095@envmsa.eas.asu.edu>
  6. News-Software: VAX/VMS VNEWS 1.4-b1  
  7. Sender: news@ennews.eas.asu.edu (USENET News System)
  8. Organization: Arizona State University, Tempe, AZ
  9. References: <1992Jul21.053858.26677@vuse.vanderbilt.edu>
  10. Distribution: usa
  11. Date: Tue, 21 Jul 1992 07:39:00 GMT
  12. Lines: 68
  13.  
  14. In article <1992Jul21.053858.26677@vuse.vanderbilt.edu>, 
  15. leegye@vuse.vanderbilt.edu (Gyesung Lee) writes...
  16.  
  17. >typedef struct a_struct {
  18. >       char            *name;
  19. >       int             mark;
  20. >       struct a_struct *next;
  21. >} *A_Type;
  22.  
  23. >A_Type Global_Root;
  24.  
  25. ..
  26.  
  27. >main  ()  /* create a global root and set next ptr to NULL */
  28. >{
  29. >  Global_Root = (A_Type) malloc (sizeof(A_Type));
  30. >  Global_Root->next = NULL;
  31. >  abc();
  32. >}
  33.  
  34. >The right answer should be:
  35. >In abc:  is  NULL
  36. >In abc1: is  NULL
  37.  
  38. >But, the result turns out as follows:
  39.  
  40. >In abc:  is  NULL
  41. >In abc1: not NULL
  42. >Segmentation fault (core dumped)
  43.  
  44. >If I modify 'A_Type' structure as follows by switching `mark' and `next',
  45. >...
  46. >then, it works correctly.
  47.  
  48. >I rearranged the elements of `struct a_struct' and ran it.
  49. >In some case it works, in other cases no luck.
  50. >Could anyone tell me what is wrong with this program ?
  51.  
  52.      I can see one major problem -- you allocated a pointer, not a whole
  53. data structure.  You declared A_Type as a pointer, so sizeof( A_Type ) is
  54. the size of a pointer to your type (however many words that may be for your
  55. machine).  Malloc returns you a pointer to an area in the system heap for
  56. you to store an address, but what you really want is to allocate the whole
  57. structure.  I suggest the following:
  58.  
  59. struct A_Struct_Tag { ... };
  60. typedef struct A_Struct A_Type; /* This is what you really want. */
  61. typedef A_Type *A_Type_Ptr;     /* Declare a type for a ptr. to A_Type. */
  62. ..
  63. A_Type_Ptr global_A_Ptr;
  64. ..
  65.  
  66.      The call to malloc would then be:
  67.  
  68. global_A_Ptr = (A_Type_Ptr) malloc( sizeof( *global_A_Ptr ) );
  69.  
  70.      Originally, when you allocated a pointer with just sizeof( A_Type ),
  71. you were pointing to an address, not the data structure that you were
  72. hoping to get.  Your random errors reflect de-referencing random locations
  73. stored in Global_Root.  Changing field declaration order does not make that
  74. much of a difference (usually).  I suggest you read a good C book and jump
  75. to the pointer chapter -- K&R's "The C Programming Language."
  76.  
  77. ---
  78. Phi-Long Tran
  79. ptran@asuvax.eas.asu.edu
  80. Arizona State University
  81.