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

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
  • 22,088
  • 14
  • 73
  • 128