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 size
— loosely 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
Post a Comment