int to int* conversion...

P

praveenraj1987

#include<iostream>
using namespace std;

int main()
{
int *p;
p=(int*)10;
cout<<(int)p;
}

is this program valid..
no read/write is being made at that location..
pls be specific to this program itself..
 
P

praveenraj1987

You're looking for comp.lang.c++.

When you post there, try and ask a question!

#include<stdio.h>
int main()
{
int *p;
p=(int*)10;
printf("%d",(int)p);
}

does this program is in C now.. :D
 
P

praveenraj1987

(e-mail address removed) said:






Well, that's written in C, at least. What is your question about it?
Is it a valid program? Yes, it is. Is it guaranteed to print 10?
No, it isn't.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Why is it not guaranteed to print 10??
inspite of making the conversion correct..
 
P

praveenraj1987

(e-mail address removed) said:





Because no such guarantee exists. The C Standard says that
converting a pointer to an integer (and vice versa) is possible,
using a suitable cast, but it says nothing about the result except
that it is "implementation-defined".

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Standard says that if an int is converted to int* then result is
implementation defined..
result as in if any read/write is made to that location that may be
implementation defined... as it may not be aligned to page boundary or
a trap representation..

but here no read or write is made to that memory location..

and moreover first i'm converting it to int* and then printing it back
after converting it back to int..
i can understand this to be implementation defined as..
int *p;
p=(int*)10;
printf("%d",p); //not defined.. i can understand..

but in the former case i think its valid..
 
K

Keith Thompson

(e-mail address removed) said:
On May 9, 1:34 pm, Richard Heathfield <[email protected]>
wrote:
(e-mail address removed) said: [...]
#include<stdio.h>
int main()
{
int *p;
p=(int*)10;
printf("%d",(int)p);
}
does this program is in C now.. :D
Well, that's written in C, at least. What is your question about
it? Is it a valid program? Yes, it is. Is it guaranteed to print
10? No, it isn't.
Why is it not guaranteed to print 10??

Because no such guarantee exists. The C Standard says that
converting a pointer to an integer (and vice versa) is possible,
using a suitable cast, but it says nothing about the result except
that it is "implementation-defined".
[...]
Standard says that if an int is converted to int* then result is
implementation defined..
result as in if any read/write is made to that location that may be
implementation defined... as it may not be aligned to page boundary or
a trap representation..

but here no read or write is made to that memory location..

Right, you don't dereference the pointer, but that's not terribly
relevant. The result of the conversion is implementation-defined. It
could even give you a trap representation, which means that the
behavior of just reading the pointer value is undefined (your program
crashing is one of the more benign possible effects).
and moreover first i'm converting it to int* and then printing it back
after converting it back to int..

On many implementations, it's very likely that the program will print
10. But the standard just doesn't make enough guarantees about
integer-to-pointer and pointer-to-integer conversions to ensure that
it will do so.
i can understand this to be implementation defined as..
int *p;
p=(int*)10;
printf("%d",p); //not defined.. i can understand..

Right, because "%d" expects an int, and passing a pointer instead
invokes undefined behavior.
but in the former case i think its valid..

Nope.
 
P

praveenraj1987

(e-mail address removed) said:
On May 9, 1:34 pm, Richard Heathfield <[email protected]>
wrote:
(e-mail address removed) said: [...]
#include<stdio.h>
int main()
{
int *p;
p=(int*)10;
printf("%d",(int)p);
}
does this program is in C now.. :D
Well, that's written in C, at least. What is your question about
it? Is it a valid program? Yes, it is. Is it guaranteed to print
10? No, it isn't.
Why is it not guaranteed to print 10??
Because no such guarantee exists. The C Standard says that
converting a pointer to an integer (and vice versa) is possible,
using a suitable cast, but it says nothing about the result except
that it is "implementation-defined".
[...]
Standard says that if an int is converted to int* then result is
implementation defined..
result as in if any read/write is made to that location that may be
implementation defined... as it may not be aligned to page boundary or
a trap representation..
but here no read or write is made to that memory location..

Right, you don't dereference the pointer, but that's not terribly
relevant.  The result of the conversion is implementation-defined.  It
could even give you a trap representation, which means that the
behavior of just reading the pointer value is undefined (your program
crashing is one of the more benign possible effects).
and moreover first i'm converting it to int* and then printing it back
after converting it back to int..

On many implementations, it's very likely that the program will print
10.  But the standard just doesn't make enough guarantees about
integer-to-pointer and pointer-to-integer conversions to ensure that
it will do so.
i can understand this to be implementation defined as..
int *p;
p=(int*)10;
printf("%d",p); //not defined.. i can understand..

Right, because "%d" expects an int, and passing a pointer instead
invokes undefined behavior.
but in the former case i think its valid..

Nope.

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

on this page
http://en.allexperts.com/q/C-1040/2009/3/convert-pointer-int.htm

Note that while exactly what reinterpret_cast does is implementation
defined the ISO C++ standard does state that:

"A pointer can be explicitly converted to any integral type
large enough to hold it.
The mapping function is implementation defined
[Note: it is intended to be unsurprising to those who know the
addressing structure
of the underlying machine. ]"

and that:

"A value of integral type or enumeration type can be explicitly
converted to a pointer.
A pointer converted to an integer of sufficient size (if any
such exists on the
implementation) and back to the same pointer type will have its
original value;
mappings between pointers and integers are otherwise
implementation defined."

so it does say that conversion is true..in second paragraph..
 
W

Willem

(e-mail address removed) wrote:
) on this page
) http://en.allexperts.com/q/C-1040/2009/3/convert-pointer-int.htm
)
) Note that while exactly what reinterpret_cast does is implementation
) defined the ISO C++ standard does state that:
)
) "A pointer can be explicitly converted to any integral type
) large enough to hold it.
) The mapping function is implementation defined
) [Note: it is intended to be unsurprising to those who know the
) addressing structure
) of the underlying machine. ]"
)
) and that:
)
) "A value of integral type or enumeration type can be explicitly
) converted to a pointer.
) A pointer converted to an integer of sufficient size (if any
) such exists on the
) implementation) and back to the same pointer type will have its
) original value;
) mappings between pointers and integers are otherwise
) implementation defined."
)
) so it does say that conversion is true..in second paragraph..

All it sais if that you convert pointer->int->pointer you will get the same
value. It sais nothing about int->pointer->int.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
M

Martin Ambuhl

#include<iostream>
using namespace std;

int main()
{
int *p;
p=(int*)10;
cout<<(int)p;
}

is this program valid..

No. It is not C.
no read/write is being made at that location..
pls be specific to this program itself..

Specifically:
1) it includes a non-standard header <iostream>
2) it contains a syntax error: 'using namespace std;'
3) it uses an undeclared variable 'cout' which should be of some kind of
unsigned int, since you apply a left shift operation to it.
3a) Not an error, but wasted typing exercise: after you evaluate 'cout'
left shifted by a pointer whose value was converted to an int (which
might not be the appropriate integer type for pointers), you simply
discard the result.
 

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
474,160
Messages
2,570,889
Members
47,422
Latest member
LatashiaZc

Latest Threads

Top