performance - Efficiency of declaring a pointer to a pointer in C++ -


this may sound petty problem in program creating, have recursive function, in declare pointer pointer class 'quadtree'.

i.e. :

void check_tree(quadtree* p_qtree, object* p_obj) {   if (!p_qtree->is_leaf())   {     quadtree** children = p_qtree->get_children();  // <-- i'm referring     (int = 0; < 4; i++)       check_tree(children[i], p_obj);   }   else   {     ...   } } 

does declaring 'quadtree**' inside recursive function decrease efficiency of program because of repeated calls function or effects negligible? better declare outside function following?

quadtree** children;   // <-- move declaration here void check_tree(quadtree* p_qtree, object* p_obj) {   if (!p_qtree->is_leaf())   {     children = p_qtree->get_children();     (int = 0; < 4; i++)       check_tree(children[i], p_obj);   }   else   {     ...   } } 

my initial thought effects of declaring pointer pointer within recursive function negligible since allocates memory pointer rather entire class, i'm not sure. feedback great.

performance wise, there no difference between allocating stack of size n , stack of size m (they both compiles reading register, adding constant value, , storing value same register).

memory-wise, declaring variable inside function, each level of stack use 8 bytes (on 64-bit machine) or 4 bytes (on 32-bit machine). imo, negligible. also, compiler might able optimize away when can done safely.

correctness-wise, allocating memory outside makes function non-reentrant. can problem when use multiple threads.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -