Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #22037 > unrolled thread

linked list

Started byZhang Yuan <zhabgyuan1993@gmail.com>
First post2012-06-08 21:02 -0700
Last post2012-06-14 12:47 -0700
Articles 20 on this page of 33 — 11 participants

Back to article view | Back to comp.lang.c


Contents

  linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-08 21:02 -0700
    Re: linked list Kaz Kylheku <kaz@kylheku.com> - 2012-06-09 04:59 +0000
      Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-08 22:13 -0700
      Re: linked list Ian Collins <ian-news@hotmail.com> - 2012-06-09 17:47 +1200
        Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-08 22:59 -0700
          Re: linked list Ian Collins <ian-news@hotmail.com> - 2012-06-09 18:35 +1200
            Re: linked list Ike Naar <ike@sverige.freeshell.org> - 2012-06-09 06:53 +0000
              Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-09 00:53 -0700
      Re: linked list 88888 Dihedral <dihedral88888@googlemail.com> - 2012-06-22 12:18 -0700
    Re: linked list Ike Naar <ike@sverige.freeshell.org> - 2012-06-09 05:34 +0000
      Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-08 22:51 -0700
        Re: linked list Ike Naar <ike@sverige.freeshell.org> - 2012-06-09 06:45 +0000
          Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-08 23:56 -0700
      Re: linked list Keith Thompson <kst-u@mib.org> - 2012-06-09 11:17 -0700
        Re: linked list Ike Naar <ike@iceland.freeshell.org> - 2012-06-09 22:02 +0000
    Re: linked list Ian Collins <ian-news@hotmail.com> - 2012-06-09 17:49 +1200
      Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-08 22:53 -0700
        Re: linked list Ian Collins <ian-news@hotmail.com> - 2012-06-09 18:36 +1200
        Re: linked list Keith Thompson <kst-u@mib.org> - 2012-06-09 11:20 -0700
    Re: linked list pete <pfiland@mindspring.com> - 2012-06-09 06:49 -0400
      Re: linked list pete <pfiland@mindspring.com> - 2012-06-09 07:28 -0400
        Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-09 07:57 -0700
          Re: linked list Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-09 19:16 +0100
          Re: linked list pete <pfiland@mindspring.com> - 2012-06-09 16:01 -0400
          Re: linked list pete <pfiland@mindspring.com> - 2012-06-09 16:50 -0400
            Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-09 18:21 -0700
              Re: linked list pete <pfiland@mindspring.com> - 2012-06-10 00:11 -0400
                Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-09 22:20 -0700
                  Re: linked list pete <pfiland@mindspring.com> - 2012-06-10 08:31 -0400
              Re: linked list nick_keighley_nospam@hotmail.com - 2012-06-11 06:10 -0700
                Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-11 09:16 -0700
                Re: linked list Keith Thompson <kst-u@mib.org> - 2012-06-11 15:45 -0700
                  Re: linked list Michael Press <rubrum@pacbell.net> - 2012-06-14 12:47 -0700

Page 1 of 2  [1] 2  Next page →


#22037 — linked list

FromZhang Yuan <zhabgyuan1993@gmail.com>
Date2012-06-08 21:02 -0700
Subjectlinked list
Message-ID<bc9a4156-754e-4251-bfab-d75241dad420@googlegroups.com>
----------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
struct stu{
    int num;
    char name[12];
    int age[0];
    struct stu *next;
    };

int main(){

struct stu *curr,*head;

head=(stu *)malloc(sizeof(stu));
curr=head;
scanf("%d",&(head->num));
curr->next=NULL;

while(curr->num!=0){
        scanf("%s%d",curr->name,curr->age);
        curr=(stu *)malloc(sizeof(struct stu));
        scanf("%d",&(curr->num));
        curr->next=NULL;
}
return 0;
}

--------------------------------------------------------------------------
it  doesn't work.When I debug:

input :
101 zhang 19 

the memory information:4 pictures 
http://flic.kr/p/ccMQey

why 0x6f17e0 change from 00-->13 unexpectedly.
I think the "*next" and "int age"storaged in different place.
but it looks like it overwrite?

[toc] | [next] | [standalone]


#22038

