Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.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
C编码::将结构写入内存块 我正在做一个必须用标准C(不C++)编码的项目,不久前,我编写了下面的程序,把不同结构的内容写入二进制文件: #include <stdio.h> #include <stdlib.h> typedef struct structA{ int a, b, c; }AAA; typedef struct structB{ int a, b, c, d, e; }BBB; int main( int argc, char **argv ){ // Create and open the output file FILE *fd = fopen("test.txt", "w"); AAA* a1 = (AAA*)malloc( sizeof(AAA) ); AAA* a2 = (AAA*)malloc( sizeof(AAA) ); BBB* b1 = (BBB*)malloc( sizeof(BBB) ); a1->a = 1; a1->b = 2; a1->c = 3; a2->a = 4; a2->b = 5; a2->c = 6; b1->a = 10; b1->b = 20; b1->c = 30; b1->d = 40; b1->e = 50; // Write all these structs to the file: fwrite((char*)&a1, sizeof(AAA), 1, fd); fwrite((char*)&a2, sizeof(AAA), 1, fd); fwrite((char*)&b1, sizeof(BBB), 1, fd); // Close the file fclose( fd ); free( a1 ); free( a2 ); free( b1 ); printf("END OF PROGRAM.\n"); return 0; }_C_Heap Memory_Memcpy - Fatal编程技术网

C编码::将结构写入内存块 我正在做一个必须用标准C(不C++)编码的项目,不久前,我编写了下面的程序,把不同结构的内容写入二进制文件: #include <stdio.h> #include <stdlib.h> typedef struct structA{ int a, b, c; }AAA; typedef struct structB{ int a, b, c, d, e; }BBB; int main( int argc, char **argv ){ // Create and open the output file FILE *fd = fopen("test.txt", "w"); AAA* a1 = (AAA*)malloc( sizeof(AAA) ); AAA* a2 = (AAA*)malloc( sizeof(AAA) ); BBB* b1 = (BBB*)malloc( sizeof(BBB) ); a1->a = 1; a1->b = 2; a1->c = 3; a2->a = 4; a2->b = 5; a2->c = 6; b1->a = 10; b1->b = 20; b1->c = 30; b1->d = 40; b1->e = 50; // Write all these structs to the file: fwrite((char*)&a1, sizeof(AAA), 1, fd); fwrite((char*)&a2, sizeof(AAA), 1, fd); fwrite((char*)&b1, sizeof(BBB), 1, fd); // Close the file fclose( fd ); free( a1 ); free( a2 ); free( b1 ); printf("END OF PROGRAM.\n"); return 0; }

C编码::将结构写入内存块 我正在做一个必须用标准C(不C++)编码的项目,不久前,我编写了下面的程序,把不同结构的内容写入二进制文件: #include <stdio.h> #include <stdlib.h> typedef struct structA{ int a, b, c; }AAA; typedef struct structB{ int a, b, c, d, e; }BBB; int main( int argc, char **argv ){ // Create and open the output file FILE *fd = fopen("test.txt", "w"); AAA* a1 = (AAA*)malloc( sizeof(AAA) ); AAA* a2 = (AAA*)malloc( sizeof(AAA) ); BBB* b1 = (BBB*)malloc( sizeof(BBB) ); a1->a = 1; a1->b = 2; a1->c = 3; a2->a = 4; a2->b = 5; a2->c = 6; b1->a = 10; b1->b = 20; b1->c = 30; b1->d = 40; b1->e = 50; // Write all these structs to the file: fwrite((char*)&a1, sizeof(AAA), 1, fd); fwrite((char*)&a2, sizeof(AAA), 1, fd); fwrite((char*)&b1, sizeof(BBB), 1, fd); // Close the file fclose( fd ); free( a1 ); free( a2 ); free( b1 ); printf("END OF PROGRAM.\n"); return 0; },c,heap-memory,memcpy,C,Heap Memory,Memcpy,我有另一个程序可以读取这个文件并从结构中提取所有信息。所以我知道上面的代码给出了我想要的 但是现在,我需要将这些结构写入分配的内存块,而不是文件。我想这很容易: #include <stdio.h> #include <stdlib.h> typedef struct structA{ int a, b, c; }AAA; typedef struct structB{ int a, b, c, d, e; }BBB;

