memcmp for< [英] memcmp for <

查看:125
本文介绍了memcmp for<的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的结构


struct MyStruct

{

int a;

int b;

int c:

bool d;

bool e;

}


我想在地图中插入这样的结构。我明白我可以声明

<用于字典比较的这种结构的运算符如


x.a< y.a || !(y.a< x.a)&& (xb< yb ||!(yb< xb)&& xc< yc

.............

或这样一个简单的


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


这似乎有效,如果我在构造函数中使用

零填充sizeof(MyStruct),然后再分配ab和c等。这将需要

关注编译器添加的任何填充。


我的问题是第二种方法是否可移植?如果是这样的话,

真的需要memset吗?标准说什么

初始化填充位?


Raj

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

推荐答案

ra******@hotmail.com 写道:
我有一个结构如

struct MyStruct
{
int a;
int b;
int c:
bool d;
bool e;
}
我想在地图中插入这样的结构。我理解我可以声明
<用于词典比较的这种结构的运算符如

x.a< y.a || !(y.a< x.a)&& (xb< yb ||!(yb< xb)&& xc< yc
............

或简单的像这样

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

这似乎可以工作,如果我memset并填充sizeof(MyStruct在我分配ab和c等之前,在构造函数中使用了零。这将需要关注编译器添加的任何填充。

我的问题是第二种方法是否是便携式?如果是这样的话我是否真的需要memset?标准是否说了什么来初始化填充位?
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)是的。 2)是的。 3)除非你的物品具有静态的存储时间,否则它们是未初始化的。请注意,尽管只要你的MyStruct停止了

是一个POD(因为你添加了私人部分或虚拟功能或

某种东西),使用memset并且它上面的memcpy变得不确定。


V



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


ra ****** @ hotmail.com schrieb:
我有一个结构如

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

我想要在地图中插入这样的结构。我明白我可以宣布
<用于词典比较的这种结构的运算符如

x.a< y.a || !(y.a< x.a)&& (xb< yb ||!(yb< xb)&& xc< yc
............

或简单的像这样

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

这似乎可以工作,如果我memset并填充sizeof(MyStruct在我分配ab和c等之前,在构造函数中使用了零。这将需要关注编译器添加的任何填充。

我的问题是第二种方法是否是便携式?如果是这样的话我是否真的需要memset?标准是否说了什么来初始化填充位?
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?




抱歉,可以'不要告诉你关于填充位的信息,但由于字节顺序问题,它仍然不是可移植的:b / b
struct Foo

{

int a;

};


Foo f1 = {1};

Foo f2 = {256};


在big-endian机器上,memcmp()比较可以正常工作。

小端机器,32 -bit整数,f1 wi ll包含字节

序列0x01 0x00 0x00 0x00(减去填充),f2将包含0x00

0x01 0x00 0x00。 memcmp()将报告f1大于f2。


干杯,

Malte



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


你提到了私人部分。你能详细说明如何改变东西吗?


如果结构带有vtable指针或者有非POD我可以只是

在调用构造函数之前重载new和memset?


Raj

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


这篇关于memcmp for&lt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