Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

ValuePointer union

1 view
Skip to first unread message

Steve Byan

unread,
Mar 1, 2010, 7:58:50 PM3/1/10
to cmoc...@googlegroups.com
Why does cmockery use the ValuePointer union to cast between void * and LargestIntegralType?

// Used to cast LargetIntegralType to void* and vice versa.
typedef union ValuePointer {
LargestIntegralType value;
void *pointer;
} ValuePointer;

I'm no C expert, but this looks unportable to me; what happens when void * is smaller than LargestIntegralType on a big-endian machine? You might get luck on a little-endian machine.

Why not use something along the lines of ((void *) value)? Cmockery already uses ((LargestIntegralType) pointer).

Best regards,
-Steve

--
Steve Byan <stev...@me.com>
Littleton, MA 01460

Zhiming G

unread,
Mar 2, 2010, 12:11:25 AM3/2/10
to cmoc...@googlegroups.com
union is not endian-portable.
maybe we should better write it as ((void *) ((uintptr_t)value))

2010/3/2 Steve Byan <stev...@verizon.net>



--
You received this message because you are subscribed to the Google Groups "Cmockery" group.
To post to this group, send email to cmoc...@googlegroups.com.
To unsubscribe from this group, send email to cmockery+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cmockery?hl=en.


Steve Byan

unread,
Mar 2, 2010, 10:18:11 AM3/2/10
to cmoc...@googlegroups.com

On Mar 2, 2010, at 12:11 AM, Zhiming G wrote:

> union is not endian-portable.
> maybe we should better write it as ((void *) ((uintptr_t)value))

Any particular reason to favor a union over a cast? If we change the definition of the ValuePointer union to use uintptr_t rather than LargestIntegralType, I think it becomes portable.

I've always avoided type-puns in favor of casts, but I'm not a C language lawyer. Maybe there are reasons to favor a union type-pun in cmockery. Any comments? Stewart?

Stewart Miles

unread,
Mar 2, 2010, 1:21:49 PM3/2/10
to cmoc...@googlegroups.com
The type-pun vs. cast depends upon the compiler you're using and the platform you're working on.  If you can prove that a direct cast works around compiler errors / warnings on  all common compilers rather than the type-pun then go ahead with the change.

At the very least, making sure a change works with multiple versions of gcc and msvc would be a good place to start.

Reply all
Reply to author
Forward
0 new messages