c - Getting a segfault- trying to retrieve data from my struct -


i'm having trouble understanding why i'm getting segfault in code. i'm trying store data in array of nodes , wanted see if data getting stored properly.

#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 managing 2^16 blocks (in init)         (size 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 file    unsigned short owner; // owner id    unsigned short size;    unsigned short block_ref; // reference 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; } 

the memory object not allocated before used it...

change

int main() {   printf("%s\n", memory[0].content.fd.name);   free(memory);   return 0; } 

to this

int main() {   init();   printf("%s\n", memory[0].content.fd.name);   free(memory);   return 0; } 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -