2

I have a code to implement linked list it works fine. I want to include a string into the linked list however when I did add string, I got a run time error crashes the program when it reaches this line:

new_node->name = "hello";

The complete code:

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

struct list_item
{
    int key;
    int value;
    string name;
    list_item *next;
};

struct list
{
    struct list_item *first;
};

int main()
{
    //Just one head is needed, you can also create this
    // on the stack just write:
    //list head;
    //head.first = NULL;
    list *head = (list*)malloc(sizeof(list));
    list_item *new_node = NULL;
    head->first = NULL;

    for(int i = 0; i < 10; i++)
    {
        //allocate memory for new_node
        new_node = (list_item*)malloc(sizeof(list_item));
        //adding the values
        new_node->key = i;
        new_node->name = "hello";
        new_node->value = 10 + i;

        //if the list is empty, the element you are inserting
        //doesn't have a next element

        new_node->next = head->first;

        //point first to new_node. This will result in a LIFO
        //(Last in First out) behaviour. You can see that when you 
        //compile
        head->first = new_node;

    }

     //print the list 
     list_item *travel;
     travel = head->first;

     while(travel != NULL)
     {
         cout << travel->value << endl;
         cout << travel->name << endl;
         travel = travel->next;
     }

    //here it doesn't matter, but in general you should also make
    //sure to free the elements
    return 0;
}

could anyone please help me how to include a string by the right way in c++?

AL3MS
  • 91
  • 3
  • 9
  • 3
    Gah! You have the unbounded glory that is C++, why degrade yourself to the likes of `malloc`! – scohe001 Apr 24 '18 at 18:05
  • 4
    Don't use `malloc` it creates uninitialised storage use `new` instead; `std::string name` is not initialised correctly. – Richard Critten Apr 24 '18 at 18:05
  • 1
    This seems like a mix between C and C++ – Philipp Apr 24 '18 at 18:05
  • Thanks guys it is working now after replace malloc with new :) – AL3MS Apr 24 '18 at 18:10
  • 1
    `std::string` lives and dies by its constructor setting up the object's internal state. `malloc` only allocates storage. It does not run constructors. Handy extra reading: [What are POD types in C++?](https://stackoverflow.com/questions/146452/what-are-pod-types-in-c) and in nitty gritty details: [C++ concepts: PODType](http://en.cppreference.com/w/cpp/concept/PODType) – user4581301 Apr 24 '18 at 18:10
  • The run time error is due to string constructor not called when using malloc. You can also use placement new to construct the string object using the memory allocated from malloc. Add the following line after the memory for new_node is allocated --- new_node = new(new_node) list_item; – Amit Rastogi Apr 25 '18 at 06:25

1 Answers1

3

you mixed C and C++ use either one. Instead of malloc use new to allocate memory.

Just use

new_node = new(list_item); 

instead of

new_node = (list_item*)malloc(sizeof(list_item)); and at last free that by calling delete

Don't use malloc as it doesn't initialize the memory. To check just run g++ -g -Wall -fdump-tree-gimple test.cpp and open vim test.cpp.004t.gimple and find the malloc() line, it says

new_node = malloc (16); /* allocated using malloc i.e not initialized */
D.21870 = &new_node->name; /* base address of string data type 'name' */
std::basic_string<char>::operator= (D.21870, "hello"); /* it causes the seg. fault */

To get more see this In what cases do I use malloc vs new?

Achal
  • 11,821
  • 2
  • 15
  • 37