Allocating memory to hold an array of structs in C

Antithesis

I'm trying to allocate memory to hold an array of structs

SERVER* topology = malloc(sizeof(struct SERVER*)* 10 );
    for (int i = 0; i < 10; ++i)
    {
        topology[i] = malloc(sizeof(struct SERVER));
    }
    PATH* paths = malloc(sizeof(struct PATH*)*10);
    for (int i = 0; i < 10; ++i)
    {
        paths[i] = malloc(sizeof(struct PATH));
    }

These are my structs

typedef struct{
    int id;
    char ip_addr[MAX_IP + 1];
    int port;
}SERVER;

typedef struct{
    int server1;
    int server2;
    int weight;
}PATH;

And then later in my code I try to free it using

for (int i = 0; i < 10; ++i)
   {
     free(paths[i]);
   }
   free(paths);

   for (int i = 0; i < 10; ++i)
   {
     free(topology[i]);
   }
   free(topology);

I keep getting the following error

error: invalid application of 'sizeof' to an incomplete type 'struct SERVER'
                topology[i] = malloc(sizeof(struct SERVER));
                                     ^     ~~~~~~~~~~~~~~~
:18:42: note: forward declaration of 'struct SERVER'
        SERVER* topology = malloc(sizeof(struct SERVER*)* 10 );
                                                ^
:26:21: error: invalid application of 'sizeof' to an incomplete type 'struct PATH'
                paths[i] = malloc(sizeof(struct PATH));
                                  ^     ~~~~~~~~~~~~~
:23:37: note: forward declaration of 'struct PATH'
        PATH* paths = malloc(sizeof(struct PATH*)*10);

...........

c:97:11: error: passing 'PATH' to parameter of incompatible type 'void *'
         free(paths[i]);
              ^~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/stdlib.h:143:18: note: passing argument to parameter here
void     free(void *);
                    ^
:103:11: error: passing 'SERVER' to parameter of incompatible type 'void *'
         free(topology[i]);
              ^~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/stdlib.h:143:18: note: passing argument to parameter here
void     free(void *);

I'm generally new to C. Any help appreciated.

Kenney

Try this:

typedef struct SERVER {
  ....
} SERVER;

typedef struct PATH {
  ....
} PATH;

You see, you use struct SERVER and struct PATH but you haven't declared these. You did declare a type SERVER and a type PATH using a typedef on an unnamed struct.

Alternatively, you could leave your structs as they are, and use sizeof(PATH*) and sizeof(STRUCT*).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C not allocating memory for array of structs correctly?

From Dev

Filling an array of structs and allocating memory on the heap

From Dev

allocating memory for structs

From Dev

Allocating memory to array of strings in c

From Dev

Allocating memory for one dimensional array in C

From Dev

C: Array not allocating more memory correctly

From Dev

Memory allocating array of structure pointers in c

From Dev

Allocating an Array of Structs without Malloc?

From Dev

Dynammically allocating array of multiple structs

From Dev

allocating memory to an array of string

From Dev

Allocating memory for structs, 2 different ways?

From Dev

Allocating Memory for string in c?

From Dev

Allocating memory (C)

From Dev

How to correctly allocate memory for an array of structs in C?

From Dev

How to correctly allocate memory for an array of structs in C?

From Dev

C++ - safety of allocating memory for an array, then returning a pointer to be deleted externally

From Dev

C++ allocating large array on heap gives "out of memory exception"

From Dev

Why is C++ allocating such a large space in memory for my dynamic array?

From Dev

Allocating memory for a array of linked lists

From Dev

Allocating memory for global multidimensional array

From Dev

Allocating memory for array of struct in fgets

From Dev

Passing a pointer to an array and allocating memory

From Dev

Free memory for array of structs

From Dev

Problems with allocating memory for a matrix in c

From Dev

Allocating memory in C for a Fortran allocatable

From Dev

In C, memory allocating fails, why?

From Dev

Allocating memory for triple pointer: C

From Dev

Allocating and freeing memory in a loop in C

From Dev

Trouble allocating memory to pointer in c

Related Related

  1. 1

    C not allocating memory for array of structs correctly?

  2. 2

    Filling an array of structs and allocating memory on the heap

  3. 3

    allocating memory for structs

  4. 4

    Allocating memory to array of strings in c

  5. 5

    Allocating memory for one dimensional array in C

  6. 6

    C: Array not allocating more memory correctly

  7. 7

    Memory allocating array of structure pointers in c

  8. 8

    Allocating an Array of Structs without Malloc?

  9. 9

    Dynammically allocating array of multiple structs

  10. 10

    allocating memory to an array of string

  11. 11

    Allocating memory for structs, 2 different ways?

  12. 12

    Allocating Memory for string in c?

  13. 13

    Allocating memory (C)

  14. 14

    How to correctly allocate memory for an array of structs in C?

  15. 15

    How to correctly allocate memory for an array of structs in C?

  16. 16

    C++ - safety of allocating memory for an array, then returning a pointer to be deleted externally

  17. 17

    C++ allocating large array on heap gives "out of memory exception"

  18. 18

    Why is C++ allocating such a large space in memory for my dynamic array?

  19. 19

    Allocating memory for a array of linked lists

  20. 20

    Allocating memory for global multidimensional array

  21. 21

    Allocating memory for array of struct in fgets

  22. 22

    Passing a pointer to an array and allocating memory

  23. 23

    Free memory for array of structs

  24. 24

    Problems with allocating memory for a matrix in c

  25. 25

    Allocating memory in C for a Fortran allocatable

  26. 26

    In C, memory allocating fails, why?

  27. 27

    Allocating memory for triple pointer: C

  28. 28

    Allocating and freeing memory in a loop in C

  29. 29

    Trouble allocating memory to pointer in c

HotTag

Archive