6

I'm trying to get my head around C. Reading through K&R, I am flicking back and forth trying to find where it states the situations I should obtain blocks of memory dynamically.

For example, I want to have an int pointer.

int *pointer;

But then K&R states I may want to do:

int *pointer;

pointer = (int*)malloc(sizeof(int));

Essentially, what have I done here that is different? In the first case I created a pointer, that has yet to point to anything, so I guess if the computer runs out of memory and I try to point it to an int value I will have problems. The second case reserves space for my pointer. So I don't have to worry about my program having as many memory problems. Is this correct? If this is correct, shouldn't I use malloc(or calloc) for every pointer I create? Just to make sure my program has fewer issues?

  • The second case reserves space for an int and let your pointer point to it. If you don't have memory at that point, you will have problems too. – Gunther Piez Dec 11 '11 at 14:35

4 Answers4

9

malloc is used to allocate memory. You can use a pointer by either allocating it with malloc or making it point to an already allocated portion of memory.

In the first case you have shown, unless you make pointer point to an address, it is not allocated and cannot be used. For example, you can make it point to an exiting int value:

int value = 0;
int* pointer;
pointer = &value;

But you cannot assign it to hold a value:

int value = 0;
int* pointer;
*pointer = value; // wrong because pointer is not allocated

This is what your second case is for.

calloc is basically malloc + initialization.

Edit: Regardless, this is not really a good example of the usage of malloc. The best use is probably when you need to allocate an array of variable size (not known at compile time). Then you will need to use:

int* array = (int*)malloc(N * sizeof(int));

This is useful for two reasons:

  1. If N is a variable you cannot do a static allocation like int array[N];
  2. The stack may be limited on how much space you can allocate.
Tudor
  • 61,523
  • 12
  • 102
  • 142
  • But in the second case I could do pointer = &value. Correct? This is essentially the same, and my pointer gets the memory address that value is stored at. –  Dec 11 '11 at 14:37
  • Thanks Tudor, but I'm still confused. It seems I can do everything malloc is used for, just by using * and & operators without any dynamic memory allocation. –  Dec 11 '11 at 14:39
  • @JJG: I've added a more useful example about malloc. – Tudor Dec 11 '11 at 14:43
3

In the first case, you have allocated on the stack a pointer to integer; and that is all. So you have a single pointer.

In the second case, you have allocated on the stack a pointer to integer; and then used malloc to allocate a block of memory large enough to hold an integer and also made it such that your pointer to integer points at that malloc()ed memory; so here you have a pointer to integer and an integer.

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
  • I would say that the first case is a definition, usually of a local variable which happens to be inside a call frame allocated by the processor on the call stack. (the compiler don't allocate it). If that definition is global, the variable is not on the stack. – Basile Starynkevitch Dec 11 '11 at 14:35
  • @JJG you can. See my example. – Tudor Dec 11 '11 at 14:36
  • you can do `int myinteger=42; int *mypointer= &myinteger; *mypointer=3;` and after than `myinteger` would contain 3. – Basile Starynkevitch Dec 11 '11 at 14:36
0

The first case: You are declare 1 variable pointer only. But without assign anything to it. It is just a declaration.

But second case: You allocate memory 1 pointer in heap(you could create nth of pointers in heap) by times the number of element you want.

The different between case 1 and case 2 is that in case 1 the memory will remain in Stack and until the program finish running. but in case 2 .. you can free up(reclaim) the memory at anytime by using free(pointer).

0

You should allocate memory (optimistically) only when it is needed using calloc or malloc. Pointer may also point to an existing memory location.

Santosh
  • 328
  • 5
  • 9