What does new char(size) mean in C++? -


while allocating buffer via new (in c++) mistakenly wrote:

int size = 1024 * 1024; char *buf = new char(size); 

instead of normal syntax

char *buf = new char[size]; 

however code did compiled , led strange crashes when using buf variable. apparently seems buffer of 8 bytes allocated.

i know meaning of new char(size).

char *buf = new char(size); 

it allocates sizeof(char) bytes , initializes sizeloosely equivalent this:

char *__internal_buf = new char; //1 byte  *__internal_buf = size; //or static_cast<char>(size);   char *buf = __internal_buf;  

it same difference in these two:

 char x(65);  //y single element initialized 'a'. 65 'a'  char y[65];  //y array of size 65 

hope helps.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -