Storing variables in memory

If I have a list of global variables like this ...

int a; char b; float c[10]; double d[3]; 

and I have an identical sequence of variables listed inside the class ...

 class test_type { int a; char b; float c[10]; double d[3]; } 

it is guaranteed that the location of all variables in memory is identical. that is, "b" is guaranteed to be stored immediately after "a" both in the list of global tables and in the list of classes?

EDIT: I ask because I wanted to A) copy the data from one to another as a "place of work" and "B"). I wanted to check any differences between them as a place of work. If the answer to the main question is β€œno”, then does anyone have any suggestions on how to get around the problem, it is advisable to leave the existing code as unchanged as possible.

+4
source share
8 answers

No. I don't think the C ++ standard guarantees anything about the memory layout of global variables.

+8
source

Why don't you just use a global object of type test_type instead of defining several global variables?

 class test_type { int a; char b; float c[10]; double d[3]; }; test_type global; 

Now, this is a piece of cake to initialize a new test_type object with a copy of "global".

+6
source

C / C ++ standards do not require storing variables in the order of their declarations. Therefore, compilers can store variables in the order they want. But it is guaranteed that they will be initialized in the order of their declaration. This is true for both global variables and class variables.

Some notes on global variables: in the compilation module they will be initialized in the specified order, but this guarantee does not apply to compilation units.

+1
source

Memory allocation is compiler dependent. Thus, a continuous distribution or an identical distribution of global variables and class variables cannot be guaranteed.

0
source

No, you cannot rely on this. This is for the compiler how to allocate memory for variables.

0
source

No, this is not guaranteed. Depending on what you would like to do, you can use member-pointer operators to make sure that you know the relative address of the member variable.

So in your class:

 class test_type { int a; char b; float c[10]; double d[3]; } void* pa = &test_type::a; void* pb = &test_type::b; void* pc = &test_type::c; void* pd = &test_type::d; void main() { std::cout << "Relative address of member 'a' " << (int)pa << std::endl; std::cout << "Relative address of member 'b' " << (int)pb << std::endl; std::cout << "Relative address of member 'c' " << (int)pc << std::endl; std::cout << "Relative address of member 'd' " << (int)pd << std::endl; } 

As far as I know, it works with msvc and gcc.

0
source

Mick - in order to do what you want (copy / test), you just need to

  • add a constructor to the test_type class, as well as a non-constructor init_from_globals () (whose code copies 4 values ​​one after another, arrays copied in a loop or through memcopy).

  • Add the method "diff_from_globals ()", which once again delimits 4 elements separately

This is a little less efficient / elegant than what you wanted in the implementation, but wrapped in a method, elegance is completely hidden from the rest of the code :)

0
source

I think people are lacking in meaning. It is always dangerous to assume any layout, as this can change from CPU to CPU, compiler to compiler, and even to the degree of optimization.

C ++ will create a default copy assignment function, as if you wrote this:

 class test_type { int a; char b; float c[10]; double d[3]; public: test_type &operator=(const test_type &other) { // copy all of the members one by one. } } 

The only caveat is that the default statement simply copies elements and includes pointers. If you dynamically allocated something that is bound to a pointer, you will have to deal with a new allocation of memory and copying data in a statement.

0
source

All Articles