c++ - Is it useful to create pointer(s) on the heap? -
is useful create pointer or array of pointers on heap ? if when , why need ?
for example:
#include <iostream> class box { /* things... */ }; int main(void){ // single pointer on heap box** pbox = new box*(nullptr); *pbox = new box(); // array of pointers on heap box** pboxes = new box*[3]{}; pboxes[0] = new box(); pboxes[1] = new box(); pboxes[2] = new box(); // delete pointers... return 0; }
edit: make question more clear ... know dealing raw pointers not best practice ... want understand pointers , uses important part of c++ hence question (is useful...).
the reasoning behind allocating memory space on heap or on stack not related type of variables allocated, it's related how it's allocated , how it's meant used.
in case, nowadays should avoid new
statements , use "managed" pointers, particularly std::xxx
variants.
Comments
Post a Comment