Casting between integers and pointers in C ++

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

Someone asked me: "What is wrong with the above code?" and I could not understand it. Someone please help me.

+7
c ++ pointers
source share
7 answers

Some people need a quote from the C ++ standard (I would put it in the comments to this answer if the format of the comments was not so limited), here are two of one from 1999:

5.2.10 / 3

The mapping done with reinterpret_cast is determined by the implementation.

5.2.10 / 5

An integral type or enumeration type value can be explicitly converted to a pointer. The pointer is converted to an integer of sufficient size (if ant exists in the implementation) and will have its original value back to the same type of pointer; mappings between pointers and integers are otherwise determined by the implementation.

And I do not see anything mandatory that such a mapping, defined by the implementation, should give a valid representation for all input data. Otherwise, an implementation in an architecture with address registers can be very well caught when executed.

 p = (int*)10; 

if the mapping does not give a view valid at that time (yes, what is a valid view for the pointer may be time dependent. For example, delete may invalidate the view of the remote pointer).

+8
source share

The fact that int and pointer data types should not have the same number of bits, according to the C ++ standard, is one thing: this means that you can lose precision.

Also, casting an int pointer to an int and then back is stupid. Why not just leave it as an int ?

I really tried to compile this under gcc, and it worked fine, but probably more by accident than a good design.

+9
source share

Assuming I'm right about what it should be, it should look like this:

 int main() { int *p, *c; // Something that creates whatever p and c point to goes here, a trivial example would be. int pValue, cValue; p = &pValue; c = &cValue; // The & operator retrieves the memory address of pValue and cValue. *p = 10; *c = 20; cout << *p << *c; } 

To assign or get a value to the variable referenced by a pointer, you need dereference .

What your code does is pour 10 into a pointer to an int (which is the memory address where the actual int is).

+1
source share

You assign values ​​(10 and 20) to pointers, which are obviously a potential problem if you try to read data at these addresses. Dropping a pointer to an integer is also very ugly. And your main function does not have a return statement. These are just a few things.

0
source share

addresses p and c can be more than int.

0
source share

Problem on some platforms you need

 p = (int*) (long) 10; 

See the GLIB documentation for type conversion macros .

And for people who cannot find use for this type of expression, you can return data inside the returned pointer functions. You can find examples from the real world, where in this case it is better to use this idiom, instead of allocating a new integer in a heap and returning it back - poor performance, memory fragmentation, just ugly.

0
source share

everything is more or less wrong in it:

 int *p,*c; p=(int*)10; c=(int*)20; 
  • then p points to memory address 10
  • then c points to memory address 20

This does not look very intentional.

And I suppose the whole program will just work.

-one
source share

All Articles