C++ memcpy从char数组转换为struct

C++ memcpy从char数组转换为struct,c++,c,arrays,memcpy,C++,C,Arrays,Memcpy,我有一个字符缓冲区[1024]和一个结构 typedef struct { int record; int key; } leaf_entry; 我试图实现的是缓冲区数组,它的作用类似于树的一个节点,树中包含了许多leaf_条目 如果我想遍历缓冲区,将缓冲区中的一个条目与另一个条目进行比较 for (i = 0; i < max_ents * entry_size; i += entry_size) { leaf_entry curr; memcpy (cur

我有一个
字符缓冲区[1024]
和一个结构

typedef struct {
   int record;
   int key;
} leaf_entry;
我试图实现的是缓冲区数组,它的作用类似于树的一个节点,树中包含了许多leaf_条目

如果我想遍历缓冲区,将缓冲区中的一个条目与另一个条目进行比较

for (i = 0; i < max_ents * entry_size; i += entry_size)
{
    leaf_entry curr;
    memcpy (curr, buffer[i], entry_size)
    if (curr == entry_to_compare)
       etc...
}
for(i=0;i

这是正确的吗?是否有更简单/更有效的方法来实现这一点

鉴于您的结构是POD,您应该能够在不复制的情况下执行此操作

我认为,在offset是进入char缓冲区的正确字节偏移量的情况下,应该可以使用类似的方法:

leaf_entry & x = static_cast<leaf_entry &>(buffer + offset);
leaf_entry&x=静态_cast(缓冲区+偏移量);

>P>我会考虑使用标准排序实现使用的方法,即使用用户定义的比较函数来接收2个指针。然后使用这些指针定位元素,并从中访问和比较感兴趣的成员。这也避免了不必要的内存复制

考虑以下用于比较整数的func:

int compareIntAsc(const void *int1, const void *int2)
{
    int *num1 = (int*)int1;
    int *num2 = (int*)int2;
    return *num1 - *num2;
}

现在考虑一个,它将比较一些结构的计数成员

int compareNodeCountAsc(const void *node1, const void *node1)
{
    return (pHuffmanNode)(node1))->charCount - ((pHuffmanNode)(node2))->charCount;
}
如果将数组中两个元素的地址传递给这样的函数,则可以比较记录或键

e、 g将元素0与其他元素进行比较

代码

typedef struct {
   int record;
   int key;
} leaf_entry, *pLeafEntry;

int compLeafKeyAsc(void *leaf1, void *leaf2)
{
    leaf_entry *p1, *p2;
    p1 = (leaf_entry *)leaf1;
    p2 = (leaf_entry *)leaf2;
    return p1->key - p2->key;
}

void printLeaf(pLeafEntry leaf)
{
    printf("----- leaf -------\n");
    printf("record: %d\n", leaf->record);
    printf("key: %d\n", leaf->key);
}

void demo()
{


    const int nElems = 16;
    leaf_entry leafArray[nElems];
    pLeafEntry firstElement;

    int i;
    for (i=0;i<nElems;i++)
    {
        leafArray[i].record = (rand()%51) + 100;        // record is [100..150]
        leafArray[i].key = (rand()%128);                // key is [0..127]
        printLeaf(&leafArray[i]);
    }

    //e.g  compare element 0 to every other element
    firstElement = &leafArray[0];
    for (i=1; i<nElems; i++)
    {
        printf("%d", firstElement->key );
        int result = compLeafKeyAsc(firstElement, &leafArray[i]);
        if (result < 0)
            printf(" is less than ");
        else if (result > 0)
            printf(" is greater than ");
        else
            printf(" is equal to ");
        printf("%d\n", leafArray[i].key);
    }
}

为什么使用char缓冲区[]而不是struct leaf\u entry缓冲区[]?为什么不使用
leaf\u entry
数组?为什么
typedef
struct?char buffer[]是因为我项目中的规范。我们正在将一页从磁盘“加载”到1024字节的主内存中。然后在那个页面中,我们想用2个int组成的条目填充它。@ JasonWoo你把它标记为代码> C++ +/COD>,在C++中,你不能完全隐藏MimcPy结构。如果您的结构是
非POD
,那么您的示例将生成未定义的行为。在你的结构中粘贴一个
std::string
,memcpy就会变成一场灾难。在这种情况下,你可能会侥幸逃脱,因为这个结构只是简单的数据类型。不要在包含对象或指针的结构中尝试这样做。也不要对一个对象这样做。他是如何生成这个“偏移量”的?由于数据对齐而崩溃的可能性。在配备现代处理器的PC或嵌入式系统上,您可能只会受到性能损失。@Nandu offset=n*sizeof(leaf_struct)@user4581301您是对的。也许,由于缓冲区包含所有int,因此可以正确对齐以避免出现这种情况。但在将数据写入磁盘时,页面中的数据不是这样序列化的。他把所有的东西都读回字节。您如何知道这是正确的顺序?请勿使用C样式转换。使用
static\u cast
。如果这还不能编译,那么再想想你在做什么。很好,最近写了很多汇编。
----- leaf -------
record: 141
key: 35
----- leaf -------
record: 110
key: 4
----- leaf -------
record: 144
key: 108
----- leaf -------
record: 103
key: 46
----- leaf -------
record: 134
key: 16
----- leaf -------
record: 144
key: 113
----- leaf -------
record: 125
key: 59
----- leaf -------
record: 116
key: 107
----- leaf -------
record: 137
key: 38
----- leaf -------
record: 133
key: 60
----- leaf -------
record: 106
key: 12
----- leaf -------
record: 126
key: 25
----- leaf -------
record: 137
key: 94
----- leaf -------
record: 130
key: 28
----- leaf -------
record: 132
key: 55
----- leaf -------
record: 141
key: 94
35 is greater than 4
35 is less than 108
35 is less than 46
35 is greater than 16
35 is less than 113
35 is less than 59
35 is less than 107
35 is less than 38
35 is less than 60
35 is greater than 12
35 is greater than 25
35 is less than 94
35 is greater than 28
35 is less than 55
35 is less than 94