0

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.

1
  • Do not change your question in a way that renders already given answer un-understandable ... :-/ Commented Dec 3, 2016 at 11:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have just fixed the line as you said. It makes sense. However the issue seems to persist (still get segfault 11). I added the contents of the file I am trying to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.