Sign in to follow this  

C++: Manually set Memory location

This topic is 2845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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! :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ;)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by jonathanjansson
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.


Thank you for clearing that up :), i was thinking that locking in MT prevented IO to that var location.

Bregma, Thanks for your reply, this is really usefull information, i could not find it in any books so far.

Share this post


Link to post
Share on other sites
You may find the Guru of the week archive interesting to read. In this case, read number 76.

Conclusion: C++' access control mechanism is there to help you. Evil hacks like defining private as public are only making the code more confusing - you're essentially lying to yourself.

Share this post


Link to post
Share on other sites
public, protected, private are compile-time only words. They prevent you from doing things directly in your code at compile time but offer zero runtime protection of your memory or anything else like that.

-me

Share this post


Link to post
Share on other sites

This topic is 2845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this