1.

Intx [] and X = newwhat is the diffrence for betweenint [10]​

Answer»

Answer:

The difference is that the FIRST performs no allocation. It cannot be known if the entity ‘x’ exists in the stack, heap, or in the static data segment. All that can be known is that the pointer declaration did not allocate the entity ‘x’ and that for any sane use of raw pointers in modern C++ this should imply that it is not this pointer variable’s job to free the data pointed to (barring rare if-any EXCEPTION). The second allocates a new integer to the heap however. Without cleanup, this will RESULT in memory leak (or unintended object retention).

This is a very notable difference. Where for EXAMPLE this CODE:

void someFunc(int* ptr);

int foo() {

int x;

while (1) {

int* p = &x;

*p = 0;

someFunc(p);

if (*p) {

return *p

}

}

return 0;

}



Discussion

No Comment Found