By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Memory value change?

P: n/a
Hello,

This may sound stupid, but I am not aware if it's possible so don't
laugh :)

See the code:

int n=5;
int* p=&n;
p--;
*p=4;

After we change p can we change the value of the variable in memory.
When I try this it returns an error on both Linux and Windows. Is there
any way to do it?

Dec 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
itportal wrote:
Hello,

This may sound stupid, but I am not aware if it's possible so don't
laugh :)

See the code:

int n=5;
int* p=&n;
p--;
*p=4;

After we change p can we change the value of the variable in memory.
When I try this it returns an error on both Linux and Windows. Is there
any way to do it?


What exactly is 'it'. There is nothing specific that the pointer is pointing
to afterwards, so there is nothing that can be changed though it. Note that
pointer arithmetic is only allowed if the location it previously pointed to
and the new location are part of the same array or one element after it.
Dec 23 '05 #2

P: n/a
itportal wrote:
This may sound stupid, but I am not aware if it's possible so don't
laugh :)

See the code:

int n=5;
int* p=&n;
Here 'p' is initialised to point to the 'n' object.
p--;
After this operation, 'p' does not point to any valid object.
*p=4;
This invokes undefined behaviour.
After we change p can we change the value of the variable in memory.
Only if you restore the proper value of 'p'. You can do it by either

p++; // or ++p;

or

p = &n;

..
When I try this it returns an error on both Linux and Windows. Is there
any way to do it?


To do what?

V
Dec 23 '05 #3

P: n/a
Victor Bazarov wrote:
itportal wrote:
This may sound stupid, but I am not aware if it's possible so don't
laugh :)

See the code:

int n=5;
int* p=&n;

Here 'p' is initialised to point to the 'n' object.
p--;

After this operation, 'p' does not point to any valid object.
*p=4;

This invokes undefined behaviour.
After we change p can we change the value of the variable in memory.

Only if you restore the proper value of 'p'. You can do it by either

p++; // or ++p;

or

p = &n;

.


OK, there is another way to change the value, and it is to index 'p'.
When I try this it returns an error on both Linux and Windows. Is there
any way to do it?

To do what?

V


V
Dec 23 '05 #4

P: n/a
Victor Bazarov wrote:
...
See the code:

int n=5;
int* p=&n;


Here 'p' is initialised to point to the 'n' object.
p--;


After this operation, 'p' does not point to any valid object.


Strictly speaking, this already invokes undefined behavior and brings us to the
point where nothing can be "restored" anymore. Although in practice, of course,
the next operation is more likely to cause any actual trouble.
*p=4;


This invokes undefined behaviour.
After we change p can we change the value of the variable in memory.


Only if you restore the proper value of 'p'. You can do it by either

p++; // or ++p;

or

p = &n;


--
Best regards,
Andrey Tarasevich
Dec 23 '05 #5

P: n/a
I want to change values in memory of other programs, but as I see it's
not possible.

Dec 24 '05 #6

P: n/a
itportal wrote:
I want to change values in memory of other programs, but as I see it's
not possible.


At least not in a standard way, since - concerning the standard - there are
no other programs. Most modern operating system/hardware combinations apply
memory protection so that a process cannot modify - or even see - the
memory of other processes. Basically, each process has its own virtual
memory space.
Usually, there are system-specific ways to share memory between processes.
Dec 24 '05 #7

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:F3*******************@newsread1.mlpsca01.us.t o.verio.net...
itportal wrote:
int n=5;
int* p=&n; Here 'p' is initialised to point to the 'n' object. p--; After this operation, 'p' does not point to any valid object.
Actually, the effect of the statement above is undefined. If it had been
p++, then p would be an off-the-end pointer for n, but as it is, the effect
is undefined.
*p=4; This invokes undefined behaviour.
Well, yes; but the program was already in the realm of undefined behavior.
After we change p can we change the value of the variable in memory.

Only if you restore the proper value of 'p'. You can do it by either p++; // or ++p; or p = &n;


No -- once you've executed p-- above, you're lost.
Dec 24 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.