4

Consider

char *p_c = new char['1', '2', '3', '4'];

Is this syntax correct? If yes, then what does it do?

I don’t know why, but compiler allows this syntax! What will it do with regards to memory? I am not able to access the variable by *p_c. How does one determine the size of and the number of elements present?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Neilkantha
  • 91
  • 6

3 Answers3

16

Your code is syntactically valid C++, if rather strange, and I don't think it does what you intended:

new char['1', '2', '3', '4'] is evaluated as new char['4'] due to the way the comma operator works. (The preceding elements are evaluated from left to right, but the value of the expression is that of the rightmost element.)

So your statement is equivalent to char *p_c = new char['4'];

'4' is a char type with a numeric value that depends on the encoding that your platform uses (ASCII, EBCDIC &c. although the former is most likely on a desktop system.).

So the number of elements in the array is whatever '4' evaluates to when converted to a size_t. On an ASCII system the number of elements would be 52.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    Note that '4' must evaluate to a positive number. (It's a character in the basic execution set.) So whatever happens, the expression is legal. (Other characters might be negative, so would produce and error at runtime.) – Martin Bonner supports Monica Dec 23 '16 at 11:43
  • 1
    It's probably putting something like "but it almost certainly doesn't do what you want it to." somewhere near the top. – Martin Bonner supports Monica Dec 23 '16 at 11:44
  • @MartinBonner: That's helpful. Presumably `char` must always be a `unsigned` type on a system with EBCDIC encoding. Else `'4'` (which has an unsigned value 244) would be negative and would thus convert to a very large `size_t`! – Bathsheba Dec 23 '16 at 11:44
  • 1
    @MartinBonner negative indices do not necessarily produce an error, it will be converted to `size_t` and allocate a large amount of memory (whether that succeeds or not depends on system details of course) – M.M Dec 23 '16 at 11:55
  • Maybe he meant `new char[4]{'1', '2', '3', '4'};`. – David G Dec 23 '16 at 16:07
5

The syntax for the new expression you used is something like:

identifier = new Type[<expression>];

In the <expression> above, C++ allows any expression whose result is convertible to std::size_t. And for your own expression, you used the comma operator.

<expression> := '1', '2', '3', '4'

which will evaluate every item in the comma list and return the last, which is '4', and that result will be converted to its std::size_t value, probably (52); So, the code is equivalent to:

char* p_c = new char['4'];
WhiZTiM
  • 21,207
  • 4
  • 43
  • 68
2
char *p_c = new char['1', '2', '3', '4'];

is functionally equivalent to:

char *p_c = new char['4'];

because of comma operator. Comma operator evaluates its operant left to right and discards them except the last one (the right most one).

The character literal '4' has value 52 in ASCII (but your system doesn't have to use ASCII and neither is it required by C or C++ standards-- but almost all modern systems do use ASCII).

So, it's as if you used:

char *p_c = new char[52];
Community
  • 1
  • 1
P.P
  • 117,907
  • 20
  • 175
  • 238