11

What's the difference between

struct mystruct *ptr = (struct test *)malloc(n*sizeof(struct test));

and

struct mystruct **ptr = (struct test *)malloc(n*sizeof(struct test *));

They both work fine, I'm just curious about the actual difference between the two. Does the first one allocate an array of structs, whereas the second one an array of struct pointers? The other way around? Also, which one has a smaller memory footprint?

alf
  • 681
  • 1
  • 8
  • 19
  • The second one probably has a smaller memory footprint. (It depends on the size of pointers and on the size of `struct mystruct`.) The first one indeed allocates an array of `struct`s and the second one an array of pointers. (So, just to clear any doubt - they don't do the same thing.) – Ry- Sep 08 '12 at 20:50

3 Answers3

20

The first allocates an array of struct, and the other allocates an array of pointers to struct. In the first case, you can write to fields by assigning ptr[0].field1 = value; right away, while in the second case you must allocate the struct itself before doing the actual writing.

It is OK to drop the cast of malloc result in C, so you could write

struct mystruct **ptr = malloc(n*sizeof(struct test *));
for (int i = 0; i != n ; i++) {
    ptr[i] = malloc(sizeof(struct test));
}
ptr[0]->field1 = value;
...
// Do not forget to free the memory when you are done:
for (int i = 0; i != n ; i++) {
    free(ptr[i]);
}
free(ptr);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Perfect answer. Awesome. Thanks :) And about the cast of malloc, what exactly is it there for? Is it ever necessary? – alf Sep 08 '12 at 21:07
  • 3
    @Matt Casting of `malloc`'s result is unnecessary in C. Here is a [link](http://stackoverflow.com/a/605858/335858) to a very good explanation why it is unnecessary and how it can hide errors. Note that in C++ the rules are different: you need to insert a cast. – Sergey Kalinichenko Sep 08 '12 at 21:12
  • @Matt you should honestly use variable-length arrays if your host environment is C99-compliant e.g. `struct mystruct structs[n];` – obataku Sep 08 '12 at 21:36
  • @dasblinkenlight Wow, thank you. It's much cleaner looking anyway. – alf Sep 08 '12 at 22:49
  • @oldrinb Why should I use variable-length arrays instead? – alf Sep 08 '12 at 22:50
  • @Matt accomplishes the same goal as far as I can see without needing to `free` (at least explicitly -- the compiler may emit `malloc`, `free` calls)... well, unless you need to allocate on the heap. – obataku Sep 08 '12 at 22:51
  • how about the lifespan? the first way (array of structs), all the struct items would be preserve until i free `*ptr` ? – TomSawyer Jun 21 '20 at 20:42
  • @TomSawyer Yes, dynamic memory stays around until you free it, or the program exits, whichever comes first. – Sergey Kalinichenko Jun 22 '20 at 00:31
2

I'm just curious about the actual difference between the two

The function malloc doesn't deal in structures or pointers. It only understands bytes. So the first allocates enough bytes for n struct test objects, which the second allocates enough space for n struct test * objects.

They both work fine

The way things look, the 2 would be used for wildly different things. For example, in the second case, you'd have to allocate memory for each ptr[i] element.

Also, which one has a smaller memory footprint

You can answer that yourself if you print sizeof(struct test) and sizeof(struct test *). But again, they're different things, with different purposes. Which has a smaller footprint, a tractor or a beetle ?

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • The first one stores all struct items data in heap, the second one stores pointers to struct items in heap. But if we want to keep lifespan on struct members, we still have to alloc it, so the total bytes to store of first one is smaller? – TomSawyer Jun 21 '20 at 20:42
1

The first allocates an array of structs. The second allocates an array of pointers to structs (no memory for the structs themselves). So the second is smaller unless of course your struct is also very small like a pointer.

TJD
  • 11,800
  • 1
  • 26
  • 34