tags:

views:

86

answers:

5
// 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?

+4  A: 

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
... or the struct is so big that will overflow the stack.
KennyTM
@KennyTM, updated to reflect that.
JaredPar
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
+6  A: 

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
I wouldn't go so far as to say "Whenever the second is at all possible, it is preferred." For instance, when Cat is a heavy object it is likely preferable to pass around a pointer to a Cat rather than the object itself.
Shakedown
But you can still pass a pointer to a variable on the stack.. Though I agree.
Pavel Radzivilovsky
@Pavel: Actually, if the second allocation is global or static, then the memory could be in the data or text segment instead of the the stack or the heap.
torak
@torak, true. But let's not confuse beginners :)
Pavel Radzivilovsky
@torak: The OP is obviously talking about situations when the two are interchangeable, which leaves us with local context only.
AndreyT
+1  A: 

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
+3  A: 

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.

AndreyT
+2  A: 

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
+1 for one of the most common errors I have encountered in c.
Nick
thank you for the +1! :)
ShinTakezou