0

For example:

int* x = new int;
int y = reinterpret_cast<int>(x);

y now holds the integer value of the memory address of variable x. Variable y is of size int. Will that int size always be large enough to store the converted memory address of ANY TYPE being converted to int?

EDIT: Or is safer to use long int to avoid a possible loss of data?

EDIT 2: Sorry people, to make this question more understandable the thing I want to find out here is the size of returned HEX value as a number, not size of int nor size of pointer to int but plain hex value. I need to get that value in in human-readable notation. That's why I'm using reinterpret_cast to convert that memory HEX value to DEC value. But to store the value safely I also need to fing out into what kind of variable to it: int, long - what type is big enough?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
codekiddy
  • 5,897
  • 9
  • 50
  • 80
  • Use size_t or more correctly - uintptr_t to match the pointer size. – malkia Jan 05 '12 at 03:25
  • 2
    I don't think you understand what "hex value" means. There's no such thing as a "memory HEX value". You have a pointer. It's made up of some number of bits. You can represent that textually however you like but that has nothing to do with the code you've posted. – Brian Roach Jan 05 '12 at 03:32
  • @Brian Roach, maybe I do not understand but memory address looks like 0x0f3a89bb, isn't that HEX value? each object has it's uniqe memory address like that. so how do think "There's no such thing as a "memory HEX value"? thanks. – codekiddy Jan 05 '12 at 03:44
  • 1
    @codekiddy - yes, that is a hexadecimal number. Whatever it is you're using to see that is *converting a bunch of bits (a value) into a hexadecimal textual representation and showing to you*. If that's what you need, you would need to do the same. Computers have no notion of hexadecimal; they operate in binary. – Brian Roach Jan 05 '12 at 03:49

4 Answers4

7

No, that's not safe. There's no guarantee sizeof(int) == sizeof(int*)

On a 64 bit platform you're almost guaranteed that it's not.

As for the "hexadecimal value" ... I'm not sure what you're talking about. If you're talking about the textual representation of the pointer in hexadecimal ... you'd need a string.

Edit to try and help the OP based on comments:

Because computers don't work in hex. I don't know how else to explain it. An int stores some amount of bits (binary), as does a long. Hexadecimal is a textual representation of those bits (specifically, the base16 representation). strings are used for textual representations of values. If you need a hexadecimal representation of a pointer, you would need to convert that pointer to text (hex).

Here's a c++ example of how you would do that:

test.cpp

#include <string>
#include <iostream>
#include <sstream>

int main()
{

    int *p; // declare a pointer to an int. 
    std::ostringstream oss; // create a stringstream
    std::string s; // create a string

    // this takes the value of p (the memory address), converts it to 
    // the hexadecimal textual representation, and puts it in the stream
    oss << std::hex << p;

    // Get a std::string from the stream
    s = oss.str();

    // Display the string
    std::cout << s << std::endl;

}

Sample output:

roach$ g++ -o test test.cpp
roach$ ./test
0x7fff68e07730

It's worth noting that the same thing is needed when you want to see the base10 (decimal) representation of a number - you have to convert it to a string. Everything in memory is stored in binary (base2)

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • +1 from me. Judging from the OP's latest edit, he is apparently still not clear on the distinction between physical data storage and textual representation of POD integers. – ksming Jan 05 '12 at 03:35
  • Yeah, that's my suspicion as well. – Brian Roach Jan 05 '12 at 03:37
  • @Brian, you said I'll need a string to store hex representation of pointer... why string?, what's wrong with int or lon long as I intended in my question? thanks alot. – codekiddy Jan 05 '12 at 04:13
2

On most 64-bit targets, int is still 32-bit, while pointer is 64bit, so it won't work.

http://en.wikipedia.org/wiki/64-bit#64-bit_data_models

malkia
  • 1,389
  • 13
  • 12
  • thanks, but what the size of converted memory HEX value? is size of int big enough to store that converted hex memory value? – codekiddy Jan 05 '12 at 03:00
  • No, you shouldn't rely on int being able to hold a pointer value. I think you'd be safe with long long though, or better just size_t. From memory it'd work on some systems but not on 64 bit Linux or Windows. Check your system with cout << "int: " << sizeof(int) << endl << "Pointer: " << sizeof(int*) – matiu Jan 05 '12 at 03:08
  • @codekiddy: why hex? Because your debugger shows it as hex? Anyhow, hex or not, it's entirely irrelevant. `x` is a pointer value. As others have already pointed out, it may not fit into `int`. It may not fit even into `long`. It will fit into `uintptr_t`. If your compiler doesn't support `uintptr_t`, the next best thing is `size_t`. – Alexey Frunze Jan 05 '12 at 03:18
  • thanks, I've added more info into my question, cos I need the size of returned HEX value not int. – codekiddy Jan 05 '12 at 03:19
  • @Alex HEX value because I'll use it to compare some objects which will have that value stored as some kind of ID. – codekiddy Jan 05 '12 at 03:23
  • 2
    @codekiddy: but there's no hex value. There is just a value. How you print it or save to a file (as binary, octal, decimal, hex or other) is entirely independent of the value itself. If you want it look like hex somewhere, you have to actually convert the value into a string containing the hex digits making up/representing the value. – Alexey Frunze Jan 05 '12 at 03:31
  • @Alex yeah there is just a value. but that value is stored at some memory address, isn'it? and that address AFAIK is represended as uniqe HEX value. – codekiddy Jan 05 '12 at 03:46
  • 1
    @codekiddy: the fact that it can be represented as something doesn't mean it is already represented as that something. All distinct objects in C(++) have unique addresses, true (we're not talking about aliased through references or unions variables). – Alexey Frunze Jan 05 '12 at 03:59
1

What you probably want is to use std::ostream's formatting of addresses:

int x(0);
std::cout << &x << '\n';

As to the length of the produced string, you need to determine the size of the respective pointer: for each used byte the output will use two hex digit because each hex digit can represent 16 values. All bytes are typically used even if it is unlikely that you have memory for all bytes e.g. when the size of pointers is 8 bytes as happens on 64 bit systems. This is because the stacks often grow from the biggest address downwards while the executable code start at the beginning of the address range (well, the very first page may be unused to cause segmentation violations if it is touched in any way). Above the executable code live some data segments, followed by the heap, and lots of unused pages.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
0

There is question addressing similar topic:

https://stackoverflow.com/a/2369593/1010666

Summary: do not try to write pointers into non-pointer variable. If you need to print out the pointer value, there are other solutions.

Community
  • 1
  • 1
Tadej Mali
  • 1,143
  • 8
  • 18