I ask this question to get rid of the confusion that I experienced about the next program. I know that using an array in certain contexts will cause the array to decay to a single pointer to its first element. I have a function that returns this array by pointer (this function is created using new[]). Will the array decay, forcing the pointer to point to only the first element? Here is an example:
new[]
int *foo() { int *t = new int[10]; return t; } int main() { int *p = foo(); }
There is confusion here. I do not know if it points to pthe first element or to the entire array. Therefore, I have the following questions:
p
delete[]
, , . .
, , , .
, . , , delete[]. , . , !
. ( ), . , , ( ) ( ).
. :
, , , , , , .
/ , , delete[], . . , , , "out".
++ , , std::vector std::map, , , " ?" .
std::vector
std::map
(, , )?
t, . . , delete [] .
t
delete []
p ?
delete [] p undefined, ?
, . .
: , , , . , , delete, delete [] . : ! , beter , , , (std::vector, std::array, std::unique_ptr, std::shared_ptr...).
delete
std::array
std::unique_ptr
std::shared_ptr
, [] p !
However, delete [] magically knows how long the array is, sizeof (p) does not work, because the allocator keeps track of the allocated space. However, sizeof is a fixed value generated in compiletime