我无法理解为什么我的代码中出现了段错误。我只是想在我的节点数组中存储数据,并希望查看我的数据是否正确存储。
#define BLOCK_SIZE 256
#define MAX_NAME_LENGTH 128
#define DATA_SIZE 254
#define INDEX_SIZE 127
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
typedef enum
{
DIR,
FILE,
INDEX,
DATA
} NODE_TYPE;
char *bitvector; // allocate space for managing 2^16 blocks (in init) (size is 8192)
typedef struct data_t
{
int size;
void *data;
} data_t;
typedef struct fs_node
{
char name[MAX_NAME_LENGTH];
time_t creat_t; // creation time
time_t access_t; // last access
time_t mod_t; // last modification
mode_t access; // access rights for the file
unsigned short owner; // owner ID
unsigned short size;
unsigned short block_ref; // reference to the data or index block
} FS_NODE;
typedef struct node
{
NODE_TYPE type;
union
{
FS_NODE fd;
data_t data[DATA_SIZE];
unsigned short index[INDEX_SIZE];
} content;
} NODE;
// storage blocks
NODE *memory; // allocate 2^16 blocks (in init)
void init()
{
memory = malloc(sizeof(NODE) * 65536);
memory[0].type = DIR;
FS_NODE *root = malloc(sizeof(FS_NODE));
strcpy(root->name,"/");
root->creat_t = 0;
root->access_t = 0;
root->mod_t = 0;
root->access = 0400;
root->owner = 0;
root->size = 0;
root->block_ref = 1;
memory[0].content.fd = *root;
}
int main()
{
printf("%s\n", memory[0].content.fd.name);
free(memory);
return 0;
}
答案 0 :(得分:1)
memory
对象在您使用之前未分配...
更改此
int main() {
printf("%s\n", memory[0].content.fd.name);
free(memory);
return 0;
}
到这个
int main() {
init();
printf("%s\n", memory[0].content.fd.name);
free(memory);
return 0;
}
答案 1 :(得分:0)