C ++ object in memory

Is there a standard for storing C ++ objects in memory? I want to set the char * pointer to a specific address in memory so that I can read the variables of some objects directly from the byte of memory by byte. When I use Dev C ++, the variables are stored one by one right in the memory address of the object in the order in which they were defined. Now, can it be different when using another compiler (for example, the variables are in a different order or somewhere else)? Thanks in advance.: -)

+3
source share
10 answers

Variables cannot be in a different order as far as I know. However, there may be a different number of additions between members. I also think that all bets are disabled with virtual classes, and different implementations of user types (for example, std :: string) can be completely different between libraries (or even build options).

This seems to be a very suspicious thing. What do you need for: access to private members?

+3
source

I believe that the layout of objects in memory is an implementation, not an order, a must, but an amount of space. In particular, you are likely to encounter problems with alignment of bytes and so on, especially on different platforms.

Can you give us some details of what you are trying to do?

+3
source

, : P. , ++ , //.

, . , "#pragma pack" ... - , . , POD.

+1

++ (public, protected ..). . :

struct A {
    int a;
    short b;
    char c;
};

struct B {
    int a;
public:
    short b;
protected:
    char c;
};

A a, b, c. B , . , , , "" .

+1

, .

0

, , , . , , - .

/ , . gcc __attribute__((__packed__)) msvc #pragma pack.

0

, , , , , - , , .

, , struct, , , , .

, , - , . (, , , )

0

, TCP/IP-. , , , , . , , , !

0

, ,

0

Source: https://habr.com/ru/post/1738549/


All Articles