J
junky_fellow
Hi,
In my previous post I asked if sizeof may be implemented as a
function. Somebody pointed a link that says that sizeof may calculated
as follows with a warning that it is not guaranteed to work on all
implementation.
size_t size_obj = (char*)(&obj + 1) - (char*)(&obj);
I wanted to find out on which implementation this would fail. One of
the answer that I got was that ptrdiff_t may not be of same size as
size_t, due to which this may fail.
I was thinking of one more scenario where this may fail, but not sure
whether I am right or wrong. Just wanted to take your opinion.
Consider a 32 bit machine on which the "obj" is lying at the largest
address ie. 0xffffffff on this machine.
Shouldn't the addition of 1 to the obj pointer fail on this machine and
we are likely to get a wrong value of the size ? Aren't we assuming
that &obj + 1 will always be a valid address by doing that ?
With this one more question comes to my mind.
When we talk about arrays, we say that address of element, one past the
last element will always be a valid one. Does that mean, that on a 32
bit machine, the largest array of char would be of size 0xffffffff -1 ?
many thanks for any help/comments ...
In my previous post I asked if sizeof may be implemented as a
function. Somebody pointed a link that says that sizeof may calculated
as follows with a warning that it is not guaranteed to work on all
implementation.
size_t size_obj = (char*)(&obj + 1) - (char*)(&obj);
I wanted to find out on which implementation this would fail. One of
the answer that I got was that ptrdiff_t may not be of same size as
size_t, due to which this may fail.
I was thinking of one more scenario where this may fail, but not sure
whether I am right or wrong. Just wanted to take your opinion.
Consider a 32 bit machine on which the "obj" is lying at the largest
address ie. 0xffffffff on this machine.
Shouldn't the addition of 1 to the obj pointer fail on this machine and
we are likely to get a wrong value of the size ? Aren't we assuming
that &obj + 1 will always be a valid address by doing that ?
With this one more question comes to my mind.
When we talk about arrays, we say that address of element, one past the
last element will always be a valid one. Does that mean, that on a 32
bit machine, the largest array of char would be of size 0xffffffff -1 ?
many thanks for any help/comments ...