If I make objects inside a method in c++, will they get automatically destroyed when the function returns, or will it continue to eat memory? -


void player::draw(sf::rendertarget& target, sf::renderstates states) const {      sf::circleshape c0;     c0.setfillcolor(sf::color::red);     c0.setpointcount(4);     c0.setradius(1);     c0.setposition(sf::vector2f(point[0].x, point[0].y));      sf::circleshape c1;     c1.setfillcolor(sf::color::red);     c1.setpointcount(4);     c1.setradius(1);     c1.setposition(sf::vector2f(point[1].x, point[1].y));      target.draw(c0);     target.draw(c1); } 

i learning c++. can see making circleshape objects inside draw method runs @ 60 times in second. read online objects in c++ stored in heap memory therefore require manual deallocation. objects c0 , c1 destroyed , memory frees when draw method returns, or continue eat heap memory ?

the code wrote creates temporary objects on stack, destroyed when method exits.

in general, when variables go out of scope, content no longer available.

if allocate memory using "new" keyword, objects kept in memory, pointer lost when variable referencing them goes out of scope.

edit:

actually, "temporary objects" feature of c++ language, can cause headaches if not used correctly. read more here: temporary objects - when created, how recognise them in code?

the objects create local variables inside function automatically allocated , destroyed, used word "temporary" describe behaviour: discarded when function exits.


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -