开发者

Declaring vector as global variable in C++

开发者 https://www.devze.com 2023-04-07 10:25 出处:网络
Is it a good practice to declare a vector as global in C++? This is what I did. #include <vector>

Is it a good practice to declare a vector as global in C++?

This is what I did.

#include <vector>
std::vector<int> vec;

My program compiles successfully, but I am not sure whether this could lead to a runtime error under certain circumstances. According to my understanding, the memory for a global variable will be allocated at compile time, and the compiler may reserve a limited amount of memory to which this vector can expan开发者_开发技巧d. Upon hitting this limit, what is being written can eat into the memory used by another variable.

Please advise.


My program compiles successfully, but I am not sure whether this could lead to a runtime error under certain circumstances.

This is safe to do; the storage for the vec variable will be allocated statically and its default constructor will be called at some point (exactly when within the context of your entire program is not strictly defined, as order of initialization across translation units is not strictly defined).

and the compiler may reserve a limited amount of memory to which this vector can expand. Upon hitting this limit, what is being written can eat into the memory used by another variable.

The vector itself allocates its storage on the heap, so there will be no limitations imposed upon its expansion that would be different if you instantiated the vector as a local variable: you're basically going to be limited by the amount of memory you can contiguously allocate at the points in time the vector needs to reallocate its internal storage.

All of that said, while this is safe to do so, it isn't necessarily good practice; it falls into the domain of every other global variable or globally-accessible bit of storage, which can be a bit of a contentious subject. Generally, I would advise that it is preferable to avoid global variable as a rule. While it may be acceptable in some cases, the global access runs counter to your ability to control the access to the variable and enforce invariants on it and the state it controls or implies. This can lead to difficult-to-maintain systems as a codebase scales because those access paths are not clearly spelled out.


According to my understanding, the memory for a global variable will be allocated at compile time, and the compiler may reserve a limited amount of memory to which this vector can expand.

This is wrong understanding.

Memory is not allocated at compile time. Memory is allocated at the program startup, and then during the program, depending on the storage types of variables. And when the program shutdowns, all the memory used by it returns to the OS, no matter what.

Upon hitting this limit, what is being written can eat into the memory used by another variable.

No. An object of std::vector<int> can never eat memory used by another variable(s).

Now coming back to your main question,

Is it a good practice to declare a vector as global in C++?

No. Avoid global variables, irrespective of their types.


Only the space for the vector metadata will be allocated in the area for global variables. The vector contents will still be dynamically allocated (constructors and destructors run normally for global variables).

It's the same situation for an automatic vector variable, like:

int main(void)
{
    std::vector<int> v;
    return 0;
}

There's a limit to the stack space available for automatic variables, but the vector contents won't use this space, only a handful of pointers and counters.


Global variables in general are bad practice. They won't "eat" into memory of another variable as you say, however, it's very easy for you as the programmer to screw something up. For example, everything in the program has access to this vector and so everything can access and modify it. You may or may not want this, but more than likely, you don't want this.

As for memory allocation, objects added to the vector are still added on runtime (because they don't exist on compile time!). Memory is also never allocated at compile time. Your program has the signature in it to allocate this memory at RUN time. Think about that...if the program allocated memory at compile time, you wouldn't really be able to run them on other machines would you? The memory would be allocated on YOUR machine, but not on other machines. Hence, memory must be allocated on run time.


Your program doesn't allocate anything during compile-time - I think you mean run-time.

Vector allocates what it holds on the heap (vec.size() * sizeof(/* what you are holding*/)) - it holds sizeof(std::vector<>) on where it is allocated. Global variables may be stored anywhere, it depends on the implementation.


Well, vec is a global variable, so the memory for it is presumably allocated from the data segment. However, the memory for the contents of vec depends on the allocator. By default, I think that the memory for the contents is allocated from the heap.

0

精彩评论

暂无评论...
验证码 换一张
取 消