Display a hexadecimal address as a decimal?

I have a program that uses a function to return addresses for specific elements inside and outside of a dynamically defined array of floats. As anticipated, when I cout the returned pointer, it displays in hexadecimal. However, I'm trying to demonstrate clearly in decimal what addresses are inside and which are outside of the array. For example, if [0][0] has a given address, say, 1000, I should be able to mathematically calculate based on the data type and the array size the address of the final element[x][y], say, 1240. I can then show that the addresses outside of the array bounds (using dynamic memory so no errors) aren't within the array, for example [x][y+1] has an address of say, 1260, and that's expected because we know the array occupies the address between 1000 and 1240 (just making up numbers here).

To do accomplish the above, I'm trying to display the pointer returned from my function as a decimal address, but I can't figure out how. I've tried casting as an uintptr_t (based on this: http://stackoverflow.com/questions/30496212/converting-an-output-from-hexadecimal-to-decimal-in-c#30496241), but get a message that I can't convert a float pointer to a uintptr_t (not sure why, the address isn't a float). Any thoughts on how I might be able to accomplish my goal?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <cstdint>
#include <iostream>
using namespace std;

float *getBegin(float *arrptr, int x, int y, int size_y);

int main()
{
	int num_rows = 5;
	int num_cols = 6;

	//Create array object
	float *ptr = new float[num_rows * num_cols];

	//Show address of first position in array [0][0]
	cout << "Address of first position: " 
		<< static_cast<uintptr_t>(getBegin(ptr, 0, 0, num_cols)) << endl;	//This generates error invalid type conversion, cannot convert float * to uintptr

	return 0;
}


//*********************************************************************
//Function to return a pointer to the array element at position [x,y] *
//*********************************************************************

float *getBegin(float *arrptr, int x, int y, int size_y) {
	float *ptr = arrptr;
	return ptr + ((x * size_y) + y);
}


Address of first position: 00D0EEF8

Last edited on
Use reinterpret_cast instead of static_cast.

Your code leaks memory.
Thanks and thanks. Are you able to shed light on why einterpret_cast works here and not static_cast? Tried searching around for the difference and I don't quite get it.

And 100% right regarding my poor memory management - the program I've pasted here is a small reworked subset of a much larger program that I made for these forums. In putting it together I didn't do all the checks I normally would - but I should either way!
This is a simplification, but basically static_cast will only convert compatible types.

static_cast can downcast and upcast references and pointers, and it can follow implicit conversion sequences and use constructors according to the overload resolution rules. There are some extra rules too, but they're insufficient to convert your pointer to uintptr_t -- the two types aren't compatible.

reinterpret_cast asks the compiler to throw away type information and treat an expression differently. It doesn't perform any conversion sequences or call any constructors, but simply tells the compiler to reinterpret the bits of the expression as a new type, subject to some restrictions. You can use reinterpret_cast to convert most stuff as long as it's the same size.

A C-style cast would have worked too: A C-style cast has the compiler try really hard to perform a type conversion, but it does so at the expense of being extremely error-prone.

The full story can be found here:
http://en.cppreference.com/w/cpp/language/static_cast.
http://en.cppreference.com/w/cpp/language/reinterpret_cast
Last edited on
Topic archived. No new replies allowed.