Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > memcmp for <

Reply
Thread Tools

memcmp for <

 
 
rajkumar@hotmail.com
Guest
Posts: n/a
 
      03-23-2005
I have a struct like

struct MyStruct
{
int a;
int b;
int c:
bool d;
bool e;
}

I want to insert such a struct in a map. I understand I can declare the
< operator for such a struct for lexicographical compare like

x.a < y.a || !(y.a < x.a) && ( x.b < y.b || !(y.b < x.b) && x.c < y.c
.............

or a simple one like this

memcmp(&x,&y,sizeof(MyStruct)) < 0;

This seems to work if I memset and fill the sizeof(MyStruct) with
zeroes in the constructor before I assign a b and c etc. This will take
care of any padding that the compiler adds.

My question is whether the second approach is portable? If so do I
really need the memset ? Does the standard say anything about
initializing the padding bits?

Raj

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-23-2005
wrote:
> I have a struct like
>
> struct MyStruct
> {
> int a;
> int b;
> int c:
> bool d;
> bool e;
> }
>
> I want to insert such a struct in a map. I understand I can declare the
> < operator for such a struct for lexicographical compare like
>
> x.a < y.a || !(y.a < x.a) && ( x.b < y.b || !(y.b < x.b) && x.c < y.c
> ............
>
> or a simple one like this
>
> memcmp(&x,&y,sizeof(MyStruct)) < 0;
>
> This seems to work if I memset and fill the sizeof(MyStruct) with
> zeroes in the constructor before I assign a b and c etc. This will take
> care of any padding that the compiler adds.
>
> My question is whether the second approach is portable? If so do I
> really need the memset ? Does the standard say anything about
> initializing the padding bits?


1) Yes. 2) Yes. 3) They are uninitialised unless your object has static
storage duration. Beware, though that as soon as your MyStruct ceases
being a POD (because you added a private section or a virtual function or
something of the sort), use of memset and memcpy on it becomes undefined.

V
 
Reply With Quote
 
 
 
 
Malte Starostik
Guest
Posts: n/a
 
      03-23-2005
schrieb:
> I have a struct like
>
> struct MyStruct
> {
> int a;
> int b;
> int c:
> bool d;
> bool e;
> }
>
> I want to insert such a struct in a map. I understand I can declare the
> < operator for such a struct for lexicographical compare like
>
> x.a < y.a || !(y.a < x.a) && ( x.b < y.b || !(y.b < x.b) && x.c < y.c
> ............
>
> or a simple one like this
>
> memcmp(&x,&y,sizeof(MyStruct)) < 0;
>
> This seems to work if I memset and fill the sizeof(MyStruct) with
> zeroes in the constructor before I assign a b and c etc. This will take
> care of any padding that the compiler adds.
>
> My question is whether the second approach is portable? If so do I
> really need the memset ? Does the standard say anything about
> initializing the padding bits?


Sorry, can't tell you about the padding bits, but it's still not
portable because of endianess issues:

struct Foo
{
int a;
};

Foo f1 = { 1 };
Foo f2 = { 256 };

On big-endian machines a memcmp() compare will work correctly. On a
little-endian machine with 32-bit ints, f1 will contain the byte
sequence 0x01 0x00 0x00 0x00 (minus padding) and f2 will contain 0x00
0x01 0x00 0x00. memcmp() will report f1 as greater than f2.

Cheers,
Malte
 
Reply With Quote
 
rajkumar@hotmail.com
Guest
Posts: n/a
 
      03-23-2005
You mentioned something about private section. Could you elaborate how
that would change things ?

If the struct carried a vtable pointer or had NON POD could i just
overload new and memset before i call the constructor ?

Raj

 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      03-23-2005
<> wrote in message
news: oups.com...
>I have a struct like
>
> struct MyStruct
> {
> int a;
> int b;
> int c:
> bool d;
> bool e;
> }
>
> I want to insert such a struct in a map. I understand I can declare the
> < operator for such a struct for lexicographical compare like
>
> x.a < y.a || !(y.a < x.a) && ( x.b < y.b || !(y.b < x.b) && x.c < y.c


Note that you can also use the (IMO better) following form:
return (x.a!=y.a) ? x.a<y.a
: (x.b!=y.b) ? x.b<y.b
: (x.c!=y.c) ? x.c<y.c
: (x.d!=y.d) ? x.d<y.d : x.e < y.e;

> or a simple one like this
>
> memcmp(&x,&y,sizeof(MyStruct)) < 0;
>
> This seems to work if I memset and fill the sizeof(MyStruct) with
> zeroes in the constructor before I assign a b and c etc. This will take
> care of any padding that the compiler adds.


But not of endianness and other binary representation issues.
Really, I don't think that saving a few statements is worth the
loss of portability. Plus the explicit form gives you much more
flexibility. So why bother?


--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-23-2005
wrote:
> You mentioned something about private section. Could you elaborate how
> that would change things ?


The layout of an object is only mandated within the same access specifier
section. So, as soon as you introduce private or protected non-static
data members, the struct is not a POD any more, and I am not really sure
why that is, but the Standard makes a point of defining POD-struct that
way.

> If the struct carried a vtable pointer or had NON POD could i just
> overload new and memset before i call the constructor ?


I am not sure what you mean by "overload memset", but yes, essentially,
your task would be to gain control over the "padding bytes" by, for
example, eliminating them using compiler-specific means.

Let me ask a rhetorical questions, though. If you are prepared to give it
overloaded 'new' and 'memset' (let's suppose it's possible somehow), why
don't you just overload the operator < ?

V
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-23-2005
Malte Starostik wrote:
> [...]
> Sorry, can't tell you about the padding bits, but it's still not
> portable because of endianess issues:
>
> struct Foo
> {
> int a;
> };
>
> Foo f1 = { 1 };
> Foo f2 = { 256 };
>
> On big-endian machines a memcmp() compare will work correctly. On a
> little-endian machine with 32-bit ints, f1 will contain the byte
> sequence 0x01 0x00 0x00 0x00 (minus padding) and f2 will contain 0x00
> 0x01 0x00 0x00. memcmp() will report f1 as greater than f2.


But won't it report f1 consistently greater than f2? The purpose of
using memcmp (as I understood it) was to forgo the real operator < and
the memberwise comparison just to see if they were different.

V
 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      03-23-2005
"Victor Bazarov" <> wrote in message
news:N_f0e.55648$ o.verio.net...

>> My question is whether the second approach is portable? If so do I
>> really need the memset ? Does the standard say anything about
>> initializing the padding bits?

>
> 1) Yes. 2) Yes. 3) They are uninitialised unless your object has static
> storage duration.


Beg pardon? Memcmp portable? I don't see why. As a simple example, I
can't think of any place in the standard that requires all equal bool values
to have the same representation. In other words, I don't see anything wrong
with an implementation that stores a byte in a bool and considers zero to be
false and any nonzero value to be true. Under such an implementation,
memcmp might yield unequal for two values that should be considered equal.


 
Reply With Quote
 
rajkumar@hotmail.com
Guest
Posts: n/a
 
      03-23-2005
I dont care about that as I want just keep them in a set. If A < B I
just want to make sure A < B all the time

Raj

 
Reply With Quote
 
rajkumar@hotmail.com
Guest
Posts: n/a
 
      03-23-2005
>Let me ask a rhetorical questions, though. If you are prepared to
give it
>overloaded 'new' and 'memset' (let's suppose it's possible somehow),

why
>don't you just overload the operator < ?


Its some legacy code. The idea being if you add a new member it will
work automatically. If you overload <
you will have to manually update it for the new member

Raj

 
Reply With Quote
 
 
 
Reply

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
memcmp() semantics Sidney Cadot C Programming 6 11-25-2003 07:20 AM
Re: memcmp versus strstr; reaction to chr(0) Burne C C Programming 3 07-25-2003 06:21 PM
Re: memcmp versus strstr; reaction to chr(0) Dan Pop C Programming 0 07-24-2003 05:51 PM
Re: memcmp versus strstr; reaction to chr(0) Thomas Matthews C Programming 0 07-24-2003 02:34 PM
Re: memcmp versus strstr; reaction to chr(0) Joona I Palaste C Programming 0 07-24-2003 10:37 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57