FromKaz Kylheku <kaz@kylheku.com>
Date2012-06-09 04:59 +0000
Message-ID<20120608213614.855@kylheku.com>
In reply to#22037
On 2012-06-09, Zhang Yuan <zhabgyuan1993@gmail.com> wrote:
> ----------------------------------------------------------------------------
> #include<stdio.h>
> #include<stdlib.h>
> struct stu{
>     int num;
>     char name[12];
>     int age[0];
>     struct stu *next;
>     };
>
> int main(){
>
> struct stu *curr,*head;
>
> head=(stu *)malloc(sizeof(stu));

If you're really working in C and not C++, you want sizeof (struct stu)
here (just like in your other malloc call below).

(In C++, a struct stu { ... } declaration will introduce stu as a type name,
but not in C.

In C, if you have some defined object called "stu" somewhere, or a type "stu",
this sizeof will measure the size of that object or type, and not "struct stu".

Example:

  int stu;

  head = (stu *) malloc(sizeof(stu)); /* actually sizeof int */
  head = (stu *) malloc(sizeof(struct stu)); /* size of the structure */

> curr=head;
> scanf("%d",&(head->num));

scanf without checking the return value is a very bad way to obtain input.
What if the next input character is not a digit? What if EOF is signaled?

scanf with error checking is still very bad for interactive input.

> curr->next=NULL;
>
> while(curr->num!=0){
>         scanf("%s%d",curr->name,curr->age);
>         curr=(stu *)malloc(sizeof(struct stu));
>         scanf("%d",&(curr->num));
>         curr->next=NULL;
> }

This leaks memory. You have assigned a new object to curr
and populated some of its fields, num and next.
but this object is not entered into the linked list.

If the input ends with EOF or is erroneous, you potentially
have an infinite loop. Worse, an infinite loop which
allocates memory.

None of that readily explains the overwrite of 19.

> return 0;
> }
>
> --------------------------------------------------------------------------
> it  doesn't work.When I debug:
>
> input :
> 101 zhang 19 
>
> the memory information:4 pictures 
> http://flic.kr/p/ccMQey
>
> why 0x6f17e0 change from 00-->13 unexpectedly.

At what point in the execution of the program?

If you single step the program, which line?

[toc] | [prev] | [next] | [standalone]


#22039

FromZhang Yuan <zhabgyuan1993@gmail.com>
Date2012-06-08 22:13 -0700
Message-ID<4c00d189-128c-438d-b2d1-96c5f2f241eb@googlegroups.com>
In reply to#22038
On Saturday, June 9, 2012 12:59:57 PM UTC+8, Kaz Kylheku wrote:
> On 2012-06-09, Zhang Yuan <zhabgyuan1993@gmail.com> wrote:
> > ----------------------------------------------------------------------------
> > #include<stdio.h>
> > #include<stdlib.h>
> > struct stu{
> >     int num;
> >     char name[12];
> >     int age[0];
> >     struct stu *next;
> >     };
> >
> > int main(){
> >
> > struct stu *curr,*head;
> >
> > head=(stu *)malloc(sizeof(stu));
> 
> If you're really working in C and not C++, you want sizeof (struct stu)
> here (just like in your other malloc call below).
> 
> (In C++, a struct stu { ... } declaration will introduce stu as a type name,
> but not in C.
> 
> In C, if you have some defined object called "stu" somewhere, or a type "stu",
> this sizeof will measure the size of that object or type, and not "struct stu".
> 
> Example:
> 
>   int stu;
> 
>   head = (stu *) malloc(sizeof(stu)); /* actually sizeof int */
>   head = (stu *) malloc(sizeof(struct stu)); /* size of the structure */
> 
> > curr=head;
> > scanf("%d",&(head->num));
> 
> scanf without checking the return value is a very bad way to obtain input.
> What if the next input character is not a digit? What if EOF is signaled?
> 
> scanf with error checking is still very bad for interactive input.
> 
> > curr->next=NULL;
> >
> > while(curr->num!=0){
> >         scanf("%s%d",curr->name,curr->age);
> >         curr=(stu *)malloc(sizeof(struct stu));
> >         scanf("%d",&(curr->num));
> >         curr->next=NULL;
> > }
> 
> This leaks memory. You have assigned a new object to curr
> and populated some of its fields, num and next.
> but this object is not entered into the linked list.
> 
> If the input ends with EOF or is erroneous, you potentially
> have an infinite loop. Worse, an infinite loop which
> allocates memory.
> 
> None of that readily explains the overwrite of 19.
> 
> > return 0;
> > }
> >
> > --------------------------------------------------------------------------
> > it  doesn't work.When I debug:
> >
> > input :
> > 101 zhang 19 
> >
> > the memory information:4 pictures 
> > http://flic.kr/p/ccMQey
> >
> > why 0x6f17e0 change from 00-->13 unexpectedly.
> 
> At what point in the execution of the program?
> 
> If you single step the program, which line?

I single step the program in line :

scanf("%d",&(head->num)); 












[toc] | [prev] | [next] | [standalone]


#22041

FromIan Collins <ian-news@hotmail.com>
Date2012-06-09 17:47 +1200
Message-ID<a3g6c9Fr2hU4@mid.individual.net>
In reply to#22038
On 06/ 9/12 04:59 PM, Kaz Kylheku wrote:
> On 2012-06-09, Zhang Yuan<zhabgyuan1993@gmail.com>  wrote:
>> ----------------------------------------------------------------------------
>> #include<stdio.h>
>> #include<stdlib.h>
>> struct stu{
>>      int num;
>>      char name[12];
>>      int age[0];
>>      struct stu *next;
>>      };
>>
>> int main(){
>>
>> struct stu *curr,*head;
>>
>> head=(stu *)malloc(sizeof(stu));
>
> If you're really working in C and not C++, you want sizeof (struct stu)
> here (just like in your other malloc call below).
>
> (In C++, a struct stu { ... } declaration will introduce stu as a type name,
> but not in C.
>
> In C, if you have some defined object called "stu" somewhere, or a type "stu",
> this sizeof will measure the size of that object or type, and not "struct stu".
>
> Example:
>
>    int stu;
>
>    head = (stu *) malloc(sizeof(stu)); /* actually sizeof int */
>    head = (stu *) malloc(sizeof(struct stu)); /* size of the structure */

Which one reason why

   head = malloc( sizeof *head );

is a good idea!

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#22045

FromZhang Yuan <zhabgyuan1993@gmail.com>
Date2012-06-08 22:59 -0700
Message-ID<acfbc6f4-9582-4692-bab3-eead1dd0a3ce@googlegroups.com>
In reply to#22041
On Saturday, June 9, 2012 1:47:53 PM UTC+8, Ian Collins wrote:
> On 06/ 9/12 04:59 PM, Kaz Kylheku wrote:
> > On 2012-06-09, Zhang Yuan<zhabgyuan1993@gmail.com>  wrote:
> >> ----------------------------------------------------------------------------
> >> #include<stdio.h>
> >> #include<stdlib.h>
> >> struct stu{
> >>      int num;
> >>      char name[12];
> >>      int age[0];
> >>      struct stu *next;
> >>      };
> >>
> >> int main(){
> >>
> >> struct stu *curr,*head;
> >>
> >> head=(stu *)malloc(sizeof(stu));
> >
> > If you're really working in C and not C++, you want sizeof (struct stu)
> > here (just like in your other malloc call below).
> >
> > (In C++, a struct stu { ... } declaration will introduce stu as a type name,
> > but not in C.
> >
> > In C, if you have some defined object called "stu" somewhere, or a type "stu",
> > this sizeof will measure the size of that object or type, and not "struct stu".
> >
> > Example:
> >
> >    int stu;
> >
> >    head = (stu *) malloc(sizeof(stu)); /* actually sizeof int */
> >    head = (stu *) malloc(sizeof(struct stu)); /* size of the structure */
> 
> Which one reason why
> 
>    head = malloc( sizeof *head );
> 
> is a good idea!
> 
> -- 
> Ian Collins

Sorry,I don't understand very well.
sizeof *head ==4 0r ==sizeof struct?

[toc] | [prev] | [next] | [standalone]


#22047

FromIan Collins <ian-news@hotmail.com>
Date2012-06-09 18:35 +1200
Message-ID<a3g95cFr2hU6@mid.individual.net>
In reply to#22045
On 06/ 9/12 05:59 PM, Zhang Yuan wrote:
> On Saturday, June 9, 2012 1:47:53 PM UTC+8, Ian Collins wrote:
>> On 06/ 9/12 04:59 PM, Kaz Kylheku wrote:
>>> On 2012-06-09, Zhang Yuan<zhabgyuan1993@gmail.com>   wrote:
>>>> ----------------------------------------------------------------------------
>>>> #include<stdio.h>
>>>> #include<stdlib.h>
>>>> struct stu{
>>>>       int num;
>>>>       char name[12];
>>>>       int age[0];
>>>>       struct stu *next;
>>>>       };
>>>>
>>>> int main(){
>>>>
>>>> struct stu *curr,*head;
>>>>
>>>> head=(stu *)malloc(sizeof(stu));
>>>
>>> If you're really working in C and not C++, you want sizeof (struct stu)
>>> here (just like in your other malloc call below).
>>>
>>> (In C++, a struct stu { ... } declaration will introduce stu as a type name,
>>> but not in C.
>>>
>>> In C, if you have some defined object called "stu" somewhere, or a type "stu",
>>> this sizeof will measure the size of that object or type, and not "struct stu".
>>>
>>> Example:
>>>
>>>     int stu;
>>>
>>>     head = (stu *) malloc(sizeof(stu)); /* actually sizeof int */
>>>     head = (stu *) malloc(sizeof(struct stu)); /* size of the structure */
>>
>> Which one reason why
>>
>>     head = malloc( sizeof *head );
>>
>> is a good idea!
>
> Sorry,I don't understand very well.
> sizeof *head ==4 0r ==sizeof struct?

"sizeof *head" translates to "size of the type of head".  So whatever 
type head is, malloc will return the correct size block.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#22050

FromIke Naar <ike@sverige.freeshell.org>
Date2012-06-09 06:53 +0000
Message-ID<slrn3vfsjt5sn2.l5k.ike@sverige.freeshell.org>
In reply to#22047
On 2012-06-09, Ian Collins <ian-news@hotmail.com> wrote:
> "sizeof *head" translates to "size of the type of head".  So whatever 
> type head is, malloc will return the correct size block.

Since head has a pointer type, it would be more accurate to
say that sizeof *head translates to "size of the type that
head points to", instead of "size of the type of head".

[toc] | [prev] | [next] | [standalone]


#22054

FromZhang Yuan <zhabgyuan1993@gmail.com>
Date2012-06-09 00:53 -0700
Message-ID<28b22212-7715-41fb-8a3b-c5b77947f412@t1g2000pbl.googlegroups.com>
In reply to#22050
On Jun 9, 2:53 pm, Ike Naar <i...@sverige.freeshell.org> wrote:
> On 2012-06-09, Ian Collins <ian-n...@hotmail.com> wrote:
>
> > "sizeof *head" translates to "size of the type of head".  So whatever
> > type head is, malloc will return the correct size block.
>
> Since head has a pointer type, it would be more accurate to
> say that sizeof *head translates to "size of the type that
> head points to", instead of "size of the type of head".

i'm fairly new to c .how to read the book: the c programming, remeber
all details in mind?

[toc] | [prev] | [next] | [standalone]


#22553

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-06-22 12:18 -0700
Message-ID<0f2b4381-3530-4eaf-9419-3672bc3daae2@googlegroups.com>
In reply to#22038
Kaz Kylheku於 2012年6月9日星期六UTC+8下午12時59分57秒寫道:
> On 2012-06-09, Zhang Yuan <zhabgyuan1993@gmail.com> wrote:
> > ----------------------------------------------------------------------------
> > #include<stdio.h>
> > #include<stdlib.h>
> > struct stu{
> >     int num;
> >     char name[12];
> >     int age[0];
> >     struct stu *next;
> >     };
> >
> > int main(){
> >
> > struct stu *curr,*head;
> >
> > head=(stu *)malloc(sizeof(stu));
> 
> If you're really working in C and not C++, you want sizeof (struct stu)
> here (just like in your other malloc call below).
> 
> (In C++, a struct stu { ... } declaration will introduce stu as a type name,
> but not in C.
> 
> In C, if you have some defined object called "stu" somewhere, or a type "stu",
> this sizeof will measure the size of that object or type, and not "struct stu".
> 
> Example:
> 
>   int stu;
> 
>   head = (stu *) malloc(sizeof(stu)); /* actually sizeof int */
>   head = (stu *) malloc(sizeof(struct stu)); /* size of the structure */
> 
> > curr=head;
> > scanf("%d",&(head->num));
> 
> scanf without checking the return value is a very bad way to obtain input.
> What if the next input character is not a digit? What if EOF is signaled?
> 
> scanf with error checking is still very bad for interactive input.
> 
> > curr->next=NULL;
> >
> > while(curr->num!=0){
> >         scanf("%s%d",curr->name,curr->age);
> >         curr=(stu *)malloc(sizeof(struct stu));
> >         scanf("%d",&(curr->num));
> >         curr->next=NULL;
> > }
> 
> This leaks memory. You have assigned a new object to curr
> and populated some of its fields, num and next.
> but this object is not entered into the linked list.
> 
> If the input ends with EOF or is erroneous, you potentially
> have an infinite loop. Worse, an infinite loop which
> allocates memory.
> 
> None of that readily explains the overwrite of 19.
> 
> > return 0;
> > }
> >
> > --------------------------------------------------------------------------
> > it  doesn't work.When I debug:
> >
> > input :
> > 101 zhang 19 
> >
> > the memory information:4 pictures 
> > http://flic.kr/p/ccMQey
> >
> > why 0x6f17e0 change from 00-->13 unexpectedly.
> 
> At what point in the execution of the program?
> 
> If you single step the program, which line?



Kaz Kylheku於 2012年6月9日星期六UTC+8下午12時59分57秒寫道:
> On 2012-06-09, Zhang Yuan <zhabgyuan1993@gmail.com> wrote:
> > ----------------------------------------------------------------------------
> > #include<stdio.h>
> > #include<stdlib.h>
> > struct stu{
> >     int num;
> >     char name[12];
> >     int age[0];
> >     struct stu *next;
> >     };
> >
> > int main(){
> >
> > struct stu *curr,*head;
> >
> > head=(stu *)malloc(sizeof(stu));
> 
> If you're really working in C and not C++, you want sizeof (struct stu)
> here (just like in your other malloc call below).
> 
> (In C++, a struct stu { ... } declaration will introduce stu as a type name,
> but not in C.
> 
> In C, if you have some defined object called "stu" somewhere, or a type "stu",
> this sizeof will measure the size of that object or type, and not "struct stu".
> 
> Example:
> 
>   int stu;
> 
>   head = (stu *) malloc(sizeof(stu)); /* actually sizeof int */
>   head = (stu *) malloc(sizeof(struct stu)); /* size of the structure */
> 
> > curr=head;
> > scanf("%d",&(head->num));
> 
> scanf without checking the return value is a very bad way to obtain input.
> What if the next input character is not a digit? What if EOF is signaled?
> 
> scanf with error checking is still very bad for interactive input.
> 
> > curr->next=NULL;
> >
> > while(curr->num!=0){
> >         scanf("%s%d",curr->name,curr->age);
> >         curr=(stu *)malloc(sizeof(struct stu));
> >         scanf("%d",&(curr->num));
> >         curr->next=NULL;
> > }
> 
> This leaks memory. You have assigned a new object to curr
> and populated some of its fields, num and next.
> but this object is not entered into the linked list.
> 
> If the input ends with EOF or is erroneous, you potentially
> have an infinite loop. Worse, an infinite loop which
> allocates memory.
> 
> None of that readily explains the overwrite of 19.
> 
> > return 0;
> > }
> >
> > --------------------------------------------------------------------------
> > it  doesn't work.When I debug:
> >
> > input :
> > 101 zhang 19 
> >
> > the memory information:4 pictures 
> > http://flic.kr/p/ccMQey
> >
> > why 0x6f17e0 change from 00-->13 unexpectedly.
> 
> At what point in the execution of the program?
> 
> If you single step the program, which line? 

Well, one can implement an instance of a container class 
that can be iterable in C.

What's next?

[toc] | [prev] | [next] | [standalone]


#22040

FromIke Naar <ike@sverige.freeshell.org>
Date2012-06-09 05:34 +0000
Message-ID<slrn3vfsjt5o3c.fhi.ike@sverige.freeshell.org>
In reply to#22037
On 2012-06-09, Zhang Yuan <zhabgyuan1993@gmail.com> wrote:
> struct stu{
>     int num;
>     char name[12];
>     int age[0];

There is room for zero ages in the struct ...

>     struct stu *next;
>     };
>
> int main(){
>
> struct stu *curr,*head;
>
> head=(stu *)malloc(sizeof(stu));
> curr=head;
> scanf("%d",&(head->num));
> curr->next=NULL;
>
> while(curr->num!=0){
>         scanf("%s%d",curr->name,curr->age);

... but here you try to store something in curr->age.
Since curr->age has size zero, the address of curr->age
can be the same as the address of curr->next, and storing
a value in curr->age may overwrite whatever was in curr->next.

>         curr=(stu *)malloc(sizeof(struct stu));
>         scanf("%d",&(curr->num));
>         curr->next=NULL;
> }
> return 0;
> }
>

[toc] | [prev] | [next] | [standalone]


#22043

FromZhang Yuan <zhabgyuan1993@gmail.com>
Date2012-06-08 22:51 -0700
Message-ID<716df42e-ee26-4419-8adf-f17a9f18125f@googlegroups.com>
In reply to#22040
On Saturday, June 9, 2012 1:34:36 PM UTC+8, Ike Naar wrote:
> On 2012-06-09, Zhang Yuan <zhabgyuan1993@gmail.com> wrote:
> > struct stu{
> >     int num;
> >     char name[12];
> >     int age[0];
> 
> There is room for zero ages in the struct ...
> 
> >     struct stu *next;
> >     };
> >
> > int main(){
> >
> > struct stu *curr,*head;
> >
> > head=(stu *)malloc(sizeof(stu));
> > curr=head;
> > scanf("%d",&(head->num));
> > curr->next=NULL;
> >
> > while(curr->num!=0){
> >         scanf("%s%d",curr->name,curr->age);
> 
> ... but here you try to store something in curr->age.
> Since curr->age has size zero, the address of curr->age
> can be the same as the address of curr->next, and storing
> a value in curr->age may overwrite whatever was in curr->next.
> 
> >         curr=(stu *)malloc(sizeof(struct stu));
> >         scanf("%d",&(curr->num));
> >         curr->next=NULL;
> > }
> > return 0;
> > }
> >

Why the address of curr->age can be the same as the address of curr->next,
not the address of curr-name[12],or other?

[toc] | [prev] | [next] | [standalone]


#22049

FromIke Naar <ike@sverige.freeshell.org>
Date2012-06-09 06:45 +0000
Message-ID<slrn3vfsjt5s81.l5k.ike@sverige.freeshell.org>
In reply to#22043
On 2012-06-09, Zhang Yuan <zhabgyuan1993@gmail.com> wrote:
> On Saturday, June 9, 2012 1:34:36 PM UTC+8, Ike Naar wrote:
>> On 2012-06-09, Zhang Yuan <zhabgyuan1993@gmail.com> wrote:
>> > struct stu{
>> >     int num;
>> >     char name[12];
>> >     int age[0];
>> 
>> There is room for zero ages in the struct ...
>> 
>> >     struct stu *next;
>> >     };
>>
>> >         scanf("%s%d",curr->name,curr->age);
>> 
>> ... but here you try to store something in curr->age.
>> Since curr->age has size zero, the address of curr->age
>> can be the same as the address of curr->next, and storing
>> a value in curr->age may overwrite whatever was in curr->next.
>
> Why the address of curr->age can be the same as the address of curr->next,
> not the address of curr-name[12],or other?

6.7.2.1 p13 of the standard (Structure and union specifiers) says:
"Within a structure object, the non-bitfield members and the units in
which bitfields reside have addresses that increase in the order in
which they are declared."

That means, for struct stu S,

  (char*)&S.num <= (char*)&S.name <= (char*)&S.age <= (char*)&S.next

Since sizeof S.num and sizeof S.name are nonzero, it follows that

  (char*)&S.num < (char*)&S.name < (char*)&S.age <= (char*)&S.next

so the only members that can have the same address are S.age and S.next .

There can be padding between S.age and S.next, so it is
not *required* that they have the same address.

Just out of curiousity: why did you declare age as a zero-sized array?

[toc] | [prev] | [next] | [standalone]


#22053

FromZhang Yuan <zhabgyuan1993@gmail.com>
Date2012-06-08 23:56 -0700
Message-ID<ba9dc476-c39d-41e6-9a9c-8bc9354fff22@ra8g2000pbc.googlegroups.com>
In reply to#22049
On Jun 9, 2:45 pm, Ike Naar <i...@sverige.freeshell.org> wrote:
> On 2012-06-09, Zhang Yuan <zhabgyuan1...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > On Saturday, June 9, 2012 1:34:36 PM UTC+8, Ike Naar wrote:
> >> On 2012-06-09, Zhang Yuan <zhabgyuan1...@gmail.com> wrote:
> >> > struct stu{
> >> >     int num;
> >> >     char name[12];
> >> >     int age[0];
>
> >> There is room for zero ages in the struct ...
>
> >> >     struct stu *next;
> >> >     };
>
> >> >         scanf("%s%d",curr->name,curr->age);
>
> >> ... but here you try to store something in curr->age.
> >> Since curr->age has size zero, the address of curr->age
> >> can be the same as the address of curr->next, and storing
> >> a value in curr->age may overwrite whatever was in curr->next.
>
> > Why the address of curr->age can be the same as the address of curr->next,
> > not the address of curr-name[12],or other?
>
> 6.7.2.1 p13 of the standard (Structure and union specifiers) says:
> "Within a structure object, the non-bitfield members and the units in
> which bitfields reside have addresses that increase in the order in
> which they are declared."
>
> That means, for struct stu S,
>
>   (char*)&S.num <= (char*)&S.name <= (char*)&S.age <= (char*)&S.next
>
> Since sizeof S.num and sizeof S.name are nonzero, it follows that
>
>   (char*)&S.num < (char*)&S.name < (char*)&S.age <= (char*)&S.next
>
> so the only members that can have the same address are S.age and S.next .
>
> There can be padding between S.age and S.next, so it is
> not *required* that they have the same address.
>
> Just out of curiousity: why did you declare age as a zero-sized array?

thank you.very explicit!
i admire you to figure much language details.
i just made a mistake declared age as array,but i didn't notice that.

[toc] | [prev] | [next] | [standalone]


#22072

FromKeith Thompson <kst-u@mib.org>
Date2012-06-09 11:17 -0700
Message-ID<lnsje49wob.fsf@nuthaus.mib.org>
In reply to#22040
Ike Naar <ike@sverige.freeshell.org> writes:
> On 2012-06-09, Zhang Yuan <zhabgyuan1993@gmail.com> wrote:
>> struct stu{
>>     int num;
>>     char name[12];
>>     int age[0];
>
> There is room for zero ages in the struct ...
[...]

Actually, C doesn't permit zero-sized arrays.  Declarating "int age[0]",
is a constraint violation, requiring a diagnostic (N1570 6.7.6.2p1).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#22083

FromIke Naar <ike@iceland.freeshell.org>
Date2012-06-09 22:02 +0000
Message-ID<slrn3vfsjt7hvq.f7r.ike@iceland.freeshell.org>
In reply to#22072
On 2012-06-09, Keith Thompson <kst-u@mib.org> wrote:
> Ike Naar <ike@sverige.freeshell.org> writes:
>> On 2012-06-09, Zhang Yuan <zhabgyuan1993@gmail.com> wrote:
>>> struct stu{
>>>     int num;
>>>     char name[12];
>>>     int age[0];
>>
>> There is room for zero ages in the struct ...
> [...]
>
> Actually, C doesn't permit zero-sized arrays.  Declarating "int age[0]",
> is a constraint violation, requiring a diagnostic (N1570 6.7.6.2p1).

True. Some compilers allow it as an extension
(e.g. gcc in default mode accepts it without a diagnostic).
With -ansi -pedantic gcc does give a diagnostic, but still generates
an executable.

[toc] | [prev] | [next] | [standalone]


#22042

FromIan Collins <ian-news@hotmail.com>
Date2012-06-09 17:49 +1200
Message-ID<a3g6esFr2hU5@mid.individual.net>
In reply to#22037
On 06/ 9/12 04:02 PM, Zhang Yuan wrote:
> ----------------------------------------------------------------------------
> #include<stdio.h>
> #include<stdlib.h>
> struct stu{
>      int num;
>      char name[12];
>      int age[0];

Didn't your compiler warn about this?

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#22044

FromZhang Yuan <zhabgyuan1993@gmail.com>
Date2012-06-08 22:53 -0700
Message-ID<f9905002-0a61-40b5-8c64-1176925940d1@googlegroups.com>
In reply to#22042
On Saturday, June 9, 2012 1:49:16 PM UTC+8, Ian Collins wrote:
> On 06/ 9/12 04:02 PM, Zhang Yuan wrote:
> > ----------------------------------------------------------------------------
> > #include<stdio.h>
> > #include<stdlib.h>
> > struct stu{
> >      int num;
> >      char name[12];
> >      int age[0];
> 
> Didn't your compiler warn about this?
> 
> -- 
> Ian Collins

Yes,just warn.I didn't pay much attention to that
Thank you.

[toc] | [prev] | [next] | [standalone]


#22048

FromIan Collins <ian-news@hotmail.com>
Date2012-06-09 18:36 +1200
Message-ID<a3g970Fr2hU7@mid.individual.net>
In reply to#22044
On 06/ 9/12 05:53 PM, Zhang Yuan wrote:
> On Saturday, June 9, 2012 1:49:16 PM UTC+8, Ian Collins wrote:
>> On 06/ 9/12 04:02 PM, Zhang Yuan wrote:
>>> ----------------------------------------------------------------------------
>>> #include<stdio.h>
>>> #include<stdlib.h>
>>> struct stu{
>>>       int num;
>>>       char name[12];
>>>       int age[0];
>>
>> Didn't your compiler warn about this?
>
> Yes,just warn.I didn't pay much attention to that
> Thank you.

Valuable lesson learned then!

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#22073

FromKeith Thompson <kst-u@mib.org>
Date2012-06-09 11:20 -0700
Message-ID<lnobos9wjx.fsf@nuthaus.mib.org>
In reply to#22044
Zhang Yuan <zhabgyuan1993@gmail.com> writes:
> On Saturday, June 9, 2012 1:49:16 PM UTC+8, Ian Collins wrote:
>> On 06/ 9/12 04:02 PM, Zhang Yuan wrote:
>> > #include<stdio.h>
>> > #include<stdlib.h>
>> > struct stu{
>> >      int num;
>> >      char name[12];
>> >      int age[0];
>> 
>> Didn't your compiler warn about this?
>
> Yes,just warn.I didn't pay much attention to that

Always pay close attention to warnings.

gcc in particular often produces warnings for constraint violations,
i.e., errors for which a compiler is permitted to reject the program
altogether.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#22057

Frompete <pfiland@mindspring.com>
Date2012-06-09 06:49 -0400
Message-ID<4FD32A2F.1A9E@mindspring.com>
In reply to#22037
Zhang Yuan wrote:
> 
> ----------------------------------------------------------------------------
> #include<stdio.h>
> #include<stdlib.h>
> struct stu{
>     int num;
>     char name[12];
>     int age[0];
>     struct stu *next;
>     };
> 
> int main(){
> 
> struct stu *curr,*head;
> 
> head=(stu *)malloc(sizeof(stu));
> curr=head;
> scanf("%d",&(head->num));
> curr->next=NULL;
> 
> while(curr->num!=0){
>         scanf("%s%d",curr->name,curr->age);
>         curr=(stu *)malloc(sizeof(struct stu));
>         scanf("%d",&(curr->num));
>         curr->next=NULL;
> }
> return 0;
> }
> 
> --------------------------------------------------------------------------
> it  doesn't work.When I debug:
> 
> input :
> 101 zhang 19
> 
> the memory information:4 pictures
> http://flic.kr/p/ccMQey
> 
> why 0x6f17e0 change from 00-->13 unexpectedly.
> I think the "*next" and "int age"storaged in different place.
> but it looks like it overwrite?

#include <stdio.h>
#include <stdlib.h>

struct stu{
    struct stu *next;
    int num;
    char name[12];
    int age;
    };

int 
main(void)
{
    struct stu *curr,*head;
    
    head = malloc(sizeof *head);
    if (head != NULL) {
        curr = head;
        puts("enter number");
        scanf("%d", &(head -> num));
        curr -> next = NULL;
        while(curr -> num != 0){
            puts("enter name and age");
            scanf("%s%d", curr -> name, &(curr -> age));
            curr = malloc(sizeof *curr);
            if (curr == NULL) {
                break;
            }
            puts("enter number");
            scanf("%d", &(curr -> num));
            curr -> next = NULL;
        }
    }
    return 0;
}

-- 
pete

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.c


csiph-web