2
// the malloc style, which returns a pointer:
struct Cat *newCat = malloc(sizeof(struct Cat));

// no malloc...but isn't it actually the same thing? uses memory as well, or not?
struct Cat cat = {520.0f, 680.0f, NULL};

Basically, I can get a initialized structure in these two ways. My guess is: It's the same thing, but when I use malloc I also have to free() that. In the second case I don't have to think about memory, because I don't call malloc. Maybe.

When should I use the malloc style, and when the other?

dontWatchMyProfile
  • 45,440
  • 50
  • 177
  • 260

5 Answers5

6

It is not the same memory, the former is allocated on heap and the latter is allocated on stack.

Heap enables you to manually control the creation and deletion, with all pros and cons: you may pass the pointer to others, but you have to delete it also.

Whenever the second is at all possible, it is preferred.

Pavel Radzivilovsky
  • 18,794
  • 5
  • 57
  • 67
4

The malloc function should only be used if you need the struct to live past the lifetime of the current function. The malloc function will allocate memory off of the heap which allows it to be long lived. This is useful if you need to store items after the exiting of the function (in a shared list for instance).

It also introduces extra overhead including ...

  • Forcing more error checking into your code
  • Separation of allocation and initialization of the structure
  • Overhead in the process to track the memory

On the other hand the second syntax allocates the memory on the stack. This is beneficial because the memory here is automatic in the sense that you don't have to think about it. The memory is freed so to speak when the function exits. Additionally it allows for a single statement for allocation and initialization.

The one exception to this rule is for structs which are big enough to potentially cause the stack to overflow. The stack is a limited resource and big structs can cause it to be exhasted. The heap generally speaking has more room and can easily accommodate these larger structures.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 4
    ... or the struct is so big that will overflow the stack. – kennytm Jun 01 '10 at 17:00
  • Brain Runtime Error: Inside a method: Why can I assign a CGRect to an CGRect iVar without messing around with malloc? The CGRect struct data is still around all the time, even when the method is done. No malloc. Or does your theory only apply to C-functions? – dontWatchMyProfile Jun 03 '10 at 14:42
3

Firstly, the first variant does not initialize any structure. It simply allocates a block of raw memory sufficient to hold an object of struct Cat type. The memory remains uninitialized (contains garbage). The second variant does initialize each field in the structure to a specific value.

Secondly, the first variant creates a nameless [and uninitialized] struct Cat object with allocated storage duration. The object will live until you explicitly deallocate it with free (or until the program ends). The second variant creates a named object cat with automatic storage duration - it will be deallocated automatically at the end of its declarative region (at the end of the containing block).

Thirdly, in practice the two variants create objects in different kinds of memory. The first variant creates the object in the heap, while the second variant will typically create it on the stack.

As for when to use one or another, there are quite a few answers here on SO to that question. This thread has some answers and plenty of links.

Community
  • 1
  • 1
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
2

The difference (among other points already said) is that you do not initialize at all the structure in the first case (malloc); you just get the memory to hold it. In the second case, you have the memory and the initialization.

ShinTakezou
  • 9,432
  • 1
  • 29
  • 39
1

In addition to the other very useful answers, there is one more key difference.

The malloc() method leaves the structure members uninitialized. You have to initialize them in a separate step.

Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50