473,385 Members | 1,275 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,385 software developers and data experts.

Pointers in C

Hi,

I came across a program as follows
main()
{
int x, y ;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", p1-p2);
}

The output of the above program is 1.
Can some one explain me how it is 1.
Thanks in advance.

Feb 21 '07
66 2616
In article <er**********@inews.gazeta.pl>, rafalp <{m**@gazeta.plwrote:
>For other computers, comparing a pointer in one ring to
a pointer in another ring might cause a security violation error at the
hardware level (i.e., before the C implementation can even react to it),
terminating the program with extreme prejudice.
>No, because you're not accessing memory in "another ring". Comparing
pointers is not the same as accessing memory they point to.
It might be that merely comparing or subtracting two addresses in
different rings causes an exception, because there may be no ordering
defined between addresses in different rings. (By the way, C requires
comparing for equality to work.)

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 22 '07 #51
rafalp <{m**@gazeta.plwrites:
Richard Bos wrote:
[...]
>For other computers, comparing a pointer in one ring to
a pointer in another ring might cause a security violation error at the
hardware level (i.e., before the C implementation can even react to it),
terminating the program with extreme prejudice.

No, because you're not accessing memory in "another ring". Comparing
pointers is not the same as accessing memory they point to.
Comparing (using the relational operators <, <=, >, or >=) two
pointers that don't point to the same object invokes undefined
behavior. It's easy to assume that this will always yield some
sensible result, but the standard simply doesn't guarantee it.

If the standard required a flat linear address space, then the
relational operators could be required to yield consistent results;
for any two pointer of the same type, it could guarantee that exactly
one of x < y, x == y, x y is true, and define a total ordering. But
that would make it more difficult to implement C on hardware with more
exotic memory models. The current definition makes it possible to
implement C on hardware on which pointer comparisons don't necessarily
yield consistent results, or even where attempting such a comparison
might crash the program.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 22 '07 #52
On Feb 22, 7:30 am, Flash Gordon <s...@flash-gordon.me.ukwrote:
>
int main(void)
{
int x, y ;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", (int)(p1-p2));
return 0;}

So we have the program (with the undefined behaviour fixed)
For some definitions of 'fixed' !

Feb 22 '07 #53
On Feb 22, 5:59 am, Richard Heathfield <r...@see.sig.invalidwrote:
santosh said:

<snip>
does CHAR_BIT include padding bits?

Since unsigned char may not contain padding bits (see 6.2.6.2), the
answer is no.
You could have CHAR_BIT be 12, and signed char have 8 value
bits and 4 padding bits (for example).

Feb 22 '07 #54

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
CBFalconer said:
>Richard Heathfield wrote:
>>santosh said:

<snip>

does CHAR_BIT include padding bits?

Since unsigned char may not contain padding bits (see 6.2.6.2),
the answer is no.

Minor practical exception - a system with parity memory bits may
actually use CHAR_BIT+1 storage bits.

It is required to behave "as if" each byte has CHAR_BIT bits.
>However, those extra bits
are never visible to the programmer.

In which case it's "as if" they don't exist. So my answer stands.
The Nintendo 64 was one such computer.
There was a ninth bit which, if completely crazed, you could raid with
special assembly instructions
to squeeze some extra memory out of the system.

Feb 22 '07 #55
rafalp wrote:
>
.... snip ...
>
Can you think of a real that checks relate-ness of pointers prior to
subtracting them and performs two different algorithms depending on
whether they are related or not?
It doesn't need to. The operation is undefined.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Feb 23 '07 #56
Old Wolf wrote, On 22/02/07 21:28:
On Feb 22, 7:30 am, Flash Gordon <s...@flash-gordon.me.ukwrote:
>int main(void)
{
int x, y ;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", (int)(p1-p2));
return 0;}

So we have the program (with the undefined behaviour fixed)

For some definitions of 'fixed' !
Quite right, lets say the undefined behaviour other than the specific
piece that the OP was interested in. I agree the pointer subtraction
still invoked undefined behaviour and I should have mentioned that.
--
Flash Gordon
Feb 23 '07 #57
rafalp <{m**@gazeta.plwrote:
Richard Bos wrote:
rafalp <{m**@gazeta.plwrote:
Clark S. Cox III napisaĆ?(a):
rafalp wrote:
santosh wrote:
That holds true only for two given objects o1, o2 of the same type
*and part of an array*. Two arbitrary objects can be placed anywhere
in memory and hence the difference of their addresses , (which invokes
undefined behaviour), need not be any expected value at all. The two
objects in OP's post were two arbitrary objects, hence his expectation
of any sensible output as well as your explanation based on an
assumption that the OP may not know about, are both wrong.
OK. That's what C standard says.
I'm pretty sure that compilers perform the same operations when
subtracting any two pointers, no matter if they are related or not.
*Your* compiler might do that but not every compiler does that. Think of
a segmented architecture with completely separate memory spaces.
Can you think of a real that checks relate-ness of pointers prior to
subtracting them and performs two different algorithms depending on
whether they are related or not?
Who's talking about two different _algorithms_?

Actually santosh is. He said "not every compiler does that" in response
to my "compilers perform the same operations".
That doesn't mean that they don't follow the same algorithm. Even if
your compiled program follows the same algorithm, your OS or even your
memory controller could ensure that the result is different.
For other computers, comparing a pointer in one ring to
a pointer in another ring might cause a security violation error at the
hardware level (i.e., before the C implementation can even react to it),
terminating the program with extreme prejudice.

No, because you're not accessing memory in "another ring".
Accessing memory is irrelevant. Just comparing two pointers to different
security rings using < or could cause a trap. And the Standard allows
it to.

Richard
Feb 23 '07 #58
At about the time of 2/21/2007 11:43 AM, Richard Tobin stated the following:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>>Quite so, but we know that they are adjacent on the OP's system (given
the assumptions I listed). Of course, it's possible that adding the
casts to char * may change that, but I doubt it.
>The assumptions you listed are not sufficient to guarantee that the
objects are adjacent. It's likely that they are, but even then
there's no reason to assume that the result of the pointer subtraction
will be positive rather than negative.

We know from the original post that the result is 1 without the casts
to char *. So unless sizeof(char *) is negative (an interesting idea)
the difference after casting will also be positive.

-- Richard
Not necessarily. Did you see Flash Gordon's post (Message ID:
<an************@news.flash-gordon.me.uk>) when he ran it on a AIX 5.3
machine? It gave -1 as the answer. Two different platforms gives two
different answers, both compiled with gcc.

As most of us here know, undefined behavior is undefined behavior:
anything can happen, any result is possible. It all depends on your
implementation, compiler, and platform.
--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Feb 23 '07 #59
At about the time of 2/20/2007 10:11 PM, Praveen stated the following:
Hi,

I came across a program as follows
main()
{
int x, y ;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", p1-p2);
}

The output of the above program is 1.
Can some one explain me how it is 1.
Thanks in advance.
It's undefined behavior for reasons that others have pointed out.

In this case, x and y are placed ajacent in memory, so the difference is
1 since you are subtracting int pointers. Some people here have had -1
on their systems.

strata:/home/dr2867/c/test 1062 $$$ ->cat ub001.c
#include <stdio.h>

int main(void)
{
int x;
int y;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", p1 - p2);
printf("p1 = %0#x\np2 = %0#x\n", (unsigned int)p1, (unsigned int)p2);

return(0);
}

strata:/home/dr2867/c/test 1063 $$$ ->./compile ub001 ub001.c

Output File: ub001

Input Files:
ub001.c

gcc -W -Wall -Wshadow -Wpointer-arith -Wcast-align -Wstrict-prototypes
-Wnested-externs -Wwrite-strings -Wflo
at-equal -Winline -Wtrigraphs -ansi -std=c89 -pedantic -ggdb3 -o ub001
ub001.c

strata:/home/dr2867/c/test 1064 $$$ ->./ub001
1
p1 = 0xbfbfec34
p2 = 0xbfbfec30
strata:/home/dr2867/c/test 1065 $$$ ->
Look at the values of p1 and p2. They are 4 off, but it returns 1
because sizeof(int) is 4...at least on my system and apparently yours too.


--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Feb 23 '07 #60
In article <GW***************@newssvr25.news.prodigy.net>,
Daniel Rudy <ZGNydWR5QHBhY2JlbGwubmV0wrote:
>We know from the original post that the result is 1 without the casts
to char *. So unless sizeof(char *) is negative (an interesting idea)
the difference after casting will also be positive.
>Not necessarily. Did you see Flash Gordon's post (Message ID:
<an************@news.flash-gordon.me.uk>) when he ran it on a AIX 5.3
machine? It gave -1 as the answer. Two different platforms gives two
different answers, both compiled with gcc.
Read what I wrote. "We know from the original post that the result is
1". The original poster ran the program and that's what he got. I
then said "If the system the OP is using [has various properties]
....". I am describing what the OP's system will produce, not what
some other gcc system will produce.
>As most of us here know, undefined behavior is undefined behavior:
anything can happen, any result is possible. It all depends on your
implementation, compiler, and platform.
That's why I spelt out various assumptions. Given those assumptions,
what I said is true.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 23 '07 #61
Daniel Rudy wrote:
At about the time of 2/20/2007 10:11 PM, Praveen stated the following:
Hi,

I came across a program as follows
main()
{
int x, y ;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", p1-p2);
}

The output of the above program is 1.
Can some one explain me how it is 1.
Thanks in advance.

It's undefined behavior for reasons that others have pointed out.

In this case, x and y are placed ajacent in memory, so the difference is
1 since you are subtracting int pointers. Some people here have had -1
on their systems.

strata:/home/dr2867/c/test 1062 $$$ ->cat ub001.c

#include <stdio.h>

int main(void)
{
int x;
int y;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", p1 - p2);
The ld format specifier might be a safer option. If you know your
implementation supports C99, then the tx or td format specifier would
be appropriate.
printf("p1 = %0#x\np2 = %0#x\n", (unsigned int)p1, (unsigned int)p2);
Why not simply?

printf("p1 = %p\np2 = %p\n", (void *)p1, (void *)p2);

Feb 23 '07 #62
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <GW***************@newssvr25.news.prodigy.net>,
Daniel Rudy <ZGNydWR5QHBhY2JlbGwubmV0wrote:
>>We know from the original post that the result is 1 without the casts
to char *. So unless sizeof(char *) is negative (an interesting idea)
the difference after casting will also be positive.
>>Not necessarily. Did you see Flash Gordon's post (Message ID:
<an************@news.flash-gordon.me.uk>) when he ran it on a AIX 5.3
machine? It gave -1 as the answer. Two different platforms gives two
different answers, both compiled with gcc.

Read what I wrote. "We know from the original post that the result is
1". The original poster ran the program and that's what he got. I
then said "If the system the OP is using [has various properties]
...". I am describing what the OP's system will produce, not what
some other gcc system will produce.
>>As most of us here know, undefined behavior is undefined behavior:
anything can happen, any result is possible. It all depends on your
implementation, compiler, and platform.

That's why I spelt out various assumptions. Given those assumptions,
what I said is true.
I, for one, did not assume that the presented output was part of your
assumptions. I though you were drawing a conclusion based only on
your stated assumptions. It's entirely possible I misread your
article; I'm too lazy to go back and check.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 23 '07 #63
"santosh" <sa*********@gmail.comwrites:
Daniel Rudy wrote:
[...]
>#include <stdio.h>

int main(void)
{
int x;
int y;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", p1 - p2);

The ld format specifier might be a safer option.
"%ld" is really no safer than "%d". You need to convert the ptrdiff_t
result to a known type before printing it:

printf("%ld\n", (long)(p1 - p2));

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 23 '07 #64
At about the time of 2/23/2007 9:34 AM, santosh stated the following:
Daniel Rudy wrote:
>At about the time of 2/20/2007 10:11 PM, Praveen stated the following:
>>Hi,

I came across a program as follows
main()
{
int x, y ;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", p1-p2);
}

The output of the above program is 1.
Can some one explain me how it is 1.
Thanks in advance.
It's undefined behavior for reasons that others have pointed out.

In this case, x and y are placed ajacent in memory, so the difference is
1 since you are subtracting int pointers. Some people here have had -1
on their systems.

strata:/home/dr2867/c/test 1062 $$$ ->cat ub001.c

#include <stdio.h>

int main(void)
{
int x;
int y;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", p1 - p2);

The ld format specifier might be a safer option. If you know your
implementation supports C99, then the tx or td format specifier would
be appropriate.
You snipped my gcc command line, but I use -ansi and -std=c89 for my
language options. Gcc didn't give any warnings about my code, so I left
it as is.
> printf("p1 = %0#x\np2 = %0#x\n", (unsigned int)p1, (unsigned int)p2);

Why not simply?

printf("p1 = %p\np2 = %p\n", (void *)p1, (void *)p2);
Because I was experimenting with it.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Feb 24 '07 #65
Daniel Rudy wrote, On 24/02/07 12:43:
At about the time of 2/23/2007 9:34 AM, santosh stated the following:
>Daniel Rudy wrote:
>>At about the time of 2/20/2007 10:11 PM, Praveen stated the following:
Hi,

I came across a program as follows
main()
{
int x, y ;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", p1-p2);
}

The output of the above program is 1.
Can some one explain me how it is 1.
Thanks in advance.

It's undefined behavior for reasons that others have pointed out.

In this case, x and y are placed ajacent in memory, so the difference is
1 since you are subtracting int pointers. Some people here have had -1
on their systems.

strata:/home/dr2867/c/test 1062 $$$ ->cat ub001.c

#include <stdio.h>

int main(void)
{
int x;
int y;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", p1 - p2);
The ld format specifier might be a safer option. If you know your
implementation supports C99, then the tx or td format specifier would
be appropriate.

You snipped my gcc command line, but I use -ansi and -std=c89 for my
language options. Gcc didn't give any warnings about my code, so I left
it as is.
The compiler is not required to warn about miss-matches between format
specifiers and the corresponding options, with gcc I believe there is an
extra switch that will make it diagnose simple cases, but it cannot test
all possibilities. Also, the type might happen to be int, it just is not
guaranteed.
>> printf("p1 = %0#x\np2 = %0#x\n", (unsigned int)p1, (unsigned int)p2);
Why not simply?

printf("p1 = %p\np2 = %p\n", (void *)p1, (void *)p2);

Because I was experimenting with it.
As long as there is a good reason ;-)
Using unsigned long would have been closer to portable, since on some 64
bit systems pointers are 64 bit but int and unsigned int are only 32
bit. %p as suggested above is of course always suitable.
--
Flash Gordon
Feb 24 '07 #66
At about the time of 2/23/2007 9:05 AM, Richard Tobin stated the following:
In article <GW***************@newssvr25.news.prodigy.net>,
Daniel Rudy <ZGNydWR5QHBhY2JlbGwubmV0wrote:
>>We know from the original post that the result is 1 without the casts
to char *. So unless sizeof(char *) is negative (an interesting idea)
the difference after casting will also be positive.
>Not necessarily. Did you see Flash Gordon's post (Message ID:
<an************@news.flash-gordon.me.uk>) when he ran it on a AIX 5.3
machine? It gave -1 as the answer. Two different platforms gives two
different answers, both compiled with gcc.

Read what I wrote. "We know from the original post that the result is
1". The original poster ran the program and that's what he got. I
then said "If the system the OP is using [has various properties]
...". I am describing what the OP's system will produce, not what
some other gcc system will produce.
Sorry, I didn't catch it until after I hit the send button.
>As most of us here know, undefined behavior is undefined behavior:
anything can happen, any result is possible. It all depends on your
implementation, compiler, and platform.

That's why I spelt out various assumptions. Given those assumptions,
what I said is true.

-- Richard


--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Feb 26 '07 #67

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

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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.