将n char *复制到另一个内部

时间:2012-10-23 19:36:16

标签: c++ copy

我将来自SQL SERVER colomn的UUID值(通过Qt Sql模块)绑定到QUuid对象中。 之后,我必须使用一个以char *作为参数的assign函数将它分配到一个内部uuid对象。

QUuid将几个char *公开为公共成员,因此我必须使用几个char * QUuid的成员构建一个16字节的char *。

即复制:

char buff0;
char buff1;
char buff2[2];
char buff3[4];
char buff4[8];

char final[16];

我使用memcpy来完成这样的任务:

int accu = 0;    
memcpy(final, &buff0, sizeof(buff0)); 
accu += sizeof(buff1);
memcpy(final+accu, &buff1, sizeof(buff1));
accu += sizeof(buff2);
memcpy(final+accu, buff2, sizeof(buff2));
accu += sizeof(buff3);
memcpy(final+accu, buff3, sizeof(buff3));
accu += sizeof(buff4);
memcpy(final+accu, buff4, sizeof(buff4));

但我发现这种方式并不真正可读和可维护。 我正在寻找一种更优雅的方式来完成这项任务。优雅,我的意思是减少代码行和/或更少的算术。

5 个答案:

答案 0 :(得分:2)

一种选择是使用联合,以便您可以互换地使用任一定义。

答案 1 :(得分:1)

使用x86程序集

;; FASM/NASM syntax
MOV    esi, final
MOVZX  eax, byte [buf0]
MOVZX  ebx, byte [buf1]
MOVZX  ecx, word [buf2]
MOV    edx, dword [buf3] 
MOV    byte [esi], al
MOV    byte [esi+1], bl
MOV    eax, dword [buf4]
MOV    ebx, dword [buf4+4]
MOV    word [esi+2], cx
MOV    dword [esi+4], edx
MOV    dword [esi+8], eax
MOV    dword [esi+12], ebx

或者如何粗略地假设buf的所有内存都是线性分配的(没有填充)并只使用一个memcpy

memcpy(final, buf0, 16)

答案 2 :(得分:1)

memcpy(&final[0], &buff0, 1);
memcpy(&final[1], &buff1, 1);
memcpy(&final[2], &buff2[0], 2);
memcpy(&final[4], &buff3[0], 4);
memcpy(&final[8], &buff4[0], 8);

答案 3 :(得分:1)

首先,下面是一个C ++ 2011解决方案,它创建了一个静态检查的copy()函数:它接受charchar的数组作为参数,并且需要char的数量{添加{1}}以填充要传递的第一个数组。当然,可以根据需要删除静态检查。原版有一些拼写错误和遗漏。

#include <algorithm>
#include <iostream>
#include <iterator>

int constexpr size(char const&) { return 1; }
template <int Size>
int constexpr size(char const(&)[Size]) { return Size; }
template <typename T0, typename... T>
int constexpr size(T0 const& arg0, T const&... args) {
    return size(arg0) + size(args...);
}

char* copy_intern(char* to, char c) { *to = c; return ++to; }
template <int Size>
char* copy_intern(char* to, char const (&array)[Size]) {
    return std::copy(array, array + Size, to);
}

template <typename T0, typename... T>
char* copy_intern(char* to, T0 const& arg0, T const&... args) {
    return copy_intern(copy_intern(to, arg0), args...);
}

template <int Total, typename... T>
void copy(char (&to)[Total], T const&... args)
{
    static_assert(Total == size(args...), "wrong argument size");
    copy_intern(to, args...);
}

int main()
{
    char buff0    = 'a';
    char buff1    = 'b';
    char buff2[2] = { 'c', 'd' };
    char buff3[4] = { 'e', 'f', 'g', 'h' };
    char buff4[8] = { 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p' };
    char final[16];

    copy(final, buff0, buff1, buff2, buff3, buff4);
    *std::copy(final, final + 16,
               std::ostreambuf_iterator<char>(std::cout)) = '\n';
}

请注意,前两个copy_intern()函数也可以在C ++ 2003中使用,以推断出参数的类型和大小。也就是说,通过这些功能,可能会重新命名为合适的功能,您可以获得至少自动获取大小的功能:

    char* tmp = final;
    tmp = copy_intern(tmp, buff0);
    tmp = copy_intern(tmp, buff1);
    tmp = copy_intern(tmp, buff2);
    tmp = copy_intern(tmp, buff3);
    tmp = copy_intern(tmp, buff4);

答案 4 :(得分:1)

这是C ++ 11 STL版本。

char* out = final;
out = std::copy_n(&buff0, 1, out);
out = std::copy_n(&buff1, 1, out);
out = std::copy_n(buff2, 2, out);
out = std::copy_n(buff3, 4, out);
out = std::copy_n(buff4, 8, out);