Suppose somewhere, somewhere in the program (not at the beginning), we have the following code:
int a = <некое значение>;
But up to this place, let's say, in our program there were already several hundred other variables, and the program addressed the dynamic memory many times. Is it possible that the system runs out of memory, and the above code is brought to the development of an unhandled exception and the program crash? Or, before this, new [] already has time to call std :: bad_alloc, and such a situation is impossible in principle? In other words, is there a guarantee that there are no exceptions in this case?
In fact, the int a = some_val instruction is simply translated into an assembler command like mov eax, ds [index]; those. The idea is that there should be an exception and should not, the only question is whether the data segment is fully reserved before the program starts for all variables, or whether the acquisition of resources occurs dynamically during the program, as in the case of dynamic memory.
It turns out that the more variable names we have, the more indices in the program that are accessed, and the greater must be the amount of the reserved data segment.
The truth still remains the question of how the data segment is requested - either it represents a certain area in the program code (data section), or else the size for the data segment and its size will be obtained from the system heap when the program is started.
int a = some_val
there is astrong nothrow guarantee
. This is intuitively understandable, and I think that it is possible to offer some rigorous justification (although, probably, not too trivial). - What happens to thedata segment
should not worry you at all during development - it makes sense to operate with higher-level guarantees and contracts. - Costantino Rupert