Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Malloc 1

Status
Not open for further replies.

dickiebird

Programmer
Feb 14, 2002
758
GB
Hi
Can anyone explain why we have malloc and its associated functions ??
Every book I've read talks about HOW at length, without going into the WHY
I've re-written,copied,created new progs, with struct declarations etc, and never used malloc, free etc
But with other programs, I've copied,cribbed etc,where malloc is used, without ever understanding why we allocate space in memory. My experience on unix is AIX and HPUX.
Anyone point me in the right direction - to a link to C tutorials perhaps ??
Regards
DickieBird
 
malloc/free manage memory on the heap - this is dynamic memory that is created after the program starts running.

One reason to use dynamic allocation is working with large data sets, or passing large data structures to functions.

Here is an example of both. Suppose you had:

double data[10000][10000];

If you tried to pass that structure to a function, the processor would try to put a copy on the stack and would overflow.

In fact, if you tried to compile the program, you might get an error telling you that you are out of stack space (depending on your operating system). The executable only has so much room it can use for data.

Are you just screwed? No, you can get around stack limitations and pass any size data freely between functions if you allocate the memory dynamically:

#define ROWS 10000
#define COLS 10000

double **data;

data = (double **) malloc(sizeof(double *) * (ROWS)); /* ptrs to rows */

for (i = 0; i < ROWS; i++)
data = (double *) malloc(sizeof(double) * (COLS)); /* row elements */


WHEN you pass **data to a function, you only pass a 4 byte memory address instead of a 100 million element matrix. Which is nice.


Then when you are done, you free up the memory for other applications to use.

for (i = 0; i < ROWS; i++)
free(data);

free(data);


Using malloc and free really depends on the memory requirements of your program. Most large-scale, complex programs use dynamic memory allocation to some extent.
 
Another &quot;popular&quot; reason for using malloc (and dynamic memory allocation)
is when you don't know how much memory you will need at compile time.
For instance, reading data from a file or database into a linked list of data
structures.
 
Try compiling the following as a Win32 Console application and see what your machine does.

#include <stdio.h>
#include <stdlib.h>

int main (int iNumberOfArguments, char *iArguments[])
{
void *aChunkOfMemory;
if (iNumberOfArguments == 1)
{
while (1)
{
aChunkOfMemory = calloc(1000000,sizeof(char));
}
}
else
{
while (1)
{
aChunkOfMemory = calloc(1000000,sizeof(char));
free(aChunkOfMemory);
}
}
return 0;
}
 
is there a way to allocate more memory space to an array
after it has been initialized?
 
To answer Maulkin's question: No, you can't &quot;redimension&quot; arrays.

However, you can, of course, use realloc() to change the size of allocated memory by passing a pointer to that memory.
Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top