Programming Puzzle

J

Julie

Dan said:
In said:
Julie said:
Irrwahn Grausewitz wrote:
Irrwahn Grausewitz wrote:
What would you call p and *p, respectively?

int main(void)
{
int a = 1;
int *p = &a;
*p = 2;
return 0;
}

I call 'p' a "pointer [to an int]" and '*p' "p dereferenced" or "a pointer [to
an int] dereferenced".

So you think the term "variable" is applicable to neither p nor *p?

I'd call p a variable, but not *p.

I agree with you here.

How would you call *p, though? It has the semantics of a variable,
doesn't it?
I beg to differ, I'd rather say that p points to a variable.

That's something too subtle for Julie to understand. She seems to be
blind to the semantic difference between p (that can point to a variable)
and *p (that *is* the variable pointed to by p).

Dan -- your condescending attitude benefits no one in this newsgroup, and only
selfishly serves you.

I was speaking in general terms, not specifically. In the case of the code
posted, *p does point to a variable a, but only in this specific case. In
general terms, a pointer x *may* point to a variable.

Here are some examples of pointers that do *not* point to variables:

int * x = NULL;

const int b = 123;
const int * w = &b;

int * r = &printer_port_out;
 
J

Jonathan Turkanis

Julie said:
Dan -- your condescending attitude benefits no one in this newsgroup, and only
selfishly serves you.

It doesn't serve him very well. ;-)

Jonathan
 
I

Ioannis Vranos

Dan said:
That's something too subtle for Julie to understand. She seems to be
blind to the semantic difference between p (that can point to a variable)
and *p (that *is* the variable pointed to by p).


Well, you and Julie actually use the term variable in the place of the
term object.


As the C++ standard says:

"A variable is introduced by the declaration of an object. The
variable’s name denotes the object."

"An object is a region of storage. [Note: A function is not an object,
regardless of whether or not it occupies storage in the way that objects
do. ] An object is created by a definition (3.1), by a new-expression
(5.3.4) or by the implementation (12.2) when needed."


So in terminology, a variable can only denote one object, which is
always different from another object.


The original question becomes "what if we pass the same object to the
swap function?".


If you do not use the correct terminology, you will never reach a
conclusion, since both of you use your own terminologies.


----

A pointer is a variable that denotes an object (which stores addresses).

A dereferenced pointer is not a variable (since it is not introduced by
the declaration of an object), but represents an object.


As I said, the original question becomes: "what if we pass the same
object to the swap function?".



It would be nice if you tried to communicate on this basis.






Regards,

Ioannis Vranos,

Language Lawyer :)
 
P

pete

Julie said:
Dan -- your condescending attitude
benefits no one in this newsgroup, and only
selfishly serves you.

I was speaking in general terms, not specifically.
In the case of the code posted,
*p does point to a variable a,

No it doesn't.
*p is an object of type int. It doesn't point anywhere.
 
I

Irrwahn Grausewitz

In said:
Julie said:
Irrwahn Grausewitz wrote:
Irrwahn Grausewitz wrote:
What would you call p and *p, respectively?

int main(void)
{
int a = 1;
int *p = &a;
*p = 2;
return 0;
}

I call 'p' a "pointer [to an int]" and '*p' "p dereferenced" or "a pointer [to
an int] dereferenced".

So you think the term "variable" is applicable to neither p nor *p?

I'd call p a variable, but not *p.

I agree with you here.

How would you call *p, though? It has the semantics of a variable,
doesn't it?

But only to some degree, and only in this special case, where p
happens to point to an object created by declaration of a variable,
that coincidentally has the same type p is a pointer to. See also
my reply in the other (non-crossposted) sub-thread, as well as
Ioannis' reply <[email protected]>, with which I
wholeheartedly agree.

<snip>
 
M

Michael

Please describe (in code) a situation where two variables share the same memory
location.
Dan you need to try stuff for yourself

#include <stdio.h>

