c/c++ question: comparing/sorting sctructs

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: c/c++ question: comparing/sorting sctructs

Post by mcostalba »

mcostalba wrote:
trojanfoe wrote:
mcostalba wrote:memcmp is a dirty trick to say at least, don't use it, is not portable, is compiler dependent, is endian dependent, is probably broken and most impostant is unnecessary for you.
:shock: rubbish
Of course memcmp() is a standard function. But just compares RAM. Is the use of memcmp to compare struct that has that long list of qualities.

I don't know if this was clear. Perhaps is still rubbish for you, hopeful less smelly :)

Hey, you guess what ? there is a lot of rubbish out there :)

http://learningcppisfun.blogspot.com/20 ... emcmp.html

http://www.gamedev.net/community/forums ... 2&#2982649


And finally also the mother of all the rubbish:

http://groups.google.co.uk/group/comp.l ... fb2fe4c83a


The latter is a thread on a newsgroup called comp.lang.c where a bunch of fanatics that call themselfs c++ gurus enjoy with ludicrous arguments against memcmp used to implement operator==()...strange world....strange people!
trojanfoe

Re: c/c++ question: comparing/sorting sctructs

Post by trojanfoe »

mcostalba wrote:
mcostalba wrote:
trojanfoe wrote:
mcostalba wrote:memcmp is a dirty trick to say at least, don't use it, is not portable, is compiler dependent, is endian dependent, is probably broken and most impostant is unnecessary for you.
:shock: rubbish
Of course memcmp() is a standard function. But just compares RAM. Is the use of memcmp to compare struct that has that long list of qualities.

I don't know if this was clear. Perhaps is still rubbish for you, hopeful less smelly :)

Hey, you guess what ? there is a lot of rubbish out there :)

http://learningcppisfun.blogspot.com/20 ... emcmp.html

http://www.gamedev.net/community/forums ... 2&#2982649


And finally also the mother of all the rubbish:

http://groups.google.co.uk/group/comp.l ... fb2fe4c83a


The latter is a thread on a newsgroup called comp.lang.c where a bunch of fanatics that call themselfs c++ gurus enjoy with ludicrous arguments against memcmp used to implement operator==()...strange world....strange people!
Well I've already set-out the conditions under which I would use memcmp - if you don't want to use it then don't! Who cares. Can you point out to me where anyone else thinks memcmp is compiler dependant and endian dependant please?
User avatar
Bo Persson
Posts: 243
Joined: Sat Mar 11, 2006 8:31 am
Location: Malmö, Sweden
Full name: Bo Persson

Re: c/c++ question: comparing/sorting sctructs

Post by Bo Persson »

trojanfoe wrote:
mcostalba wrote:memcmp is a dirty trick to say at least, don't use it, is not portable, is compiler dependent, is endian dependent, is probably broken and most impostant is unnecessary for you.
:shock: rubbish
True, it is not endian dependent. :-)
User avatar
Bo Persson
Posts: 243
Joined: Sat Mar 11, 2006 8:31 am
Location: Malmö, Sweden
Full name: Bo Persson

Re: c/c++ question: comparing/sorting sctructs

Post by Bo Persson »

Cardoso wrote:Thank you all for your answers.
Now please consider the special case of a struct with no pointers in it and the compiler switch to padding with 1 byte turned on.
BTW does padding with 1 byte mean no padding bytes at all? The sizeof(structname) would be what we see in the code with no hidden bytes?.
Now, would memcmp work with no problems at all?
If that is the case then I would have my opening book struct array problem solved.

Thanks,
Alvaro
Yes, that would probably work.

However, if the compiler would otherwise chose padding on 4 byte boundaries, it does that for a reason. Most often that reason is the performance of all other operations on the members, like assignment or increment.

Do you want memcmp to be fast, or everyting else? :-)
trojanfoe

Re: c/c++ question: comparing/sorting sctructs

Post by trojanfoe »

Bo Persson wrote:
trojanfoe wrote:
mcostalba wrote:memcmp is a dirty trick to say at least, don't use it, is not portable, is compiler dependent, is endian dependent, is probably broken and most impostant is unnecessary for you.
:shock: rubbish
True, it is not endian dependent. :-)
No, it was none of things stated :P
Dann Corbit
Posts: 12541
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: c/c++ question: comparing/sorting sctructs

Post by Dann Corbit »

Bo Persson wrote:
Cardoso wrote:Thank you all for your answers.
Now please consider the special case of a struct with no pointers in it and the compiler switch to padding with 1 byte turned on.
BTW does padding with 1 byte mean no padding bytes at all? The sizeof(structname) would be what we see in the code with no hidden bytes?.
Now, would memcmp work with no problems at all?
If that is the case then I would have my opening book struct array problem solved.

Thanks,
Alvaro
Yes, that would probably work.

However, if the compiler would otherwise chose padding on 4 byte boundaries, it does that for a reason. Most often that reason is the performance of all other operations on the members, like assignment or increment.

Do you want memcmp to be fast, or everyting else? :-)
It is possible to pack certain structures and leave everything else efficient. E.g. with MS VC++:
#pragma pack(1)
/*
packed struct definitions here...
*/
#pragma pack(8)

However, it is almost always better to make a sensible comparison method instead of using memcmp(). Using memcmp() is inherently non-portable, creates nonsensical orderings and probably will not create any real savings anyway.
But YMMV.
I suppose that there is a time for it, if desperate times call for desperate measures as revealed by a profile. I would definitely never do it that way myself for an opening book comparison method but that is neither here nor there. I would fail that practice on almost any code review we have here (but we code in C++ across multiple platforms so using memcmp() for ordering is simply broken beyond repair for our uses [*]).

[*] -- cough -- there is one place in our code where I use it for a server side equality compare under a specific operating system for specific data types. So, "Never say 'never'." Drats. I just said it.
Cardoso
Posts: 362
Joined: Thu Mar 16, 2006 7:39 pm
Location: Portugal
Full name: Alvaro Cardoso

Re: c/c++ question: comparing/sorting sctructs

Post by Cardoso »

Bo Persson wrote:
Cardoso wrote:Thank you all for your answers.
Now please consider the special case of a struct with no pointers in it and the compiler switch to padding with 1 byte turned on.
BTW does padding with 1 byte mean no padding bytes at all? The sizeof(structname) would be what we see in the code with no hidden bytes?.
Now, would memcmp work with no problems at all?
If that is the case then I would have my opening book struct array problem solved.

Thanks,
Alvaro
Yes, that would probably work.

However, if the compiler would otherwise chose padding on 4 byte boundaries, it does that for a reason. Most often that reason is the performance of all other operations on the members, like assignment or increment.

Do you want memcmp to be fast, or everyting else? :-)
Well, my obsession for memcmp is that it would allow me to sort an array of structs so that Î can do a binary search on that array looking for a board position.

Alvaro
User avatar
Bo Persson
Posts: 243
Joined: Sat Mar 11, 2006 8:31 am
Location: Malmö, Sweden
Full name: Bo Persson

Re: c/c++ question: comparing/sorting sctructs

Post by Bo Persson »

Cardoso wrote:
Bo Persson wrote:
Cardoso wrote:Thank you all for your answers.
Now please consider the special case of a struct with no pointers in it and the compiler switch to padding with 1 byte turned on.
BTW does padding with 1 byte mean no padding bytes at all? The sizeof(structname) would be what we see in the code with no hidden bytes?.
Now, would memcmp work with no problems at all?
If that is the case then I would have my opening book struct array problem solved.

Thanks,
Alvaro
Yes, that would probably work.

However, if the compiler would otherwise chose padding on 4 byte boundaries, it does that for a reason. Most often that reason is the performance of all other operations on the members, like assignment or increment.

Do you want memcmp to be fast, or everyting else? :-)
Well, my obsession for memcmp is that it would allow me to sort an array of structs so that Î can do a binary search on that array looking for a board position.

Alvaro
Yes, I can see that. :-)

The thing is that

1. If and when that is a good idea, the compiler might do that for you.
2. It is unlikely that your program spends any significant time in the opening book.

We have had numerous discussions here about bit twiddling techniques. Sometimes, the best alternative was to write the code as simply as possible, because then the compiler's optimizer actually understood the intention, and could generate minimal code. The trickiest code always runs the risk that the compiler doesn't understand it, and has to avoid some optimizations.

Save your low level tricks until you know that they are needed. Perhaps you can spend the time saved considering a hashed opening book, where you can avoid the binary search alltogther?
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: c/c++ question: comparing/sorting sctructs

Post by mcostalba »

trojanfoe wrote:
Bo Persson wrote:
trojanfoe wrote:
mcostalba wrote:memcmp is a dirty trick to say at least, don't use it, is not portable, is compiler dependent, is endian dependent, is probably broken and most impostant is unnecessary for you.
:shock: rubbish
True, it is not endian dependent. :-)
No, it was none of things stated :P
I promise to myself not to answer...but I failed to keep that promise :)

As I have _already_ explained in a previous post, is not memcmp that is broken.

It is broken the USE OF memcmp to compare structs. This is becuase the same struct can be rapresented in RAM in differtent ways according to the compiler, the OS and, yes, also the endianess: a struct created on a MAC and saved on disk and then read back in a x86 will be stored differently in memory.
trojanfoe

Re: c/c++ question: comparing/sorting sctructs

Post by trojanfoe »

mcostalba wrote:
trojanfoe wrote:
Bo Persson wrote:
trojanfoe wrote:
mcostalba wrote:memcmp is a dirty trick to say at least, don't use it, is not portable, is compiler dependent, is endian dependent, is probably broken and most impostant is unnecessary for you.
:shock: rubbish
True, it is not endian dependent. :-)
No, it was none of things stated :P
I promise to myself not to answer...but I failed to keep that promise :)

As I have _already_ explained in a previous post, is not memcmp that is broken.

It is broken the USE OF memcmp to compare structs. This is becuase the same struct can be rapresented in RAM in differtent ways according to the compiler, the OS and, yes, also the endianess: a struct created on a MAC and saved on disk and then read back in a x86 will be stored differently in memory.
LOL :lol: