Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > compare a large number of variables

Thread Tools

compare a large number of variables

Chris Torek
Posts: n/a
>> Charlie Gordon wrote:
>>> printf("%p", 0);
>>> As we all know, this code in incorrect, and it will fail on
>>> architectures where
>>> sizeof(void*) > sizeof(int)

>Roland Illig <(E-Mail Removed)> writes:
>> Not quite. If the argument passing scheme doesn't care about the size,
>> for example by using registers for both types ...

In article <(E-Mail Removed)>
Keith Thompson <(E-Mail Removed)> wrote:
>Even assuming both types are passed in the *same* register, the code
>can fail if a null pointer isn't represented as all-bits-zero. There
>may be other ways it can fail that I can't think of off the top of my

The usual one is that you run out of argument registers, so that
remaining arguments are passed by some other mechanism, where the
size does matter. For instance, on MIPS, you get four "integer-like"
arguments via registers, the rest via temporary storage the compiler
allocates in an appropriate stack frame; on SPARC you get six; on
PowerPC the number depends on whose ABI you use, I believe.


printf("%p", 0);

"just happens" to work, but:

printf("i: %d, s: %s, c: %c, p: %p", i, s, c, 0);

"just happens" to behave bizarrely on a 64-bit MIPS. (The four
arguments a0 through a3 include the format and i, s, and c; the
fifth argument for %p -- given incorrectly here as an unadorned
integer constant zero -- writes only 32 of the 64 bits that will
be read.)

The same code "just happens" to work on the SPARC until two more
parameters are inserted.

>> Nevertheless, the code is not portable and should be avoided.

>Yup. In fact, it invokes undefined behavior, with all that implies.

In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
Reading email is like searching for food in the garbage, thanks to spammers.
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare two extremely large lists? Joe Young Perl Misc 7 01-18-2011 11:22 AM
Put variables into member variables or function variables? C++ 9 03-23-2008 04:03 PM
How to compare two large text files? www Java 3 06-29-2007 02:03 PM
OT: Number Nine, Number Nine, Number Nine FrisbeeŽ MCSE 37 09-26-2005 04:06 PM
How to compare these variables by use operator 'AND' pattanawadee C Programming 5 09-23-2003 04:36 PM