0

I have a simple program :

class Element
{
public:
    Element(void) { m_iVal = 56789;}
    ~Element(void) {};
private :
    int m_iVal;
};  

The variable m_iVal is private, which poses restrictions from a conceptual point of view (inheritance, ...).
However, although private, access to it content is still possible.

enter image description here

Is there a way to prohibit physical access to a varibal (or a heap area) so that any access to this private area will cause a memory access violation.

Landstalker
  • 1,368
  • 8
  • 9
  • 3
    What you are doing is undefined behavior. – Rinat Veliakhmedov Feb 10 '20 at 14:14
  • 1
    `private` is not to make it impossible to access the member, but it is to prevent you from accidentally accessing it. If you still want to access it you can, but why would you? (btw your code accessing it has undefined behavior, and you cannot prevent someone from doing that, though there are also legal ways) – 463035818_is_not_an_ai Feb 10 '20 at 14:16
  • @RinatVeliakhmedov I am not sure this is undefined behavior in C++. At least in C, it is required that the address of the structure to be the same as the address of its first member. – lvella Feb 10 '20 at 14:18
  • @RinatVeliakhmedov I am perfectly aware of this but I wish to prohibit "any search" in an instance of my object by another local third-party program or DLL ... – Landstalker Feb 10 '20 at 14:20
  • @lvella "_At least in C, it is required that the address of the structure to be the same as the address of its first member._" If I remember correctly: in C++, this is correct for POD-structures. All of structures in C are POD, but not all structures in C++ are POD. – Algirdas Preidžius Feb 10 '20 at 14:20
  • no matter how you write your code you cannot prevent others from writing code that has ub – 463035818_is_not_an_ai Feb 10 '20 at 14:21
  • @Landstalker "but I wish to prohibit "any search" in an instance of my object by another local third-party program or DLL " - I'm pretty sure you cannot do that. You just have to write your code such that it behaves correctly (or terminates) in the event someone does that. You can't prevent it. There's no magic "make my code hacker proof" switch you can flip. – Jesper Juhl Feb 10 '20 at 14:24
  • @Landstalker "I wish to prohibit "any search" in an instance of my object by another local third-party program or DLL" I am sure every developer of a pirated software would love that. So far, that has proven very hard to do, but you might try it with Trusted Computing. – lvella Feb 10 '20 at 14:27
  • @idclev463035818 I agree with you. I'm looking for a fair way to "deter" search in data LOCALLY (Relative to my dev environment, not absolutely ...) – Landstalker Feb 10 '20 at 14:28
  • @JesperJuhl; @Ivella : Yes, i agree – Landstalker Feb 10 '20 at 14:29
  • 2
    @Landstalker What you want is a unicorn. You cannot have that. You can have a pony. It's no unicorn but it looks similar. Doesn't do magic though. Sorry. – Jesper Juhl Feb 10 '20 at 14:30
  • @JesperJuhl yes, of course, Thanks. – Landstalker Feb 10 '20 at 14:33
  • "deter"? declaring the variable as `private` is the way to do that. If someone writes code like in your access-the-private example then they just get what they deserve – 463035818_is_not_an_ai Feb 10 '20 at 14:45
  • @idclev463035818 Yes, I will content myself with this – Landstalker Feb 10 '20 at 14:55
  • @idclev463035818 "deter" in the sense that I must prevent access for other modules developed in LOCAL (not by me). It is not a question of preventing access in an absolute way because it is not possible – Landstalker Feb 10 '20 at 14:58
  • Are you looking to hide certain data from debugger? kinda hard coded password ? – TruthSeeker Feb 11 '20 at 13:19
  • @TruthSeeker something like that yes. it is not really passwords but rather sensitive data which is not yet processed by other encryption modules (hash functions, ...) which hide a certain outside visibility. But for the interior (our modules) I try to prohibit physical access because it will avoid me to developpe a new software protection layer internally – Landstalker Feb 11 '20 at 13:29
  • [this](https://stackoverflow.com/a/926192/4139593) , [this](https://stackoverflow.com/q/39483519/4139593) and [this](https://stackoverflow.com/a/25830845/4139593) can help you. – TruthSeeker Feb 11 '20 at 13:43
  • @TruthSeeker thanks :) – Landstalker Feb 11 '20 at 13:43

1 Answers1

5

Access protection; public/protected/private is purely a compile time thing. It exists to prevent simple errors. It it not a run-time protection mechanism.

Once your code has passed the first stage of your compiler, public, protected and private no longer exists at all. And in the generated binary, there's no trace of those what-so-ever.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70