Search code examples
c++new-operatordynamic-memory-allocationcomma-operator

What does this dynamic allocation do?


Today, I found out that you can write such code in C++ and compile it:

int* ptr = new int(5, 6);

What is the purpose of this? I know of course the dynamic new int(5) thing, but here i'm lost. Any clues?


Solution

  • You are using the comma operator, it evaluates to only one value (the rightmost).

    The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the rightmost expression is considered.

    Source

    The memory address that the pointer is pointing to is initialized with a value of 6 above.