C++: Manually set Memory location

Started by
11 comments, last by Palidine 14 years, 1 month ago
Hi there peeps! I was wondering if it is possible to set a variable location to a specific memory value? I mean something like: int A = 0; int* B = &A //B Points to memory of A int C = 0; *C = B; I know the syntax is not correct, im only curious if this is possible to do, so you might insert a private members' memory location in here to edit the content from outside. Like this structure: emplate<class Type> class Test { public: template<class Type> void Change(Type** _Pointer ) { *_Pointer = &Private } Test() { PointToPrivate = &Private this->Private = "Default"; } void Print() { cout << "Private value: " << this->Private << "\n"; } Type* PointToPrivate; protected: Type Private; }; This allows an external pointer to point to Private, when poiner is being set equal to PointToPrivate; This way you CAN acces the private member from outside. Only thing what is needed is an internal pointer that is public and points to another private variable( and has to be same type offcourse) . But as far as i know, you can not add public members to a class and point to its private members. Also inheritance is not possible, it IS possible to use inheritance IF the pointer needs to point to a protected member though. I hope this is possible some way, even if it IS difficult, i would like to know the answer. Thanks in advance! :)
Advertisement
You want to access private members from outside of class?
This could help:
#define private public
What exactly are you trying to achieve here? Are you working with some existing class that you can't modify, but you need access to one of it's private variables?

If not, either just make that variable public, or provide a function that returns a pointer to your private variable (but then, what's the point in making it private?).

Otherwise, either the class' design is flawed, or you're trying to use it in a way it's not meant to be used. So rather than hacking around the problem, see if you can fix it instead.
Create-ivity - a game development blog Mouseover for more information.
Quote:Original post by bubu LV
You want to access private members from outside of class?
This could help:
#define private public


Thanx bubu LV, this works for that part( pretty awesome solution btw :P )

Quote:Original post by Captain P
What exactly are you trying to achieve here? Are you working with some existing class that you can't modify, but you need access to one of it's private variables?

If not, either just make that variable public, or provide a function that returns a pointer to your private variable (but then, what's the point in making it private?).

Otherwise, either the class' design is flawed, or you're trying to use it in a way it's not meant to be used. So rather than hacking around the problem, see if you can fix it instead.


Actually, i'm trying to use it in a way it is not meant to be used, i'm trying to hack around in my own code, not with my vision of using this in evil ways, but to understand how safe a memory location is/can be. Seeing the fact that redefining the meaning of private to public is possible, i'm questioning myself how 'locking' is possible in a multithreaded program.

Now the only quetion remaining: Is there an option to use some kind of Direct Memory Acces?

[Edited by - dark0fire on March 3, 2010 6:41:33 AM]
Quote:Original post by bubu LV
#define private public
Amazing.
Quote:Thanx bubu LV, this works for that part( pretty awesome solution btw :P )
Even more amazing.

This forum needs to become a pay site like experts exchange, because we shouldn't be giving up quality information like this for free!

Quote: #define private public


If that is not proof of the evilness of a macro then I dont know what is.

If you need to do that then there is something seriously wrong with your design.
Either define a friend class or simply make an accessor method.
Ah, and don't forget to:
#define class struct

so members with default visibility are also visible! :)
Quote:Original post by bubu LV
Ah, and don't forget to:
#define class struct

so members with default visibility are also visible! :)
Um...it looks like the OP is actually taking these suggestions seriously, so if you're just kidding around here (which I assume you are), you might want to clarify that ;)
Quote:Original post by dark0fire
Actually, i'm trying to use it in a way it is not meant to be used, i'm trying to hack around in my own code, not with my vision of using this in evil ways, but to understand how safe a memory location is/can be. Seeing the fact that redefining the meaning of private to public is possible, i'm questioning myself how 'locking' is possible in a multithreaded program.


As you have noticed private does not make anything safe in that way. The private keyword is just there to remind us that we should not access the variable directly. We can figure out intended way of using the class or make the variable accessible if that seems fine.

Locking in multithreaded programming has nothing to do with private variables. Locking is just a way of stopping threads so not two of them are writing to the same variable at the same time.
Quote:Original post by dark0fire
I was wondering if it is possible to set a variable location to a specific memory value?

Of course.

You realize that C was invented for the purpose of porting the Unix operating system to other processors? Without the ability to access memory-mapped hardware registers, OS programming would be pretty pointless.

A pointer in C is a variable that holds a memory address. You may set it to any value, then dereference it. The results of applying a dereference to an arbitrary address is potentially undefined, but that's how a awful lot of low-level stuff is done. For example,
char* uart_interrupt_reg = reinterpret_cast<char*>(0x0000ffe0);if (*uart_interrupt_reg & UART_READ_AVAILABLE_BIT)  ...

You can also use that feature to do things like subvert the C++ access specifiers or constness. C gives you more than enough rope to shoot yourself in the foot with. Go wild.

Successful programmers, of course, know how to use their tools wisely.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement