ValuePointer union

已查看 1 次
跳至第一个未读帖子

Steve Byan

未读,
2010年3月1日 19:58:502010/3/1
收件人 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

未读,
2010年3月2日 00:11:252010/3/2
收件人 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

未读,
2010年3月2日 10:18:112010/3/2
收件人 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

未读,
2010年3月2日 13:21:492010/3/2
收件人 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.

回复全部
回复作者
转发
0 个新帖子