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 

getline and linked list problem

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Kay
Guest





PostPosted: Mon Aug 30, 2004 2:33 pm    Post subject: getline and linked list problem Reply with quote



This function is used getline function to get data. The data is stored
as String. I want to add it in a linked list. However, the strcpy that I
have pointed cause the program segementation fault. What I can do to
add the item in the linked list and it don't need to change the Linked
list ADT item type ?


void load(istream& is, Position p, List * r )

string temp_name;

//get the restaurant name in restaurnat.txt
while ( getline(is, temp_name, 'n')) {

char *name;

//change type of name of restaurant from string to char
name = new char[temp_name.length() + 1 ];
strcpy( name, temp_name.c_str()); <-- This one

char *namet;

p = ListHead(r);
namet = (char *)malloc(sizeof(name));
strcpy (namet, name);

//Add the namet in the linked list
if (!rest_get_name(r, p, namet)) {
exit (EXIT_FAILURE);
}

Back to top
Karl Heinz Buchegger
Guest





PostPosted: Mon Aug 30, 2004 2:51 pm    Post subject: Re: getline and linked list problem Reply with quote



Kay wrote:
Quote:

This function is used getline function to get data. The data is stored
as String. I want to add it in a linked list. However, the strcpy that I
have pointed cause the program segementation fault. What I can do to
add the item in the linked list and it don't need to change the Linked
list ADT item type ?

void load(istream& is, Position p, List * r )

string temp_name;

//get the restaurant name in restaurnat.txt
while ( getline(is, temp_name, 'n')) {

char *name;

//change type of name of restaurant from string to char
name = new char[temp_name.length() + 1 ];
strcpy( name, temp_name.c_str()); <-- This one


That should be ok.
It might be that you managed to blow up the memory management
in a previous call or somewhere else in your program ...

Quote:
char *namet;

p = ListHead(r);
namet = (char *)malloc(sizeof(name));
strcpy (namet, name);

.... this is suspect.
namet points to memory with the sizeof a char pointer.
Yet you insist on copying an arbitrary number of characters
to it. Shouldn't the above sequence read

namet = (char*)malloc( strlen( name ) + 1 );

Quote:

//Add the namet in the linked list
if (!rest_get_name(r, p, namet)) {
exit (EXIT_FAILURE);
}

PS: Is there a reason you are using malloc in a C++ program?

--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Kay
Guest





PostPosted: Mon Aug 30, 2004 2:56 pm    Post subject: Re: getline and linked list problem Reply with quote



Yes, I can solve this problem. THX for your reading. ^^

Kay wrote:
Quote:
This function is used getline function to get data. The data is stored
as String. I want to add it in a linked list. However, the strcpy that I
have pointed cause the program segementation fault. What I can do to
add the item in the linked list and it don't need to change the Linked
list ADT item type ?


void load(istream& is, Position p, List * r )

string temp_name;

//get the restaurant name in restaurnat.txt
while ( getline(is, temp_name, 'n')) {

char *name;

//change type of name of restaurant from string to char
name = new char[temp_name.length() + 1 ];
strcpy( name, temp_name.c_str()); <-- This one

char *namet;

p = ListHead(r);
namet = (char *)malloc(sizeof(name));
strcpy (namet, name);

//Add the namet in the linked list
if (!rest_get_name(r, p, namet)) {
exit (EXIT_FAILURE);
}



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.