int main(void){
int x=10, *p;
p=&x;
printf("%p, %p\n",(void *)p,(void *)&x);
return 0;
}
 
J

Julie

Michael said:
Dan you need to try stuff for yourself

#include <stdio.h>

int main(void){
int x=10, *p;
p=&x;
printf("%p, %p\n",(void *)p,(void *)&x);
return 0;
}

You need to think a little harder:

You have two variables: x and p. Are you saying that &x == &p? I sure hope
not.

Try again.
 
K

Keith Thompson

Julie said:
You need to think a little harder:

You have two variables: x and p. Are you saying that &x == &p? I sure hope
not.

Try again.

No, of course nobody is saying htat &x == &p.

x and p are undeniably variables. The question is whether *p is also
a variable. If it is, the above program demonstrates a situation
where two variables share the same memory location; if it isn't, it
doesn't.

*p is certainly an object. The question of whether it's also a
"variable" is, in my opinion, a profoundly uninteresting one. The C
standard doesn't define "variable" as a technical term. It uses it
(informally) in a few places. It happens that it uses it only to refer
to declared objects, but we can't really infer anything from that.
 
J

Julie

Keith said:
No, of course nobody is saying htat &x == &p.

x and p are undeniably variables. The question is whether *p is also
a variable. If it is, the above program demonstrates a situation
where two variables share the same memory location; if it isn't, it
doesn't.

*p is certainly an object. The question of whether it's also a
"variable" is, in my opinion, a profoundly uninteresting one. The C
standard doesn't define "variable" as a technical term. It uses it
(informally) in a few places. It happens that it uses it only to refer
to declared objects, but we can't really infer anything from that.

*p _may_ be an object. Consider the following:

int *p = NULL;

*p is definitely not an object in this case.

Yes, profoundly uninteresting, but somewhat worthy of senseless never-ending
blather, of which I have been a major (unapologetic) contributor.

Since we are in comp.lang.c* newsgroups, the discussions are not strictly
limited to what the standard does/doesn't define -- such restricted topics are
discussed in comp.std.c*.

Using unions it is possible to declare a variable that shares the same
address. Dereferenced pointers (or references in C++) to a variable are at
most an alias to a variable, but not a variable in terms that I consider
well-defined. Others may (and obviously do, hopefully an informed view)
consider the term 'variable' differently.
 
K

Keith Thompson

Julie said:
*p _may_ be an object. Consider the following:

int *p = NULL;

*p is definitely not an object in this case.

Right, but in the example we've been talking about, p has been
assigned the value &x (where x is a declared int object).
Yes, profoundly uninteresting, but somewhat worthy of senseless never-ending
blather, of which I have been a major (unapologetic) contributor.

As have I from time to time.
Since we are in comp.lang.c* newsgroups, the discussions are not
strictly limited to what the standard does/doesn't define -- such
restricted topics are discussed in comp.std.c*.

Actually, comp.lang.c tends to be about the language as defined by the
standard, whereas comp.std.c is about the standard itself. (I just
noticed that this is cross-posted to comp.lang.c++, which is almost
never a good idea.)

[...]
 
D

Dan Pop

In said:
Oohhhh.. you're arguing with Dan....

Nope, he isn't, but you're way too stupid to realise that. Clue: he was
replying to a request made by Julie and incorrectly mentioning my name.

Dan
 
A

Alex Monjushko

You need to think a little harder:
You have two variables: x and p. Are you saying that &x == &p? I sure hope
not.

That is not what he is saying. You are failing to grasp that *p is a
variable. Perhaps this quote from page 45 of K&R2 will enlighten you:

"...The increment and decrement operators can only be applied
to variables..."

This, in my mind, would mean that *p can be considered a variable, at
least part of the time. Technically, of course, *p can hardly be
considered a variable when p does not contain the address of a valid
object.
 
H

Herb Sutter

I haven't read this thread, but:
You have two variables: x and p. Are you saying that &x == &p? I sure hope
not.

Of course, you can easily construct that sort of aliasing:

