0

I tried this

string *codes = (string*)malloc(256*sizeof(string));
codes[0] = "";

in C++. But it didn't work, but when I tried

string *codes = new string[256];
codes[0] = "";

This worked. I did not understand the basic idea behind why this is happening. Could someone please tell me.

Thanks

Karthik Prakash
  • 153
  • 4
  • 11

1 Answers1

3

This is because new uses constructor of the given class (in your case: std::string) and malloc() doesn't do this.

syntagma
  • 23,346
  • 16
  • 78
  • 134