Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.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 - Fatal编程技术网

C 将内存中的数据块保存到字节数组,以便稍后从字节数组恢复该数据块

C 将内存中的数据块保存到字节数组,以便稍后从字节数组恢复该数据块,c,C,我有以下代码: typedef struct { struct { uint64_t key; uint64_t hash; uint64_t size; uint64_t body; } length; } block_head; ----------------------------------------------------- //Block allocation uint64_t d1, d2, d

我有以下代码:

typedef struct {
    struct {
        uint64_t key;
        uint64_t hash;
        uint64_t size;
        uint64_t body;
    } length;
} block_head;

-----------------------------------------------------
//Block allocation

uint64_t d1, d2, d4; //Lengths for octet strings to be saved in memory block
uint64_t d3; //just for saving a size - integer value

unsigned char **data = (unsigned char**)malloc(sizeof(block_head) + d1 + d2 + d4);
block_head *head = (block_head *)data;
head->length.key = d1;
head->length.hash = d2;
head->length.size = d3;
head->length.body = d4;
-----------------------------------------------------
//Later we fill memory of data block

// get a pointer key buffer location
unsigned char *d = (char *)data + sizeof(secure_head_t);

//Add octet string
FillData1(d);

// get a pointer to the body buffer location
unsigned char *body = (unsigned char *)data + (sizeof(block_head) + head->length.d1 + head->length.d2);
//get the length of the body free space (of a block)
int body_length = head->length.body;

//body is filled with octet string, and length is saved to body_length
FillBody2((unsigned char*)body, &body_length) 

// Advance the body pointer to the location of the remaining space, and calculate just how much room is still available.

body += body_length;

// Save another octet string to block
FillBody3((unsigned char *)data + (sizeof(block_head) + head->length.key), &body_length);
现在我需要将填充的块
(unsigned char**data)
保存到字节数组中,以便以后从数组恢复到块中

我这样做了,但不起作用:

unsigned char **data = some_filled_data;

block_head *head = (block_head *)data;

// convert data to arr

unsigned char *arr = (unsigned char *)malloc( sizeof(block_head) + (head->length.key + head->length.hash + head->length.body));
memcpy(&arr, data, sizeof(block_head) + (head->length.key + head->length.hash + head->length.body));

// convert arr to data

unsigned char *data = (unsigned char*)malloc( sizeof(unsigned char) * strlen(arr));
memcpy(&data, &arr, strlen(arr));
如果我尝试使用来自
arr
的新转换块,那么我将得到一个错误,因为它的构造不正确或类似于此


如何正确地将数据转换为
arr
,将
arr
转换为数据,以表示同一块?

虽然我无法解读描述,但这回答了标题:

typedef struct {
    int a;
    char b[20];
} Test;

void fillAndBackupTest(char **out) {
    Test test;
    test.a = 20;
    strcpy(test.b, "Hello!");
    *out = (char*) malloc(sizeof Test);
    memcpy(*out, &test, sizeof Test);
}

void restoreAndPrintTest(char *in) {
    Test test;
    memcpy(&test, in, sizeof Test);
    printf("a: %d, b: %s\n", test.a, test.b);
}

int main()
{
    char *blob;
    fillAndBackupTest(&blob);
    restoreAndPrintTest(blob);
    free(blob);
    return 0;
}
定义了一个结构类型,
Test
fillAndBackupTest()
创建一个,填充它的字段并将它的“副本”存储到它为自己分配的缓冲区中(现在它是一个
char*
,但实际上它很可能仍然是
void*
),然后
restoreAndPrintTest()
恢复一个(不同的)
Test
实例,并打印其内容

使用
malloc
——计算一个大小和一些任意数字的总和(
key
body
?)似乎不是一个好主意,而且
strlen
也不能测量二进制blob的长度