J
James Kanze
Would you ever use a signed integer to represent a memory address?
Would you ever use an integral type to represent a memory address?
Would you ever use a signed integer to represent a memory address?
Tony said:Would you ever use a signed integer to represent a memory address?
James said:Would you ever use an integral type to represent a memory address?
Alf said:* Tony:
In other words, instead of a practical problem one has a clash with
some ideology.
For me (and I guess also for James) choosing between the two is a
no-brainer.
Tony said:I use an unsigned integer type, via preprocessor define, that is the
appropriate width for the target platform.
But the question remains: Why is there such a wide demand for storing
adresses in integral type variables? What is wrong with pointers and
references?
Bo Persson
peter said:I am not sure that there is such a wide demand, but there are
legitimate reasons that one might want to look at pointeres as ints
that might creep up into many types of of programming. One such
frequent one could be for hashing, other one could be related to
special memory allocators.
I use an unsigned integer type, via preprocessor define, that
is the appropriate width for the target platform.
I am not sure that there is such a wide demand, but there are
legitimate reasons that one might want to look at pointeres as
ints that might creep up into many types of of programming.
One such frequent one could be for hashing, other one could be
related to special memory allocators.
peter koch wrote:
Agreed, but there is not much use for a general preprocessor
define here, as hashing would also need to things like how
many bits are actually used (48 out of 64?). The same for a
memory alllocator.
You could just use int, or unsigned long long, or perhaps
ptrdiff_t, as appropriate for a specific system. You must know
the target system anyway.
This is slightly useful in a C-style interfaces which call a
user function and allow the user to pass some data along
unchanged. Rather than using a union:
union Data {
void* p;
int i;
};
void library( void (*user_func)( Data ), Data d )
{
// ...
user_func( d );
}
which involves some inconvenience on the part of the user,
they instead accept an intptr_t (or equivalent if you don't
have stdint.h):
void library( void (*user_func)( intptr_t ), intptr_t d )
{
// ...
user_func( d );
}
This allows the user to pass a plain integer without any work,
or pass a pointer via a cast at the call site and a cast
inside user_func.
James said:And when such a type doesn't exist?
(Admittedly unlikely today,
with long long, but I've encountered it in the past, with 48 bit
pointers and 32 bit longs, and no larger integral type.)
More to the point, of course, is why? An integer (signed or
otherwise) isn't a pointer, and a pointer isn't an integer. And
there's really not much point in trying to represent one in the
other. (There are a very few cases where I would represent a
small integral value as a pointer, but I can't think of any
where I'd represent a pointer as an integer.)
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.