-
-
[讨论]请看看这个区块链程序的错误。不能分配内存。帮忙纠正一下。。。好吗
-
发表于:
2024-1-27 11:42
4807
-
[讨论]请看看这个区块链程序的错误。不能分配内存。帮忙纠正一下。。。好吗
//写一个区块链程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#define BLOCK_SIZE 1024
typedef struct block{
int size;
char data[BLOCK_SIZE];
struct block *next;
struct block *prev;
}block;
typedef struct chain{
int size;
block *head;
block *tail;
block *current;
block *current_block;
int current_block_index;
int current_block_offset;
int current_block_size;
int current_block_data_size;
int current_block_data_offset;
int current_block_data_index;
int current_block_data_end;
int current_block_data_start;
}chain;
chain *chain_new(){
chain *c = (chain*)malloc(sizeof(chain));
c->size = 0;
c->head = NULL;
c->tail = NULL;
c->current = NULL;
c->current_block = NULL;
c->current_block_index = 0;
c->current_block_offset = 0;
c->current_block_size = 0;
c->current_block_data_size = 0;
c->current_block_data_offset = 0;
c->current_block_data_index = 0;
c->current_block_data_start = 0;
c->current_block_data_end = 0;
return c;
}
block *block_new(){
block *b = (block*)malloc(sizeof(block));
b->size = 0;
b->next = NULL;
b->prev = NULL;
return b;
}
void chain_add(chain *c, block *b){
if(c->head == NULL){
c->head = b;
c->tail = b;
c->current = b;
c->current_block = b;
c->current_block_index = 0;
c->current_block_offset = 0;
c->current_block_size = 0;
c->current_block_data_size = 0;
c->current_block_data_offset = 0;
c->current_block_data_index = 0;
c->current_block_data_start = 0;
c->current_block_data_end = 0;
c->size++;
return;
}
c->tail->next = b;
b->prev = c->tail;
c->tail = b;
c->size++;
return;
}
void chain_add_data(chain *c, char *data, int size){
if(c->current_block_data_size + size > BLOCK_SIZE){
block *b = block_new();
chain_add(c, b);
c->current_block = b;
c->current_block_index++;
c->current_block_offset = 0;
c->current_block_size = 0;
c->current_block_data_size = 0;
c->current_block_data_offset = 0;
c->current_block_data_index = 0;
c->current_block_data_start = 0;
c->current_block_data_end = 0;
c->current_block->size = size;
memcpy(c->current_block->data, data, size);
c->current_block_data_size = size;
c->current_block_data_offset = 0;
c->current_block_data_index = 0;
c->current_block_data_start = 0;
c->current_block_data_end = size;
return;
}
memcpy(c->current_block->data + c->current_block_data_offset, data, size);
c->current_block_data_size += size;
c->current_block_data_offset += size;
c->current_block_data_index++;
c->current_block_data_start = c->current_block_offset;
c->current_block_data_end = c->current_block_offset + c->current_block_data_size;
return;
}
void chain_print(chain *c){
block *b = c->head;
int i = 0;
while(b != NULL){
printf("block %d: size %d\n", i, b->size);
b = b->next;
i++;
if(b == c->current_block){
printf("current block\n");
printf("current block index %d\n", c->current_block_index);
printf("current block offset %d\n", c->current_block_offset);
printf("current block size %d\n", c->current_block_size);
printf("current block data size %d\n", c->current_block_data_size);
printf("current block data offset %d\n", c->current_block_data_offset);
printf("current block data index %d\n", c->current_block_data_index);
printf("current block data start %d\n", c->current_block_data_start);
printf("current block data end %d\n", c->current_block_data_end);
printf("current block data:\n");
int j = 0;
while(j < c->current_block_data_size){
printf("%c", c->current_block->data[j]);
j++;
if(j % 16 == 0){
printf("\n");
j++;
while(j % 16 != 0){
printf(" ");
j++;
if(j % 16 == 0){
printf("\n");
j++;
break;
}
if(j == c->current_block_data_size){
break;
}
printf("%c", c->current_block->data[j]);
j++;
}
}
}
printf("\n");
break;
}
}
return;
}
void chain_print_data(chain *c){
block *b = c->head;
int i = 0;
while(b != NULL){
printf("block %d: size %d\n", i, b->size);
b = b->next;
i++;
if(b == c->current_block){
printf("current block\n");
printf("current block index %d\n", c->current_block_index);
printf("current block offset %d\n", c->current_block_offset);
printf("current block size %d\n", c->current_block_size);
printf("current block data size %d\n", c->current_block_data_size);
printf("current block data offset %d\n", c->current_block_data_offset);
printf("current block data index %d\n", c->current_block_data_index);
printf("current block data start %d\n", c->current_block_data_start);
printf("current block data end %d\n", c->current_block_data_end);
printf("current block data:\n");
printf("current block data:\n");
int j = 0;
while(j < c->current_block_data_size){
printf("%c", c->current_block->data[j]);
j++;
if(j % 16 == 0){
printf("\n");
j++;
while(j % 16 != 0){
printf(" ");
j++;
if(j % 16 == 0){
printf("\n");
j++;
break;
}
if(j == c->current_block_data_size){
break;
}
printf("%c", c->current_block->data[j]);
j++;
}
}
}
printf("\n");
break;
}
}
return;
}
int main(){
chain *c = chain_new();
char data[BLOCK_SIZE];
int i = 0;
while(i < BLOCK_SIZE){
data[i] = 'a';
i++;
}
i = 0;
while(i < BLOCK_SIZE){
data[i] = 'b';
i++;
}
i = 0;
while(i < BLOCK_SIZE){
data[i] = 'c';
i++;
}
i = 0;
while(i < BLOCK_SIZE){
data[i] = 'd';
i++;
}
i = 0;
while(i < BLOCK_SIZE){
data[i] = 'e';
i++;
}
chain_add(c, block_new());
chain_add_data(c, data, BLOCK_SIZE);
chain_print(c);
chain_print_data(c);
return 0;
}
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!
最后于 2024-1-27 11:42
被ranshu编辑
,原因: