'C++ static storage pointer to dynamic storage

If I have an automatically allocated pointer in static storage

int* p[2];

and I do:

int i;
p[0] = &i;
p[1] = new int;

This gives me an array of automatically allocated pointers in static storage to an int in static storage and an unnamed int in dynamic storage.

Is this correct, good practice and do I have to delete p[1] since it points to an address in dynamic storage?



Solution 1:[1]

If you new something you have to delete it. Always.

Beyond that, yes, you can store pointers in the same array that point to data with different kinds of lifetimes. Whether this is a good idea depends on what you are doing; although, most likely it is not.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jwezorek