Dynamically allocating space for a variable or array in C sizeof: This operator returns the number of chars of space needed for a type or variable. For example, assuming x is of type double, either sizeof ( double ) or sizeof ( x ) where the brackets are optional, will return 8 since on most machines a double is 64 bits and a char is 8 bits. malloc, calloc and realloc: These functions allocate storage space dynamically from a pool of free space. [They use system call sbrk() to extend the data segment if the pool needs to be extended.] All these functions return a pointer to available space. malloc and realloc do not initialize new space. calloc initializes to 0 bits which is somewhat useful for ints, but not for doubles or pointers. Using "double" type as an example: #include /* NEVER FORGET THIS INCLUDE */ double *px, *array; /* malloc allocates a single storage location and returns a pointer to void. */ px = malloc( sizeof(double) ); /* px is a memory location that can store a double */ *px = 3.141; /* How to store a value there */ /* calloc allocates storage for an array where n is the size of the array. n is of type size_t */ array = calloc( n, sizeof(double) ); /* array points to the first of n consecutive doubles */ /* For less fuss use malloc instead */ array = malloc( n * sizeof(double) ); /* realloc additionally can resize space */ array = realloc ( array, n * sizeof(double) ); /* The first parameter will be automatically cast to pointer to void. If the first parameter is NULL (e.g. array=NULL; beforehand) then realloc behaves like malloc. Otherwise, realloc changes the size of array to the size given as the second argument. The contents will be unchanged up to the minimum of the new and old sizes, however, realloc may need to move the location of the space. The new location is returned in "array". It is a serious error to call realloc with a first parameter not obtained from a previous alloc call. */ IN ALL CASES, ALWAYS CHECK THE RETURN: if ( NULL == ( px or array = alloc (... sizeof ...) ) { fprintf(stderr,"Out of memory\n"); exit(1); } Pre ANSI C compilers required you to use a cast: px = (double *) alloc ... This should NOT be done in ANSI C as it prevents the compiler from catching some errors such as forgetting to include stdlib.h. free: void free( void *p ) /* any pointer or NULL */ This frees storage previously allocated by an alloc function. free(NULL) does nothing, but is useful because it means you do not need to check a pointer before calling free. It is a serious error (that is, a run time error - hard to spot and causes mayhem but usually elsewhere in the run) to: Free something that was not allocated. Free something twice. Use something that has been freed already. It is very poor programming to forget to free memory that was previously alloced but is no longer used - a "memory leak". Exiting with memory still alloced is fine, but if the code is rewritten later this may lead to a memory leak. The problem with a memory leak is that you may run out of memory during a run (a run time error!!). The problem may not be apparent on test runs, but will result in your program mysteriously crashing if it is run for a long period of time or over a lot of data as may happen if you are writing a server or daemon. Typical example of how memory leaks arise: int leak_example() { double *px; int result; px = malloc(...) /* Memory needed for internal data */ . . . /* function does not free px and does not return px */ return result; } /* Once this function returns, there is no way to refer to px and free that memory. Each call to function leaks a double */ Programming advice: It is very bad practice to tinker with a part of a program that has allocs in it until the program appears to run. Likely there are hidden bugs. Don't hire anyone who programs in C that way! Instead think about the logic of the program and look for code that doesn't do what was intended. Typically alloc errors appear as segmentation faults - when debugging the error appears to come from unrelated parts of the program. A good programmer will anticipate problems by thinking clearly about array bounds and the like while developing the code. There are several public domain alloc utilities (efence, malloc-dbg, debug-malloc, malloc-lib, etc.) which do tighter checking of allocs to help track down problems. alloca(): This function allocates space in the stack frame of the calling function, which means the space is automatically freed when the caller returns. Unfortunately not all systems have this function, and the space available is limited by the size of the stack. MORE EXAMPLES: Allocate space for string of length 9 (that is an array of size 10 since a string terminator is needed): #include char *str; if (NULL == ( str = malloc( 10 * sizeof(char)))) { fprintf(stderr,"Out of memory\n"); exit(1); } Allocate an array of ints, fildes of size fdim, if fildes has been allocated before (prior to the first call set fildes = NULL): int *fildes; filedes = realloc(fildes, fdim * sizeof int); if (filedes == NULL) outofmem(); /* need to define outofmem() */ Allocate an array of argv type (an array of pointers to strings): char **cmdlist; cmdlist = NULL; cmdlist = realloc(cmdlist, cdim * sizeof(char *)); if (cmdlist == NULL) outofmem(); /* No space has been set aside for the strings yet */ If these arrays and strings are part of a structure pointed to by "plist" say, then just put "plist->" in front of cmdlist, cdim, filedes, fdim and str ANOTHER EXAMPLE: #include /* Function sum_array returns an array of size dim whose entries are the sum of corresponding entries of arrays a and b up to dim */ int *sum( int *a, int *b, int dim /* size of a and b */) { int i, *sum_array; if( NULL == (sum_array = malloc( dim * sizeof int))) { fprintf(stderr,"Out of memory\n"); exit(1); } for (i=0; i < dim; i++) sum_array[i] = a[i] + b[i]; return sum_array; }