7

The compiler throws runtime segfault upon following code :

#include <iostream>
#include <string>
using namespace std;

struct Node{
  int data;
  void *next;   
  string nodeType;
};

Node* initNode(int data){
  Node *n = (Node*)malloc(sizeof(Node));
  n->data = data;
  n->next = NULL;
  n->nodeType = "Node";   //if this line is commented it works else segfault
  return n;
}

int main() {
  Node *n1 = initNode(10);
  cout << n1->data << endl;
}

Can someone please explain why string assignment does not work inside a struct which is dynamically allocated where in case of static allocation why it works ?

where as the following way it works :

Node initNode(string data){
  Node n;
  n.data = data;  //This works for node creation statically
  n.next = NULL;
  n.nodeType = "Node";  //and even this works for node creation statically
  return n;
}

and then in the main function:

int main() {
  Node n2 = initNode("Hello");
  cout << n2.data << endl;
}
2
  • 1
    malloc just allocates memory, it doesn't construct the object instance, which means the string constructor is not called. Commented Mar 17, 2015 at 9:53
  • 3
    General rule of thumb : don't use C's malloc in C++, use new, and actually don't use new either if possible, and use make_shared or std::unique_ptr (C++11) Commented Mar 17, 2015 at 10:06

4 Answers 4

8

This doesn't work because you don't actually construct a Node instance into the memory which you malloc.

You should use new instead:

Node *n = new Node{};

malloc only allocates memory, it has no idea what a class is or how to instantiate one. You should generally not use it in C++.

new allocates memory and constructs an instance of the class.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey yaa this works: Node *n = new Node(); But i thought that malloc would actually allocate memory by creating instance ? Isn't that true ? can you please explain or provide pointers to difference between doing "new" and "malloc" and when to use it ?
yup right! thanks! well I actually had a feeling that the string's constructor would be called when I did malloc as it was string and not string*
malloc doesn't call any constructors of any members of your class.
4

There is no place, where std::string constructor is executed.

You should use new

example *e = new example;

or placement new

void *example_raw = malloc(sizeof(example));
example *e = new(example_raw) example;

Comments

3
 Node *n = (Node*)malloc(sizeof(Node));

This cast is nonsense. You can't just tell the compiler to pretend that a chunk of data you just allocate contains a valid Node object and then manipulate it.

1 Comment

yup right! well I actually had a feeling that the string's constructor would be called when I did malloc as it was string and not string* inside the Node
1

string in c++ is an class and to create string objects use new instead of malloc as below .

Node *n = new Node{};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.