C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

linked list
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language
View previous topic :: View next topic  
Author Message
Zhang Yuan
Guest





PostPosted: Sat Jun 09, 2012 4:02 am    Post subject: linked list Reply with quote



----------------------------------------------------------------------------
#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?
Back to top
Ian Collins
Guest





PostPosted: Sat Jun 09, 2012 4:02 am    Post subject: Re: linked list Reply with quote



On 06/ 9/12 04:59 PM, Kaz Kylheku wrote:
Quote:
On 2012-06-09, Zhang Yuan<zhabgyuan1993 (AT) gmail (DOT) 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
Back to top
Ian Collins
Guest





PostPosted: Sat Jun 09, 2012 4:02 am    Post subject: Re: linked list Reply with quote



On 06/ 9/12 04:02 PM, Zhang Yuan wrote:
Quote:
----------------------------------------------------------------------------
#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
Back to top
Ian Collins
Guest





PostPosted: Sat Jun 09, 2012 4:35 am    Post subject: Re: linked list Reply with quote

On 06/ 9/12 05:59 PM, Zhang Yuan wrote:
Quote:
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 (AT) gmail (DOT) 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
Back to top
Ian Collins
Guest





PostPosted: Sat Jun 09, 2012 4:36 am    Post subject: Re: linked list Reply with quote

On 06/ 9/12 05:53 PM, Zhang Yuan wrote:
Quote:
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
Back to top
Kaz Kylheku
Guest





PostPosted: Sat Jun 09, 2012 4:59 am    Post subject: Re: linked list Reply with quote

On 2012-06-09, Zhang Yuan <zhabgyuan1993 (AT) gmail (DOT) com> wrote:
Quote:
----------------------------------------------------------------------------
#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 */

Quote:
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.

Quote:
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.

Quote:
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?
Back to top
Zhang Yuan
Guest





PostPosted: Sat Jun 09, 2012 5:13 am    Post subject: Re: linked list Reply with quote

On Saturday, June 9, 2012 12:59:57 PM UTC+8, Kaz Kylheku wrote:
Quote:
On 2012-06-09, Zhang Yuan <zhabgyuan1993 (AT) gmail (DOT) 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));
Back to top
Ike Naar
Guest





PostPosted: Sat Jun 09, 2012 5:34 am    Post subject: Re: linked list Reply with quote

On 2012-06-09, Zhang Yuan <zhabgyuan1993 (AT) gmail (DOT) com> wrote:
Quote:
struct stu{
int num;
char name[12];
int age[0];

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

Quote:
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.

Quote:
curr=(stu *)malloc(sizeof(struct stu));
scanf("%d",&(curr->num));
curr->next=NULL;
}
return 0;
}
Back to top
Zhang Yuan
Guest





PostPosted: Sat Jun 09, 2012 5:51 am    Post subject: Re: linked list Reply with quote

On Saturday, June 9, 2012 1:34:36 PM UTC+8, Ike Naar wrote:
Quote:
On 2012-06-09, Zhang Yuan <zhabgyuan1993 (AT) gmail (DOT) 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?
Back to top
Zhang Yuan
Guest





PostPosted: Sat Jun 09, 2012 5:53 am    Post subject: Re: linked list Reply with quote

On Saturday, June 9, 2012 1:49:16 PM UTC+8, Ian Collins wrote:
Quote:
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.
Back to top
Zhang Yuan
Guest





PostPosted: Sat Jun 09, 2012 5:59 am    Post subject: Re: linked list Reply with quote

On Saturday, June 9, 2012 1:47:53 PM UTC+8, Ian Collins wrote:
Quote:
On 06/ 9/12 04:59 PM, Kaz Kylheku wrote:
On 2012-06-09, Zhang Yuan<zhabgyuan1993 (AT) gmail (DOT) 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?
Back to top
Ike Naar
Guest





PostPosted: Sat Jun 09, 2012 6:45 am    Post subject: Re: linked list Reply with quote

On 2012-06-09, Zhang Yuan <zhabgyuan1993 (AT) gmail (DOT) com> wrote:
Quote:
On Saturday, June 9, 2012 1:34:36 PM UTC+8, Ike Naar wrote:
On 2012-06-09, Zhang Yuan <zhabgyuan1993 (AT) gmail (DOT) 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?
Back to top
Ike Naar
Guest





PostPosted: Sat Jun 09, 2012 6:53 am    Post subject: Re: linked list Reply with quote

On 2012-06-09, Ian Collins <ian-news (AT) hotmail (DOT) com> wrote:
Quote:
"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".
Back to top
Zhang Yuan
Guest





PostPosted: Sat Jun 09, 2012 6:56 am    Post subject: Re: linked list Reply with quote

On Jun 9, 2:45 pm, Ike Naar <i...@sverige.freeshell.org> wrote:
Quote:
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.
Back to top
Zhang Yuan
Guest





PostPosted: Sat Jun 09, 2012 7:53 am    Post subject: Re: linked list Reply with quote

On Jun 9, 2:53 pm, Ike Naar <i...@sverige.freeshell.org> wrote:
Quote:
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?
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
 


Powered by phpBB © 2001, 2006 phpBB Group