Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Kaz Kylheku <kaz@kylheku.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: linked list |
| Date | 2012-06-09 04:59 +0000 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <20120608213614.855@kylheku.com> (permalink) |
| References | <bc9a4156-754e-4251-bfab-d75241dad420@googlegroups.com> |
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?
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web