473,507 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

casting from int to void* and back to int

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
Nov 14 '05 #1
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.
Nov 14 '05 #2
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
Nov 14 '05 #3
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
Nov 14 '05 #4
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
Nov 14 '05 #5
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
Nov 14 '05 #6
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

Nov 14 '05 #7
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.
Nov 14 '05 #8

"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 ?


Nov 14 '05 #9
"jacob navia" <ja***@jacob.remcomp.fr> 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*.

--
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 ?


He obviously already did; but did you?
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #10
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

Nov 14 '05 #11
BOTH ways are non portable.
int -->void * -->int
or
void *-->int-->void *
Nov 14 '05 #12
"jacob navia" <ja***@jacob.remcomp.fr> wrote:
BOTH ways are non portable.
int -->void * -->int
or
void *-->int-->void *


To whom or what are you replying?
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #13

"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???
Nov 14 '05 #14
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
Nov 14 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3442
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...
16
2419
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...
8
8883
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...
231
22922
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...
35
2645
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
33
3617
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...
3
3628
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'...
7
2638
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...
5
5907
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...
9
2448
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...
0
7321
Oralloy
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,...
0
7377
jinu1996
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...
1
7034
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...
0
7488
tracyyun
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...
1
5045
isladogs
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...
0
4702
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
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 ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.