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 Previous  1, 2, 3
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language
View previous topic :: View next topic  
Author Message
Keith Thompson
Guest





PostPosted: Mon Jun 11, 2012 8:45 pm    Post subject: Re: linked list Reply with quote



nick_keighley_nospam (AT) hotmail (DOT) com writes:
Quote:
On Sunday, June 10, 2012 2:21:23 AM UTC+1, Zhang Yuan wrote:
snip

I learn a lot!
Your code is very comprehensive.
I found the difference between natives and foreigners.
I post the same question on comp.lang.c and home forum.
This is explicit and penetrate deeply.

the average age and years experience on comp.lang.c (clc) is probably
higher than your home forum. Or put it another way- clc has an ageing demographic!

Thank you!
I want to know if there exist some standards about linked list?

any book on algorithms. Sedgewick "Algorithms" is quite readable,
Knuth "The Art of Computer programming" is rather thorough.
The wikipedia entry for "Linked List" looks ok (I haven't read
it carefully).

Knuth is *extremely* thorough, but it's not a tutorial.

--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) 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"
Back to top
Michael Press
Guest





PostPosted: Thu Jun 14, 2012 5:47 pm    Post subject: Re: linked list Reply with quote



In article <lnk3zda2nn.fsf (AT) nuthaus (DOT) mib.org>,
Keith Thompson <kst-u (AT) mib (DOT) org> wrote:

Quote:
nick_keighley_nospam (AT) hotmail (DOT) com writes:
On Sunday, June 10, 2012 2:21:23 AM UTC+1, Zhang Yuan wrote:
snip

I learn a lot!
Your code is very comprehensive.
I found the difference between natives and foreigners.
I post the same question on comp.lang.c and home forum.
This is explicit and penetrate deeply.

the average age and years experience on comp.lang.c (clc) is probably
higher than your home forum. Or put it another way- clc has an ageing demographic!

Thank you!
I want to know if there exist some standards about linked list?

any book on algorithms. Sedgewick "Algorithms" is quite readable,
Knuth "The Art of Computer programming" is rather thorough.
The wikipedia entry for "Linked List" looks ok (I haven't read
it carefully).

Knuth is *extremely* thorough, but it's not a tutorial.

Why not? It is expository and there are exercises to work.

--
Michael Press
Back to top
88888 Dihedral
Guest





PostPosted: Fri Jun 22, 2012 7:18 pm    Post subject: Re: linked list Reply with quote



Kaz Kylheku於 2012年6月9日星期六UTC+8下午12時59分57秒寫道:
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?



Kaz Kylheku於 2012年6月9日星期六UTC+8下午12時59分57秒寫道:
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?

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

What's next?
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language All times are GMT
Goto page Previous  1, 2, 3
Page 3 of 3

 
 


Powered by phpBB © 2001, 2006 phpBB Group