int a, &b = a;
assert( &a == &b );

I suspect the point of the thread was something else.

Also, FWIW, optimizers routinely fold objects like a and b below (i.e.,
they can occupy the same memory address) because the optimizer can prove
that a conforming program can't tell because their uses don't overlap:

#include <stdio.h>

int main() {
int a = 10;
printf( "%d", a );
int b = 20;
printf( "%d", b );
}

Herb

---
Herb Sutter (www.gotw.ca)

Convener, ISO WG21 (C++ standards committee) (www.gotw.ca/iso)
Contributing editor, C/C++ Users Journal (www.gotw.ca/cuj)
Visual C++ architect, Microsoft (www.gotw.ca/microsoft)
 
K

Keith Thompson

Herb Sutter said:
Also, FWIW, optimizers routinely fold objects like a and b below (i.e.,
they can occupy the same memory address) because the optimizer can prove
that a conforming program can't tell because their uses don't overlap:

#include <stdio.h>

int main() {
int a = 10;
printf( "%d", a );
int b = 20;
printf( "%d", b );
}

To expand on that point a bit, an optimizer can fold a and b because
it can prove that *this particular* program can't tell the difference
(and it's allowed to assume, for purposes of its proof, that the
program doesn't exhibit undefined behavior).

A conforming program could compare the addresses of a and b and expect
them to be different, but that would be a different program, and one
in which the optimizer wouldn't be allowed to fold a and b.

Actually, I'm making an assumption that I'm not 100% certain is
correct. Can a conforming implementation fold a and b if their uses
don't overlap, but the program examines their addresses? In other
words, the following program:

#include <stdio.h>
int main(void)
{
int a, b;

a = 10;
printf("a = %d, ", a);

b = 20;
printf("b = %d, ", b);

printf("addresses are %s\n", &a == &b ? "equal" : "unequal");

return 0;
}

should normally print

a = 10, b = 20, addresses are unequal

if a and b are not folded. If it prints

a = 10, b = 20, addresses are equal

does it imply that the compiler is non-conforming?

This is cross-posted to comp.lang.c and comp.lang.c++. If you're
going to reply based on either language standard, please follow up to
the appropriate group.
 
I

Ioannis Vranos

Keith said:


Fellows we got in the same stuff again. Actually the wrong side of the
subject has been erased. At first cross-posting in clc++ and clc has
nothing useful to provide. They are separate languages. Regarding C++ as
I had said in some messages earlier in the thread:



The C++ standard says:

"A variable is introduced by the declaration of an object. The
variable’s name denotes the object."

"An object is a region of storage. [Note: A function is not an object,
regardless of whether or not it occupies storage in the way that objects
do. ] An object is created by a definition (3.1), by a new-expression
(5.3.4) or by the implementation (12.2) when needed."


So in terminology, a variable can only denote one object, which is
always different from another object.


The original question becomes "what if we pass the same object to the
swap function?".


If you do not use the correct terminology, you will never reach a
conclusion, since both of you use your own terminologies.


----

A pointer is a variable that denotes an object (which stores addresses).

A dereferenced pointer is not a variable (since it is not introduced by
the declaration of an object), but represents an object.


As I said, the original question becomes: "what if we pass the same
object to the swap function?".



It would be nice if you tried to communicate on this basis.






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
C

Chris Torek

... Can a conforming implementation fold a and b if their uses
don't overlap, but the program examines their addresses? In other
words, the following program:

#include <stdio.h>
int main(void)
{
int a, b;

a = 10;
printf("a = %d, ", a);

b = 20;
printf("b = %d, ", b);

printf("addresses are %s\n", &a == &b ? "equal" : "unequal");

return 0;
}

should normally print

a = 10, b = 20, addresses are unequal

if a and b are not folded. If it prints

a = 10, b = 20, addresses are equal

does it imply that the compiler is non-conforming?

