memcpy从char数组转换为struct

时间:2015-11-11 03:07:19

标签: c++ c arrays memcpy

我有一个char buffer[1024]和一个结构

typedef struct {
   int record;
   int key;
} leaf_entry;

我想要实现的是缓冲区数组,它像树的节点一样,包含许多leaf_entry条目。

如果我想迭代缓冲区以将缓冲区中的条目与另一条目进行比较

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...
}

这是对的吗?有没有更简单/更有效的方法来实现这一目标?

2 个答案:

答案 0 :(得分:0)

如果你的结构是POD,你应该可以不复制地执行此操作。

我相信像这样的东西应该在offset是char缓冲区中正确的字节偏移量的情况下工作:

leaf_entry & x = static_cast<leaf_entry &>(buffer + offset);

答案 1 :(得分:0)

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

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

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;
}

如果将数组中两个元素的地址传递给这样的函数,则可以比较记录或键。

例如将元素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);
    }
}

<强>输出

----- 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