首页
社区
课程
招聘
[讨论]请看看这个区块链程序的错误。不能分配内存。帮忙纠正一下。。。好吗
发表于: 2024-1-27 11:42 4807

[讨论]请看看这个区块链程序的错误。不能分配内存。帮忙纠正一下。。。好吗

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编辑 ,原因:
收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 228
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
2
这个区块链程序有以下几点需要修改:

分配内存时需要检查分配是否成功:
c
复制
chain *c = malloc(sizeof(chain));
if(!c) {
  printf("malloc failed");
  return;
}
分块存储时需要检查是否分配成功:
c
复制
block *b = malloc(sizeof(block));
if(!b) {
  printf("malloc failed");
  return;  
}
复制数据时需要检查数据大小是否超限:
c
复制
if(size > BLOCK_SIZE) {
  printf("data size exceeded block size");
  return;
}
打印数据时需要检查索引是否超出范围:
c
复制
while(j < c->current_block_data_size && j < BLOCK_SIZE) {
  // print data
}
释放已分配内存:
c
复制
void free_chain(chain *c) {
  // free each block
  // free chain
}
使用更清晰的变量命名

添加注释说明程序功能

检查其他可能发生错误的地方

总体来说,主要问题是没有正确检查内存分配是否成功,数据操作是否合理,以及最后释放内存。修正这些问题可以使程序更健壮和清晰。
2024-1-29 11:05
1
雪    币: 619
活跃值: (1178)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
信息详细点,哪里报错,报错内容是什么,多来点图
2024-1-29 15:54
0
雪    币: 3686
活跃值: (1670)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
还当是ethervm呢
2024-1-31 19:23
0
游客
登录 | 注册 方可回帖
返回
//