In C at least, yes it does. (I would be quite surprised if the
answer changes for C++.) Note that the compiler *can* still combine
the underlying storage space for "a" and "b", as long as it *also*
pretends that &a != &b even though then &a == &b. For instance,
suppose the compiler first tries to optimize the printf() call.
At this time, "a" and "b" are still separate entities in the
compiler's internal representation (e.g., separate RTL pseudo-registers
inside GCC). Thus the result of &a==&b is a known constant 0 (in
C). The next step is to replace:

printf("addresses are %s\n", 0 ? "equal" : "unequal");

with:

printf("addresses are %s\n", "unequal");

and perhaps even optimize this into:

puts("addresses are unequal"); /* note, puts() adds the \n */

Now that this has been done, there is no longer any occurrence of
&a or &b, so now the entities "a" and "b" can be folded together
to occupy the same hard-register-or-stack-slot. So now &a==&b even
though the code that prints whether &a==&b says they are not!
(Of course, if they are in a "hard register" they may not have an
address at all; and in the case of GCC, at least, constant propagation
would result in their removal entirely, at this point.)

If you were to replace the above printf() call with:

printf("addresses are %p and %p\n", (void *)&a, (void *)&b);

the system would have to print two different numbers, in general,
and this would probably preclude the sort of folding described
here.
 
J

Julie

Ioannis said:
Keith said:

Fellows we got in the same stuff again. Actually the wrong side of the
subject has been erased. At first cross-posting in clc++ and clc has
nothing useful to provide. They are separate languages. Regarding C++ as
I had said in some messages earlier in the thread:

The C++ standard says:

Fortunately, comp.lang.c* doesn't have to restrict itself to just specifically
what is contained/defined within the standard. Such limited discussions are
appropriate in comp.std.c*; here we can talk about the language, as defined by
the standard.
"A variable is introduced by the declaration of an object. The
variable’s name denotes the object."

Fine -- but what that is saying is that when you create an object, you also
introduce a variable. The opposite is not necessarily true: introducing a
variable does *NOT* imply the declaration of an object.

If that is the extent of what is discussed in the standard, then the short of
it is that the standard does not define what a variable is.
"An object is a region of storage. [Note: A function is not an object,
regardless of whether or not it occupies storage in the way that objects
do. ] An object is created by a definition (3.1), by a new-expression
(5.3.4) or by the implementation (12.2) when needed."

Seems like it isn't complete to me. Either the other ways (C-style?) to
instanciate an 'object' are either implied or just plain left out. Consider
the differences between:

int i; // created by a definition

int * j = new int; // by a new-expression

extern int k; // by the implementation

int * l = (int *)malloc(sizeof(int)); // what about me?
 
I

Ioannis Vranos

Julie said:
Fine -- but what that is saying is that when you create an object, you also
introduce a variable. The opposite is not necessarily true: introducing a
variable does *NOT* imply the declaration of an object.



No it says that you declare an object by using a variable. So when you
"declare" a variable in essence you declare an object.


If that is the extent of what is discussed in the standard, then the short of
it is that the standard does not define what a variable is.



It does. A variable (its name) denotes an object.


"An object is a region of storage. [Note: A function is not an object,
regardless of whether or not it occupies storage in the way that objects
do. ] An object is created by a definition (3.1), by a new-expression
(5.3.4) or by the implementation (12.2) when needed."


Seems like it isn't complete to me. Either the other ways (C-style?) to
instanciate an 'object' are either implied or just plain left out. Consider
the differences between:

int i; // created by a definition

int * j = new int; // by a new-expression

extern int k; // by the implementation


Actually defined in another compilation unit.


int * l = (int *)malloc(sizeof(int)); // what about me?


In the above cases we have:

int i;

i is a variable that denotes an object.


int *j=new int;


j is a variable that denotes an int * object, that is the pointer itself.

The new int that is created in the free store is another object, not
denoted by a variable, since it is not created by a variable declaration.

extern int k; means that it is defined elsewhere (in general) and the
"int i" case applies for the definition.


The malloc() situation is the same with the new int situation.






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top