Hi All,
I apologize if this has been brought up here before.
Searching through the newsgroup I found variants of
my question, but not exactly what I am thinking about.
If on a given platform, I am guaranteed that:
sizeof(integer) <= sizeof(void*)
Is it safe to store an int in a void* and cast it
back to int without truncating/losing data ?
Secondly, is the minimum size of a void* (or char*),
defined in the standards or is it entirely platform
dependant ?
TIA,
Pranab 14 10545
Pranab Mehta wrote: If on a given platform, I am guaranteed that: sizeof(integer) <= sizeof(void*) Is it safe to store an int in a void* and cast it back to int without truncating/losing data ?
It's not safe, in that there's no guarantee in the standard that it
will work. C99 defines the types "intptr_t" and "uintptr_t" which do
preserve equality on round-trip conversions with pointer to void, but
unfortunately the trip is the wrong way: void* -> integer -> void*
rather than integer -> void* -> integer.
If int is no larger than pointer to void then one way to store the
value is by simply copying the bytes:
int i = value;
void *p;
memcpy(&p, &i, sizeof i);
If you later copy the bytes back into an int object, the value is
guaranteed to be the same as the original. This method has the
(rather major) drawback that you can't use the pointer to void as a
value, even the trivial expression statement
p;
invokes undefined behaviour if the representation is not a valid one
for pointer to void. This means that it's also not strictly safe to
pass the pointer to void to functions, for example.
Storing an int in a pointer to void is a pretty nasty thing to do, and
indicates a possible problem with your design, in my opinion.
Secondly, is the minimum size of a void* (or char*), defined in the standards or is it entirely platform dependant ?
In a C99 hosted environment a pointer to void must be at least 17 bits
wide.
Jeremy.
Pranab Mehta wrote: Hi All,
I apologize if this has been brought up here before. Searching through the newsgroup I found variants of my question, but not exactly what I am thinking about.
If on a given platform, I am guaranteed that: sizeof(integer) <= sizeof(void*) Is it safe to store an int in a void* and cast it back to int without truncating/losing data ?
Is it "safe?" Yes, because on the great majority of
C implementations it will work as you desire. Is it
"perfectly safe?" No, because the C language Standard
does not specify the result of either conversion, and it
is therefore possible that some C implementation might
not give the desired result.
You must make your own decisions about how much "safety"
your applicaition requires.
Secondly, is the minimum size of a void* (or char*), defined in the standards or is it entirely platform dependant ?
I'm not sure what you are asking here. No C object
can have a `sizeof' smaller than 1. No non-aggregate
object is required to have a `sizeof' greater than 1.
All the Standard has to say about your question is
1 <= sizeof(void*) && sizeof(void*) == sizeof(char*)
.... and each implementation is free to choose any value
satisfying these constratints.
-- Er*********@sun.com
On 12 Apr 2004 13:32:48 -0700, pr****@employees.org (Pranab Mehta) wrote: Hi All,
I apologize if this has been brought up here before. Searching through the newsgroup I found variants of my question, but not exactly what I am thinking about.
If on a given platform, I am guaranteed that: sizeof(integer) <= sizeof(void*) Is it safe to store an int in a void* and cast it back to int without truncating/losing data ?
I was hesitant to be the first one to attempt to answer this, and I'm glad
I waited ;-)
In any case, Jeremy brought up the thing that immediately popped into my
mind upon first reading your post: "Why?"
As much as I wrinkle up my nose at the thought of using unions, in this
particular case my nose curls even higher at the thought of the cast you
proposed (and my ears begin to wiggle). Could the use of a union possibly
spare you from the indignity of such an ugly cast?
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
In a 64 bit machine with sizeof(int) == 4
and sizeof(void *) == 8 this will fail.
Such a system is the new 64 bit version of
windows for instance.
For that system, lcc-win32 uses:
sizeof(int) == sizeof(long) == 4
sizeof(void *) == sizeof(long long) == 8
You should never assume that an int will hold
a pointer since windows 2.0...
In the 16 bit world we had
sizeof(int) == sizeof(short) == 2
sizeof(char *) ==sizeof(long) == 4
using the FAR memory model. Using the SMALL/NEAR
memory model you had
sizeof(int) == sizeof(void *) == 2
Mixtures of pointers were possible, with
data pointers in 32 bits and code pointers
in 16 bits, etc.
Windows 32 introduced
sizeof(int) == 4 == sizeof(void *).
Since 1995, you can store a pointer
in an integer. This will not work in
64 bit windows systems where a void *
is 8.
If the expected life time
of your software will not exceed a few
years, you can still do it since 64 bit
systems will run 32 bit software using
an emulation layer.
jacob http://www.cs.virginia.edu/~lcc-win32
jacob navia wrote: In a 64 bit machine with sizeof(int) == 4 and sizeof(void *) == 8 this will fail.
Such a system is the new 64 bit version of windows for instance.
For that system, lcc-win32 uses: sizeof(int) == sizeof(long) == 4 sizeof(void *) == sizeof(long long) == 8
You should never assume that an int will hold a pointer since windows 2.0... [...]
He's attempting int -> void* -> int, not
void* -> int -> void*.
-- Er*********@sun.com
Jeremy Yallop wrote:
.... snip ... In a C99 hosted environment a pointer to void must be at least 17 bits wide.
Where did you get this? Chapter & verse please.
--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
CBFalconer wrote: Jeremy Yallop wrote: ... snip ... In a C99 hosted environment a pointer to void must be at least 17 bits wide.
Where did you get this? Chapter & verse please.
5.2.4.1 Translation limits
1 The implementation shall be able to translate and execute at
least one program tha\t contains at least one instance of every
one of the following limits:
[...]
-- 65535 bytes in an object (in a hosted environment only)
A pointer to void must be able to represent the address of every byte
of such an object, plus a null pointer, plus one past the end of the
object, and perhaps one or two other things. To represent 65537
distinct values requires 17 bits of storage.
Jeremy.
"Eric Sosman" <Er*********@sun.com> a écrit dans le message de
news:40***************@sun.com... jacob navia wrote:
He's attempting int -> void* -> int, not void* -> int -> void*.
-- Er*********@sun.com
Read the original message please: Is it safe to store an int in a void* and cast it back to int without truncating/losing data ?
Jeremy Yallop wrote: CBFalconer wrote: Jeremy Yallop wrote: ... snip ... In a C99 hosted environment a pointer to void must be at least 17 bits wide.
Where did you get this? Chapter & verse please.
5.2.4.1 Translation limits
1 The implementation shall be able to translate and execute at least one program tha\t contains at least one instance of every one of the following limits: [...]
-- 65535 bytes in an object (in a hosted environment only)
A pointer to void must be able to represent the address of every byte of such an object, plus a null pointer, plus one past the end of the object, and perhaps one or two other things. To represent 65537 distinct values requires 17 bits of storage.
Consider that a pointer need not consist of a single address. It
may very well be an index into a table, for example. Pointers to
all those objects need not exist simultaneously. If they did they
would use up all that minimum storage specified.
--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
BOTH ways are non portable.
int -->void * -->int
or
void *-->int-->void *
"Eric Sosman" <Er*********@sun.com> a écrit dans le message de
news:40***************@sun.com... jacob navia wrote:
He's attempting int -> void* -> int, not void* -> int -> void*.
Ahhh. You think that the result would be different???
jacob navia wrote: "Eric Sosman" <Er*********@sun.com> a écrit dans le message de news:40***************@sun.com... jacob navia wrote:
He's attempting int -> void* -> int, not void* -> int -> void*. Ahhh. You think that the result would be different???
Under the conditions specified by the O.P.:
If on a given platform, I am guaranteed that: sizeof(integer) <= sizeof(void*)
... it is "likely" that the result will be as desired,
that is, that the original int value will survive being
converted to void* and back. As others (you among them)
have pointed out, "likely" is not "certain" because the
Standard makes no such guarantee.
Converting an arbitrary pointer value to an `int'
and back is "less likely" to work, for the reasons you
listed in your first post to this (interminable) thread.
-- Er*********@sun.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jacob Jensen |
last post by:
This question has probably been asked a million time, but here it comes
again. I want to learn the difference between the three type cast operators:
static_cast, reinterpret_cast, dynamic_cast. A...
|
by: He Shiming |
last post by:
Hi,
I'm having a little bit of trouble regarding pointer casting in my program.
I don't understand why the following two cases produce different results.
Case 1:
IInterface *pInterface = new...
|
by: rihad |
last post by:
Hi, I've this question: suppose we have two differently typed pointers:
struct foo *foo;
char **array;
Becase one can always legally cast any pointer to (void *), and becase (void *)
is...
|
by: Brian Blais |
last post by:
Hello,
I saw on a couple of recent posts people saying that casting the return
value of malloc is bad, like:
d=(double *) malloc(50*sizeof(double));
why is this bad? I had always thought...
|
by: ytrama |
last post by:
Hi,
I have read in one of old posting that don't cast of pointer which
is returned by the malloc. I would like to know the reason.
Thanks in advance,
YTR
| |
by: Mark P |
last post by:
A colleague asked me something along the lines of the following today.
For some type X he has:
X* px = new X;
Then he wants to convert px to a char* (I'm guessing for the purpose of...
|
by: Beta What |
last post by:
Hello,
I have a question about casting a function pointer. Say I want to make
a generic module (say some ADT implementation) that requires a function
pointer from the 'actual/other modules'...
|
by: William S Fulton |
last post by:
I'm looking for the name of the following casting style in order to do
some reading around on it and why it is sometimes used.
unsigned long long ull = 0;
void * ptr = 0;
ull = *(unsigned...
|
by: brekehan |
last post by:
I've always been a little sketchy on the differences between static,
dynamic, and reinterpret casting. I am looking to clean up the
following block by using C++ casting instead of the C style...
|
by: Jess |
last post by:
Hello,
It seems both static_cast and dynamic_cast can cast a base class
pointer/reference to a derived class pointer/reference. If so, is
there any difference between them?
In addition, if I...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |