linked list

Z

Zhang Yuan

----------------------------------------------------------------------------
#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?
 
K

Kaz Kylheku

----------------------------------------------------------------------------
#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?
 
Z

Zhang Yuan

----------------------------------------------------------------------------
#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));
 
I

Ike Naar

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.
 
I

Ian Collins

----------------------------------------------------------------------------
#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!
 
I

Ian Collins

----------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
struct stu{
int num;
char name[12];
int age[0];

Didn't your compiler warn about this?
 
Z

Zhang Yuan

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?
 
Z

Zhang Yuan

----------------------------------------------------------------------------
#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.
 
Z

Zhang Yuan

----------------------------------------------------------------------------
#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?
 
I

Ian Collins

----------------------------------------------------------------------------
#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.
 
I

Ike Naar

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?
 
I

Ike Naar

"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".
 
Z

Zhang Yuan

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.
 
Z

Zhang Yuan

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?
 
Z

Zhang Yuan

pete said:
Zhang said:
----------------------------------------------------------------------------
#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);

That doesn't make any sense!

/* BEGIN new.c */

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

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

void stu_free(struct stu *head);
void stu_print(struct stu *head);

int
main(void)
{
struct stu *curr,*head;

puts("/* BEGIN new.c */ output\n");
head = malloc(sizeof *head);
if (head != NULL) {
curr = head;
curr -> next = NULL;
puts("enter number");
if (1 > scanf("%d", &(head -> num))) {
puts("\n\nscanf problem.\n\n");
exit(EXIT_FAILURE);
}
while(curr -> num != 0) {
puts("enter name and age");
scanf("%s%d", curr -> name, &(curr -> age));
if (1 > scanf("%d", &(head -> num))) {
puts("\n\nscanf problem.\n\n");
exit(EXIT_FAILURE);
}
curr -> next= malloc(sizeof *(curr -> next));
if (curr -> next == NULL) {
break;
}
curr = curr -> next;
curr -> next = NULL;
puts("enter number");
scanf("%d", &(curr -> num));
}
*(curr -> name) = '\0';
curr -> age = 0;
}
stu_print(head);
stu_free(head);
puts("\n/* END new.c */ output");
return 0;
}


void
stu_free(struct stu *head)
{
struct stu *next;

while (head != NULL) {
next = head -> next;
free(head);
head = next;
}

Why not just free head?
 
B

Ben Bacarisse

Zhang Yuan said:
Why not just free head?

Do you understand what pete's code is doing? Are you saying that there
is a simpler why to write code that does the same thing (if,write it out
so it's clear to everyone exactly what you are suggesting), or are you
saying that pete's function does not need to do everything that it does
and thus can be sorter or simpler?

You might want to start sniping your replies so that they contain only
the parts needed to understand your comments. It will make them easier
to read and simpler to reply to.
 
K

Keith Thompson

Ike Naar said:
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).
 
K

Keith Thompson

Zhang Yuan said:
#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.
 
I

Ike Naar

Ike Naar said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top