getline and linked list problem

Discussion in 'C++' started by Kay, Aug 30, 2004.

  1. Kay

    Kay Guest

    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);
    }
     
    Kay, Aug 30, 2004
    #1
    1. Advertising

  2. Kay wrote:
    >
    > 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 ...

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

    >
    > //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
     
    Karl Heinz Buchegger, Aug 30, 2004
    #2
    1. Advertising

  3. Kay

    Kay Guest

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

    Kay wrote:
    > 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);
    > }
    >
     
    Kay, Aug 30, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    494
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    489
    emerth
    Jul 10, 2003
  3. fool
    Replies:
    14
    Views:
    526
    Barry Schwarz
    Jul 3, 2006
  4. joshd
    Replies:
    12
    Views:
    685
    John Carson
    Oct 2, 2006
  5. jawdoc
    Replies:
    9
    Views:
    779
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page