How does the compiler control protection of variables in memory? Is there a tag bit associated with private variables inside the memory? How does it work?
If you mean private
members of instances, then there's no protection whatsoever at run-time. All protection takes place at compile-time and you can always get at the private members of a class if you know how they are laid out in memory. That requires knowledge of the platform and the compiler, and in some cases may even depend on compiler settings such as the optimization level.
E.g., on my Linux/x86-64 w/GCC 4.6, the following program prints exactly what you expect. It is by no means portable and might print unexpected things on exotic compilers, but even those compilers will have their own specific ways to get to the private members.
#include <iostream>
class FourChars {
private:
char a, b, c, d;
public:
FourChars(char a_, char b_, char c_, char d_)
: a(a_), b(b_), c(c_), d(d_)
{
}
};
int main()
{
FourChars fc('h', 'a', 'c', 'k');
char const *p = static_cast<char const *>(static_cast<const void *>(&fc));
std::cout << p[0] << p[1] << p[2] << p[3] << std::endl;
}
(The complicated cast is there because void*
is the only type that any pointer can be cast to. The void*
can then be cast to char*
without invoking the strict aliasing rule. It might be possible with a single reinterpret_cast
as well -- in practice, I never play this kind of dirty tricks, so I'm not too familiar with how to do them in the quickest way :)
FourChars
must have at least size four, the result of printing it's first four bytes has implementation-dependent, but not undefined behavior. –
Achaemenid reinterpret_cast<>
? –
Balboa reinterpret_cast
is perhaps not a problem, accessing the pointer is. –
Balboa void*
, char*
or unsigned char*
. If you couldn't, you could never implement std::memcpy
and friends. (Other casts may cause aliasing problems.) –
Achaemenid offset_of
, the layout may be fixed by this only fact. But in case of absence of valid access to the members, isn't the compiler free to relocate or even remove them? –
Balboa reinterpret_cast
. –
Balboa reinterpret_cast
by a static_cast
via const void*
. –
Achaemenid reinterpret_cast
. –
Hayward private
members (quite relevant to the Q) and the standard was ratified well over a year ago so you should get the final N3290 and dispose of the draft. –
Schatz public
, it may aswell be private
or protected
. See this question. –
Lesotho It's the compiler's job to see that some members are private and disallow you from using them. They aren't any much different from other members after compilation.
There is however an important aspect, in that data members aren't required to be laid out in memory in the order in which they appear in the class definition, but they are required to for variables with the same access level.
(&a)[1]
is not necessarily b
, even if it is granted that &b > &a
). But once the compiler is given and the compiling options defined, the behavior does not depend on the execution. If it works will always work, if it doesn't ... will never. Is fact, it is non-portable code. –
Pernickety © 2022 - 2024 — McMap. All rights reserved.