And with what function you can find out how much memory is allocated for a variable in C ++?

  • Comments are not intended for extended discussion; conversation moved to chat . Please transfer all relevant details to the question or to the answer (s). - Nick Volynkin

1 answer 1

There is no such function.

A type has a size that can be recognized with sizeof .
However, the size of the actual allocated memory may differ:

  • when selecting "in the heap" (with the help of new ), most likely, more will be allocated, since heap can align its blocks, for example 16 bytes.
  • when allocated on the stack, the size can be either larger, due to the alignment of stack frames, or smaller - the compiler can put two unrelated variables in one memory location.
  • with static variable allocation, the same thing can happen, plus additional objects can be created to ensure thread safety.
  • It can be said that the sum of sizeofs is not equal to the allocated memory. - avp
  • The heap additionally still allocates a few bytes for storing the size of the selected area (in most implementations) + additional service information for the heap manager. - Vladimir Gamalyan
  • Worse, if there is a pointer to another object in the class, it is not clear whether this other object is part of the original object or not, and whether its memory should also be considered. - VladD