Allocating memory for array of struct in fgets

asdasd

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.

Some programmer dude

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Allocating memory for an array of struct i get an error

From Dev

Allocating Memory to a String in a Struct

From Dev

Dynamically allocating an array of a struct

From Dev

Allocating memory to a struct using pointers

From Dev

allocating memory to an array of string

From Dev

Allocating memory for a array of linked lists

From Dev

Allocating memory for global multidimensional array

From Dev

Allocating memory to array of strings in c

From Dev

Passing a pointer to an array and allocating memory

From Dev

Difference between allocating memory in struct and main?

From Dev

allocating memory for a struct when it includes a string

From Dev

C Programming: Reading data from a file, dynamically allocating memory, placing contents in struct array

From Dev

Dynamically allocating array of struct from file C

From Dev

C++ allocating an array of pointers to struct

From Dev

Check if there is enough memory before allocating byte array

From Dev

Allocating memory for one dimensional array in C

From Dev

Correctly allocating/freeing memory for records in static array

From Dev

Memory management in allocating 2-D array

From Dev

Allocating memory to hold an array of structs in C

From Dev

C: Array not allocating more memory correctly

From Dev

Allocating memory for a 2-dimensional array that is in a structure

From Dev

Allocating memory for a structure array inside another structure

From Dev

Filling an array of structs and allocating memory on the heap

From Dev

Memory management in allocating 2-D array

From Dev

Memory leak after allocating multidimensional array

From Dev

C not allocating memory for array of structs correctly?

From Dev

Trouble dynamically allocating memory for string array

From Dev

Allocating memory for array of pointers of int dynamically

From Dev

Memory allocating array of structure pointers in c

Related Related

  1. 1

    Allocating memory for an array of struct i get an error

  2. 2

    Allocating Memory to a String in a Struct

  3. 3

    Dynamically allocating an array of a struct

  4. 4

    Allocating memory to a struct using pointers

  5. 5

    allocating memory to an array of string

  6. 6

    Allocating memory for a array of linked lists

  7. 7

    Allocating memory for global multidimensional array

  8. 8

    Allocating memory to array of strings in c

  9. 9

    Passing a pointer to an array and allocating memory

  10. 10

    Difference between allocating memory in struct and main?

  11. 11

    allocating memory for a struct when it includes a string

  12. 12

    C Programming: Reading data from a file, dynamically allocating memory, placing contents in struct array

  13. 13

    Dynamically allocating array of struct from file C

  14. 14

    C++ allocating an array of pointers to struct

  15. 15

    Check if there is enough memory before allocating byte array

  16. 16

    Allocating memory for one dimensional array in C

  17. 17

    Correctly allocating/freeing memory for records in static array

  18. 18

    Memory management in allocating 2-D array

  19. 19

    Allocating memory to hold an array of structs in C

  20. 20

    C: Array not allocating more memory correctly

  21. 21

    Allocating memory for a 2-dimensional array that is in a structure

  22. 22

    Allocating memory for a structure array inside another structure

  23. 23

    Filling an array of structs and allocating memory on the heap

  24. 24

    Memory management in allocating 2-D array

  25. 25

    Memory leak after allocating multidimensional array

  26. 26

    C not allocating memory for array of structs correctly?

  27. 27

    Trouble dynamically allocating memory for string array

  28. 28

    Allocating memory for array of pointers of int dynamically

  29. 29

    Memory allocating array of structure pointers in c

HotTag

Archive