473,378 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

An exotic pointer...

Hello everyone,

I found something weird happening with a pointer:

Take the following code:

int x=0, n=10, *px=&x, *py=NULL;

printf ("BEFORE: valeur de px = %p et py = %p\n\n", px, py);
*px++=n;
printf ("AFTER : valeur de px = %p et py = %p \n", px, py);

It gives the following result, quite clear, with px becoming px+1 :
BEFORE: valeur de px = 0012FF78 et py = 00000000
AFTER : valeur de px = 0012FF7C et py = 00000000

Everything will stay that way (I mean, normal), should I replace
*px++=n by anything that crosses my mind, like:
*px++; or *++px; (!) or simply px++; or ++px;

But if I do: *++px=n instead, I get the amazing output:
BEFORE: valeur de px = 0012FF78 et py = 00000000
AFTER : valeur de px = 0012FF7C et py = 0000000A <====== (here)

where the value of the py pointer (which has nothing to do with px in
the first place!) has changed.

What is this?

Anyone having any opinion will be quite welcomed, except to say that I
am like the pointer py.
Compiler Microsoft C++ Visual Studio 97 under W2K.

Nov 14 '05 #1
4 1669

"Patrick6" <pa*************@club-internet.fr> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hello everyone,

I found something weird happening with a pointer:

Take the following code:

int x=0, n=10, *px=&x, *py=NULL;

printf ("BEFORE: valeur de px = %p et py = %p\n\n", px, py);
*px++=n;
printf ("AFTER : valeur de px = %p et py = %p \n", px, py);

It gives the following result, quite clear, with px becoming px+1 :
BEFORE: valeur de px = 0012FF78 et py = 00000000
AFTER : valeur de px = 0012FF7C et py = 00000000

Everything will stay that way (I mean, normal), should I replace
*px++=n by anything that crosses my mind, like:
*px++; or *++px; (!) or simply px++; or ++px;

But if I do: *++px=n instead, I get the amazing output:
Not 'amazing' at all. See below.
BEFORE: valeur de px = 0012FF78 et py = 00000000
AFTER : valeur de px = 0012FF7C et py = 0000000A <====== (here)

where the value of the py pointer (which has nothing to do with px in
the first place!) has changed.

What is this?
It's called 'undefined behavior'. After initialization, 'px'
contains a valid address. After incrementing, 'px' no longer
contains a valid address. You then try to dereference this
invalid pointer value. Behavior is undefined.

Anyone having any opinion will be quite welcomed, except to say that I
am like the pointer py.
See above. However, I must ask why you feel the need to increment
the pointer at all. Don't make the error of believing that objects
declared contiguously in source code would result in contiguous storage
of those objects in memory. This is not guaranteed, and is often
not the case.
Compiler Microsoft C++ Visual Studio 97 under W2K.


Doesn't matter the compiler. You've violated the language rules.
In this particular case, it appears that the result was the corruption
of the value of the object 'py'. But according to the language,
*anything* could have happened, from 'seeming to work', to what
you've seen, to a crash, or anything (or nothing) in between.

-Mike
Nov 14 '05 #2
Patrick6 wrote:

I found something weird happening with a pointer:

Take the following code:

int x=0, n=10, *px=&x, *py=NULL;

printf ("BEFORE: valeur de px = %p et py = %p\n\n", px, py);
*px++=n;
printf ("AFTER : valeur de px = %p et py = %p \n", px, py);

It gives the following result, quite clear, with px becoming px+1 :
BEFORE: valeur de px = 0012FF78 et py = 00000000
AFTER : valeur de px = 0012FF7C et py = 00000000
OK.
Everything will stay that way (I mean, normal), should I replace
*px++=n by anything that crosses my mind, like:
*px++; or *++px; (!) or simply px++; or ++px;
Well, this is not exactly the same, since you are modifying the pointer
(operator '++'), dereferencing it (operator '*'), but not assigning
anything to the result of the dereference.

However, doing '*++px' is illegal. The result of '++px' no longer points
to 'x' object. It points to memory right after 'x' object. It is legal
to create such a pointer, but attempts to dereference it lead to
undefined behavior.
But if I do: *++px=n instead, I get the amazing output:
BEFORE: valeur de px = 0012FF78 et py = 00000000
AFTER : valeur de px = 0012FF7C et py = 0000000A <====== (here)
That's what I'm talking about. The result of '++px' points to a location
in memory right after 'x' object. Attempts to dereference this pointer
lead to undefined behavior (you are taking it even on step further - you
are trying to modify the memory pointed by the resultant pointer). Your
"amazing output" is nothing more than one possible manifestation of that
undefined behavior.
where the value of the py pointer (which has nothing to do with px in
the first place!) has changed.


Anything can happen. The program might crash. In this case an unrelated
pointer got modified. Undefined behavior is undefined behavior.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #3
On 13 Dec 2004 17:26:02 -0800, "Patrick6"
<pa*************@club-internet.fr> wrote:
Take the following code:

int x=0, n=10, *px=&x, *py=NULL;
....
Everything will stay that way (I mean, normal), should I replace
*px++=n by anything that crosses my mind, like:
*px++; or *++px; (!) or simply px++; or ++px;
*++px is undefined behaviour. In addition, there is no need to be
incrementing the px pointer anyway, since it only references a single
variable.

But if I do: *++px=n instead, I get the amazing output:
BEFORE: valeur de px = 0012FF78 et py = 00000000
AFTER : valeur de px = 0012FF7C et py = 0000000A <====== (here)


That's the result of undefined behaviour. If you increment a pointer past
the bounds of an array (or in this case, a single variable), it can modify
anything - including variables that appear to be unrelated.
Nov 14 '05 #4
> I found something weird happening with a pointer:

Well...not so wierd really.
int x=0, n=10, *px=&x, *py=NULL;
[...]
But if I do: *++px=n instead, I get the amazing output:
BEFORE: valeur de px = 0012FF78 et py = 00000000
AFTER : valeur de px = 0012FF7C et py = 0000000A <====== (here)

where the value of the py pointer (which has nothing to do with px in
the first place!) has changed.

What is this?


Well let us look at what your variables look like in memory

address value

0012FF74 10 //n
0012FF78 0 //x
0012FF7C 0 //py
0012FF80 0012FF78 //px

When you do *px++ = n, then what happens is first *px = n:

0012FF74 10 //n
0012FF78 10 //x
0012FF7C 0 //py
0012FF80 0012FF78 //px

then px is incremented:

0012FF74 10 //n
0012FF78 10 //x
0012FF7C 0 //py
0012FF80 0012FF7C //px

Getting the result you first saw.
Now if we try the second example, first px is incremented:

0012FF74 10 //n
0012FF78 0 //x
0012FF7C 0 //py
0012FF80 0012FF7C //px

px is moved up to point to the next 32-bit value, which just happens to
be py... Then we set *px = n:

0012FF74 10 //n
0012FF78 0 //x
0012FF7C 10 //py
0012FF80 0012FF7C //px

And there we go.

But don't ever rely on this happening, as the way your variables are
arranged in memory may be up to the compiler and could change.

-Chris

Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
4
by: stj911 | last post by:
http://counterpunch.org/rahni04072007.html Test Tube Zealots: The American Chemical Society Terminates the Membership of Chemists from Iran By DAVID N. RAHNI The American Chemical Society...
1
by: almurph | last post by:
Hi, Hope that you can help me here. I'm trying to write a little windows appliction that takes essentially calculates and displays the VB.NET "ChrW()" code for an exotic character (mainly east...
0
by: denappel | last post by:
Hi, Yesterday I was facing a weird problem using Crystal Reports in VS2008. I have to make print label for supermarket products. They use an exotic lettertype for the price, being MarkerGD....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.