asdasd
asdasd

Reputation: 59

Allocating memory for array of struct in fgets

I am trying to allocate memory for an array of structs (children) while reading how much memory should be allocated from an input file. I am unsure what the smartest way to do this is. My structs:

typedef struct sNaryNode {
  void* data; // point to each nodes data
  unsigned int n; // the number of children
  struct sNaryNode **child; // the child list
} NaryNode;

typedef struct {
  char *name;
  unsigned int utility; // -1, 0, 1
  unsigned int probability; // 0-1
} Child;

typedef struct {
  unsigned int level;
  unsigned int player;
  unsigned int nChildren;
  Child *children;  // The array of structs I am trying to allocate memory to.
} Data;

I have tried the following:

void readData(Data *node) {
  FILE *input_file = fopen("data.csv", "r"); 

  if(input_file == NULL) printf("Could not open file\n");

  else { 
    char buf[80]; 
    int n = 0;
      while(fgets(buf, 80, input_file)!= NULL) {
        // allocate space for new data
        Data *node = (Data*)calloc(1, sizeof(Data));
        sscanf(buf, " %u, %u, %u, ",  &node->level, &node->player, &node->nChildren);
        node->children = calloc(node->nChildren, sizeof *node->children);
        sscanf(buf, "%u, %u, %s ",  &node->children[n].utility, &node->children[n].probability, node->children[n].name);
        n++;
    }
    fclose(input_file);
  }
}

int main(void) {
  Data *node;
  readData(node);
}

It results in a segmentation fault which I expect has something to do with wrong memory allocation.

File I am reading:

level, player, nChildren,  utility,  probability,     name
  1,     1,      2,          1 0,      0.50 0.50,       "Kom med det første tilbud (anchor)" "Afvent modspilleren kommer med første tilbud"
  2,     2,      2,          1 0,      0.50 0.50,       "Kom med lavt modtilbud (anchor)"
  2,     2,      2,          1 0,      0.50 0.50,       "Kom med det første tilbud "anchor"

EDIT: GDB tells me that the segfault is coming from the second sscanf line in readData. Still unsure what is causing it.

Upvotes: 0

Views: 111

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409176

The most problematic line is this one:

node->children[n] = *(Child*)calloc(1, sizeof(node->nChildren));

First of all you haven allocated memory for node->children, it is a null pointer which you dereference. Secondly, you allocate the wrong amount of memory. Thirdly since you dereference the returned pointer and then throw it away you will have a memory leak.

Exactly how to solve all the problems I don't know, not without more details about the file you're reading. But the first and third problem can be solve by allocating memory for node->children like this

node->children = calloc(node->nChildren, sizeof *node->children);

There are also problems reading the name. One thing is that the sscanfformat "%s" reads space delimited strings. The second and most serious is that you don't allocate space for the name.

Upvotes: 1

Related Questions