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.. as no memory read and write is being made..
p is just being used as a variable..
pls be specific to this program..
 
I

Ian Collins

#include<iostream>
using namespace std;
int main()
{
int *p;
p=(int*)10;
cout<<(int)p;
}

is this program valid.. as no memory read and write is being made..
p is just being used as a variable..
pls be specific to this program..

What is your question?
 
P

praveenraj1987

No, it isn't even guaranteed to compile.  Consider the case where
sizeof(int*) > sizeof(int).

a cast is being made..
int *p;
p=(int*)10;// casting it to int*

and printing back to
cout<<(int)p;
i'm specifically speaking about this program..
no other case..
 
I

Ian Collins

a cast is being made..
int *p;
p=(int*)10;// casting it to int*

and printing back to
cout<<(int)p;
i'm specifically speaking about this program..
no other case..

CC /tmp/x.cc -m64
"/tmp/x.cc", line 7: Error: Cannot cast from int* to int.

That was your program compiled in 64 bit mode.
 
P

praveenraj1987

CC /tmp/x.cc -m64
"/tmp/x.cc", line 7: Error: Cannot cast from int* to int.

That was your program compiled in 64 bit mode.

Which compiler and platform.. if that is the case..
then thnx but would like to try it on own..
 
P

praveenraj1987

Which compiler and platform.. if that is the case..
then thnx but would like to try it on own..

it should compile at least.. with a warning.

C:\Documents and Settings\tango.TANGO-PC\Desktop\temp\temp1.cpp||In
function `int main()':|
C:\Documents and Settings\tango.TANGO-PC\Desktop\temp\temp1.cpp|9|
warning: cast to pointer from integer of different size|
||=== Build finished: 0 errors, 1 warnings ===|
 
P

praveenraj1987

it should compile at least.. with a warning.

C:\Documents and Settings\tango.TANGO-PC\Desktop\temp\temp1.cpp||In
function `int main()':|
C:\Documents and Settings\tango.TANGO-PC\Desktop\temp\temp1.cpp|9|
warning: cast to pointer from integer of different size|
||=== Build finished: 0 errors, 1 warnings ===|


compiled with this online compiler in strict mode..
http://www.comeaucomputing.com/tryitout/
 
J

Juha Nieminen

Ian said:
"/tmp/x.cc", line 7: Error: Cannot cast from int* to int.

Exactly what forbids a cast from int* to int?

(Just because you might lose precision doesn't mean that the cast is
forbidden.)
 
J

Juha Nieminen

Ian said:
No, it isn't even guaranteed to compile.

Why not? Since when has casting from a pointer type to an integral
type been forbidden?
Consider the case where sizeof(int*) > sizeof(int).

That's like saying that casting from long to int is forbidden because
sizeof(long) might be larger than sizeof(int). (Of from double to float.)
 
A

Alf P. Steinbach

* Juha Nieminen:

Because the int has too few bits to represent the pointer.

Since when has casting from a pointer type to an integral
type been forbidden?

It isn't, but there's a requirement on the destination type: that it can
represent the original value, so that it can be casted back with the original
value as result.

That's like saying that casting from long to int is forbidden because
sizeof(long) might be larger than sizeof(int). (Of from double to float.)

No, it isn't: there's no roundtrip conversion requirement on numerical types.


Cheers & hth.,

- Alf
 
P

praveenraj1987

  Why not? Since when has casting from a pointer type to an integral
type been forbidden?


  That's like saying that casting from long to int is forbidden because
sizeof(long) might be larger than sizeof(int). (Of from double to float.)

okay, tell me does this program gives 10 on each compiler..
and about converting pointer-int and back from int-pointer.. is that
the value is same to 10..

on this link..
http://en.allexperts.com/q/C-1040/2009/3/convert-pointer-int.htm
"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."

what it says in second paragraph..
 
J

James Kanze

#include<iostream>
using namespace std;
int main()
{
int *p;
p=(int*)10;
cout<<(int)p;
}
is this program valid.

It doesn't compile on my machine. On machines where it does
compile, it has undefined behavior (which, of course, the
implementation may define.)
. as no memory read and write is being made..

How is that relevant?
p is just being used as a variable..

How can it be used as anything else, since it is a variable.
pls be specific to this program..

OK. Copy/pasted into a file on my machine, then:
Gabi (6): comp ptr.cc
ptr.cc: In function 'int main()':
ptr.cc:14: error: cast from 'int*' to 'int' loses precision

(comp is an alias which invokes g++ with all the necessary
options.)
 
J

James Kanze

Exactly what forbids a cast from int* to int?

The C++ standard.
(Just because you might lose precision doesn't mean that the
cast is forbidden.)

The only legal casts are those explicitly defined in the
standard---you can't just convert anything to anything (although
you can convert a lot more than makes sense). The only cast of
pointer to integral type is a reinterpret_cast, where it says "A
pointer can be explicitly converted to any integral type large
enough to hold it." On my machine, an int isn't large enough to
hold a pointer, so the conversion isn't legal.
 
J

James Kanze

Why not? Since when has casting from a pointer type to an
integral type been forbidden?
That's like saying that casting from long to int is
forbidden because sizeof(long) might be larger than
sizeof(int). (Of from double to float.)
[/QUOTE]

No. The standard says you can cast from long to int, even if
int is smaller than long. The standard says that you cannot
cast from pointer to int if int is smaller than a pointer.
okay, tell me does this program gives 10 on each compiler..

As has been already stated: it doesn't even compile on all
compilers. Where it compiles, it has undefined behavior (unless
the implementation has explicitly defined the behavior).
and about converting pointer-int and back from int-pointer..
is that the value is same to 10..

Pointer to int to pointer isn't the same thing as int to pointer
to int. Your code does the latter.
on this link..http://en.allexperts.com/q/C-1040/2009/3/convert-pointer-int.htm
"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."
what it says in second paragraph..

Just read what it says. It specifically requires that the
integral type be large enough to hold the pointer in order to
convert to int. That's rarely the case on modern machines. And
it requires that pointer->int->pointer result in the same value
if the original pointer is valid. Which is totally irrelevant
to the code you posted.
 
J

Juha Nieminen

James said:
The only cast of
pointer to integral type is a reinterpret_cast, where it says "A
pointer can be explicitly converted to any integral type large
enough to hold it."

Does the standard define any integral type which is guaranteed to be
large enough to hold the value of a pointer?

(I'd say ptrdiff_t, but I'd probably be wrong.)
 
B

Bo Persson

Juha said:
Does the standard define any integral type which is guaranteed to
be large enough to hold the value of a pointer?

No, think segmented memory.
(I'd say ptrdiff_t, but I'd probably be wrong.)

It stores the difference between pointers, doesn't have to store the
pointers themselves.

If you have a segmented memory, the implementation can limit objects
to fit in a single segment. That also limits the required range for
ptrdiff_t.



Bo Persson
 
J

James Kanze

Does the standard define any integral type which is guaranteed
to be large enough to hold the value of a pointer?

It expressedly says that there might not be one (in a
parenthese).
(I'd say ptrdiff_t, but I'd probably be wrong.)

I've used implementations where a pointer was bigger than a
ptrdiff_t. (For that matter, I've used implementations where
there wasn't any integral type as large as a pointer. Now that
long long exists, and requires at least 64 bits, I imagine that
the case will be rare.)

The next version of the standard will include intptr_t and
uintptr_t, in <stdint.h> (or <cstdint>, if you prefer). But the
type is still "optional", and will only be present if such a
type exists.
 

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,420
Latest member
ZitaVos505

Latest Threads

Top