Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
memcpy混淆了struct的内容?_C_File_Io_Memcpy - Fatal编程技术网

memcpy混淆了struct的内容?

memcpy混淆了struct的内容?,c,file,io,memcpy,C,File,Io,Memcpy,我一直在编写R2K对象模块,但在将符号表条目写入文件时遇到了问题。我一直在尝试使用memcpy将存储在sym_表中的symbol表的条目放入一个名为bytes_sym的单字节整数数组中,然后将其写入文件。它复制正确的大小,但由于某种原因使字节的位置混乱。这是我的密码: /* ** symbol table entry */ typedef struct syment { uint32_t flags; /* flag word */ uint32_t value

我一直在编写R2K对象模块,但在将符号表条目写入文件时遇到了问题。我一直在尝试使用memcpy将存储在sym_表中的symbol表的条目放入一个名为bytes_sym的单字节整数数组中,然后将其写入文件。它复制正确的大小,但由于某种原因使字节的位置混乱。这是我的密码:

/*
** symbol table entry
*/    
typedef
struct syment {
    uint32_t flags;      /* flag word */
    uint32_t value;      /* value associated with this symbol */
    uint32_t sym;        /* symbol name's index into string table */
}
    syment_t;

// header->data[8] is the number of symbol table entries
int sym_length = header->data[8] * sizeof(syment_t);

uint8_t bytes_sym[sym_length];

for(int i = 0; i < header->data[8]; i++){
    memcpy(&bytes_sym[i * sizeof(syment_t)], &sym_table[i], sizeof(syment_t));
}
fwrite(bytes_sym, sym_length, 1, file);

// prints the newly copied symbol table section one byte at a time
// I know it's gross to look at, but it's only for testing :p
printf("New Symtab:\n");
for(int i = 0; i < sym_length; i++){
    printf("0x%x ", bytes_sym[i]);
}
printf("\n");
写完后,它们是(不正确,不应该不同):


我就是不知道是什么导致了这一切,所以任何帮助都将不胜感激

好吧,你们这些家伙完全在撒谎,说他们没有弄乱我的数据。在做了一点研究之后,我发现在我的32位对象模块的symbol table部分中,每个4字节块中的字节顺序是颠倒的,这是一个endian。所以我写了一个小函数来修复它:

///    Flips the bytes in each 4 byte block after the endian flip caused by memcpy
///    @param - the array of bytes
///    @param - the length of the array in bytes
///
void flip_bytes(uint8_t byte_array[], int length){

    uint8_t tmp;
    for(int i = 0; i < length; i++){
            if((i+1) % 4 == 0){

                    // switch the first and last bytes
                    tmp = byte_array[i];
                    byte_array[i] = byte_array[i-3];
                    byte_array[i-3] = tmp;
                    // switch the middle 2 bytes
                    tmp = byte_array[i-1];
                    byte_array[i-1] = byte_array[i-2];
                    byte_array[i-2] = tmp;
            }
    }
}
///在memcpy导致的endian翻转之后,翻转每个4字节块中的字节
///@param-字节数组
///@param-数组的长度(以字节为单位)
///
无效翻转字节(uint8字节数组[],整数长度){
uint8_t tmp;
for(int i=0;i
欢迎来到堆栈溢出。请尽快阅读这两页。数据结构是什么样子的?我们需要知道这一点。还请阅读有关如何创建MCVE()的内容。那么您想看看代码的编写和数据的读取吗?同样使
0x%x
a
0x%02x
。看起来像是一个大端对小端的问题。我们需要更多的信息,特别是a会有帮助,但是
memcpy
绝对不会混淆任何东西。问题出在别处。我的理解是这些值被正确复制了。这只是你阅读它们打印的方式是导致问题的原因。你能在memcopy之前显示打印代码吗?另外,哪一个是正确的?您需要实际提供一个以确保查看,但更可能的情况是您没有一致地打印数据。就其本身而言,正确使用而言,
memcpy()
不会弄乱任何事情。你没有提供足够的信息让任何人知道你的问题是什么;您在本“回答”中未提供足够的信息来说明您在何处以及如何使用此功能;你关于“完全撒谎”的说法不符合规程。您可能有调用未定义行为的代码。很难确定,因为您没有提供,但不稳定的行为是由不稳定的编码引起的。正确使用时(例如,内存的源区域和目标区域可能不会重叠),
memcpy()
会起作用。对这个答案投反对票,因为它具有误导性。认为
memcpy
是endian感知的,并以不同的顺序将字节写入源代码,这显然是不正确的。您可能会遇到的是,源数据本身使用了与预期不同的Endiance。这是一个数据格式问题,与
memcpy
无关。您不应该消极地接受此反馈。若答案包含错误信息,那个么它可能会对未来的读者造成潜在的伤害,应该指出这一点。
0xb1 0x0 0x0 0x0 0x2c 0x0 0x40 0x0 0x0 0x0 0x0 0x0 
0xa3 0x0 0x0 0x0 0x20 0x0 0x0 0x10 0x5 0x0 0x0 0x0 
0xb1 0x40 0x0 0x0 0x38 0x0 0x40 0x0 0xb 0x0 0x0 0x0 
0xa1 0x0 0x0 0x0 0x14 0x0 0x40 0x0 0x10 0x0 0x0 0x0 
0xb1 0x40 0x0 0x0 0x0 0x0 0x40 0x0 0x15 0x0 0x0 0x0 
0x67 0x0 0x0 0x0 0x11 0x0 0x0 0x0 0x1f 0x0 0x0 0x0 
0xa2 0x0 0x0 0x0 0x0 0x0 0x0 0x10 0x19 0x0 0x0 0x0 
0xb1 0x40 0x0 0x0 0x64 0x0 0x40 0x0 0x29 0x0 0x0 0x0
///    Flips the bytes in each 4 byte block after the endian flip caused by memcpy
///    @param - the array of bytes
///    @param - the length of the array in bytes
///
void flip_bytes(uint8_t byte_array[], int length){

    uint8_t tmp;
    for(int i = 0; i < length; i++){
            if((i+1) % 4 == 0){

                    // switch the first and last bytes
                    tmp = byte_array[i];
                    byte_array[i] = byte_array[i-3];
                    byte_array[i-3] = tmp;
                    // switch the middle 2 bytes
                    tmp = byte_array[i-1];
                    byte_array[i-1] = byte_array[i-2];
                    byte_array[i-2] = tmp;
            }
    }
}