Let's say I have a header file, armorshop.h, containing a class definition, along with a corresponding .cpp for this header file.
My question is:
Thanks
//scenario 1
//armorshop.h
#ifndef __SFML__armorshop__
#define __SFML__armorshop__
class armorshop : public entity
{
public:
};
int allocatedmemory1;
sf::FloatRect allocatedmemory2;
class armorshop allocatedmemory3;
std::vector<armorshop> allocatedmemory4;
#endif
(question 3 clarification in relation to scenario 1:) If I #include "armorshop.h"
multiple times will this cause an error
//scenario 2
//armorshop.cpp
int allocatedmemory1;
sf::FloatRect allocatedmemory2;
class armorshop allocatedmemory3;
std::vector<armorshop> allocatedmemory4;
- How does allocated memory get freed in c++?
How memory is freed, depends on the implementation, and how the memory was allocated in the first place.
- Will allocatedmemory1, allocatedmemory2, allocatedmemory3, or allocatedmemory4 ever be freed on its own in each of these scenarios?
All of those variables have static storage duration. Objects with static storage duration are deallocated after main
has returned.
Whether global variables were defined in a header file that is included in a source file (scenario 1), or the globals were defined in a source file directly (scenario 2), makes no difference to when the variables are "freed".
- Do variables and ... get freed when they are not being used
Nothing is "freed" based on whether they are used or not.
Do ... classes ... get freed
Classes are not "freed".
If I #include "armorshop.h" multiple times will this cause an error
armorshop.h
defines global variables. Including the header in multiple source files will cause each source file to define those same variables. That violates the one definition rule. You should hardly ever define global variables in a header.
PS. __SFML__armorshop__
is a reserved identifier, because it contains two consecutive underscores. Defining it in your code makes your program ill-formed.
This is a two-part question.
The first part, is that if the globally-scoped objects get declared in a header file in this fashion, and this header file gets included from multiple translation units, this is undefined behavior, and will typically result in duplicate symbol declaration errors during the link phase. A recently-adopted addition to the forthcoming C++17
allows globally-scoped objects to be declared as inline
, permitting them to be declared in multiple translation units, as defined behavior.
For the second part, the globally-scoped objects get declared in a single translation unit, the globally-scoped objects get constructed at some point before main()
is invoked, and they get destroyed, or deallocated, at some point after main()
returns. The relative order in which statically-scoped objects in different translation units get constructed or destroyed is not specified. Only the relative order in which statically-scoped objects in the same translation unit get constructed, or destroyed, is specified.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.