我有另一个程序可以读取这个文件并从结构中提取所有信息。所以我知道上面的代码给出了我想要的

但是现在,我需要将这些结构写入分配的内存块,而不是文件。我想这很容易:

#include <stdio.h>
#include <stdlib.h>
    
typedef struct structA{
        int a, b, c;
}AAA;
typedef struct structB{
        int a, b, c, d, e;
}BBB;
    
int main( int argc, char **argv ){

        u_char* BlockOfMemory = (u_char*) malloc( sizeof(u_char) * 100 );

        AAA* a1 = (AAA*)malloc( sizeof(AAA) );
        AAA* a2 = (AAA*)malloc( sizeof(AAA) );
        BBB* b1 = (BBB*)malloc( sizeof(BBB) );

        a1->a = 1;   a1->b = 2;   a1->c = 3;
        a2->a = 4;   a2->b = 5;   a2->c = 6;
        b1->a = 10;  b1->b = 20;  b1->c = 30;  b1->d = 40;  b1->e = 50;

        // Write all these structs into BlockOfMemory:
        memcpy ( BlockOfMemory, &a1, sizeof( AAA ) );
        memcpy ( (BlockOfMemory+sizeof(AAA)), &a2, sizeof( AAA ) );
        memcpy ( (BlockOfMemory+sizeof(AAA)+sizeof(AAA)), &b1, sizeof( BBB ) );

        printf("==>  %hhn\n", BlockOfMemory);

        free( a1 );
        free( a2 );
        free( b1 );
        free( BlockOfMemory );

        printf("END OF PROGRAM.\n");
        return 0;
}
这里的目标是内存块必须包含与二进制文件完全相同的信息。我的代码编译和运行的情况很奇怪,但考虑到我拥有的工具(VI和GCC),我无法验证我的代码是否正确或偏离了标准

有人能提供建议吗?另外,这里使用的函数是
memcpy()
?多谢各位


编辑:修复了由于剪切n粘贴错误而错误添加第二个“free(b1);”时的第一个程序。

如前所述,您正在将指针地址
a1
传递给memcpy,与
a2
b1
相同。应该是

memcpy ( BlockOfMemory, a1, sizeof( AAA ) );
memcpy ( (BlockOfMemory+sizeof(AAA)), a2, sizeof( AAA ) );
memcpy ( (BlockOfMemory+sizeof(AAA)+sizeof(AAA)), b1, sizeof( BBB ) );
如果你想打印
BlockOfMemory
的内容,你需要转换回
AAA*
BBB*
并用指针算法移动,就像这样

unsigned char* tmp = BlockOfMemory;
AAA* x = (AAA*)tmp;
printf("==>  %d %d %d\n", x->a, x->b, x->c);

tmp += sizeof(AAA);
x = (AAA*)tmp;
printf("==>  %d %d %d\n", x->a, x->b, x->c);

tmp += sizeof(AAA);
BBB* y = (BBB*)tmp;
printf("==>  %d %d %d %d %d\n", y->a, y->b, y->c, y->d, y->e);

您正在使用
&a1
等将ptr传递给
memcpy
中的ptr。如果在
printf
中使用
%hhn
,您将无法使用任何内容。
n
说明符用于写入到目前为止写入变量的字符数,它不会打印任何内容。在第二个示例中,
%hhn
没有任何意义。
%n
格式将到目前为止打印的字符数写入参数所指向的位置。哦,你说得对。或者干脆
AAA*x=(AAA*)tmp;x++
@MarcoBonelli是的,当然,为了解释,我在那里花了很长时间
memcpy ( BlockOfMemory, a1, sizeof( AAA ) );
memcpy ( (BlockOfMemory+sizeof(AAA)), a2, sizeof( AAA ) );
memcpy ( (BlockOfMemory+sizeof(AAA)+sizeof(AAA)), b1, sizeof( BBB ) );
unsigned char* tmp = BlockOfMemory;
AAA* x = (AAA*)tmp;
printf("==>  %d %d %d\n", x->a, x->b, x->c);

tmp += sizeof(AAA);
x = (AAA*)tmp;
printf("==>  %d %d %d\n", x->a, x->b, x->c);

tmp += sizeof(AAA);
BBB* y = (BBB*)tmp;
printf("==>  %d %d %d %d %d\n", y->a, y->b, y->c, y->d, y->e);