The malloc() usage - dynamic memory allocation

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info:

To do: Allocate memory without freeing it up

To show: The malloc() C function usage - dynamic memory allocation

 

/* Playing with malloc(), memory on the heap */

#include <stdio.h>

#include <stdlib.h>

 

void main(void)

{

int x;

int *y;

 

/* do 100000 times iteration, 100000 blocks and be careful! Your system will be out of memory!

Because the allocated memory not released back to system! */

 

for(x=0; x<100000; x++)

{

/* For every iteration/block, allocate 16K, system will truncate to the nearest value */

y = (int *)malloc(16384);

 

/* If no more memory */

if(y == NULL)

{

puts("No more memory lol! You need to free up some...");

/* exit peacefully */

exit(0);

}

/* Allocate the memory block, print the block and the address */

printf("Allocating-->block: %i address: %p\n", x, y);

}

/* Here, we do not free up the allocation!!! After the program running, your system will be in lack of free memory! */

}

 

Output example:

 

Allocating-->block: 0 address: 00355018

Allocating-->block: 1 address: 00420068

Allocating-->block: 2 address: 00424098

Allocating-->block: 3 address: 004280C8

Allocating-->block: 4 address: 0042C0F8

Allocating-->block: 5 address: 00430128

Allocating-->block: 6 address: 00434158

Allocating-->block: 7 address: 00438188

Allocating-->block: 8 address: 0043C1B8

Allocating-->block: 9 address: 004401E8

Allocating-->block: 10 address: 00444218

...

[trimmed]

...

Allocating-->block: 27 address: 00488548

Allocating-->block: 28 address: 0048C578

Allocating-->block: 29 address: 004905A8

Allocating-->block: 30 address: 004945D8

Allocating-->block: 31 address: 00498608

Allocating-->block: 32 address: 0049C638

Allocating-->block: 33 address: 004A0668

Allocating-->block: 34 address: 004A4698

Allocating-->block: 35 address: 004A86C8

Allocating-->block: 36 address: 004AC6F8

Allocating-->block: 37 address: 004B0728

Allocating-->block: 38 address: 004B4758

Allocating-->block: 39 address: 004B8788

Allocating-->block: 40 address: 004BC7B8

Allocating-->block: 41 address: 004C07E8

Allocating-->block: 42 address: 004C4818

Allocating-->block: 43 address: 004C8848

Allocating-->block: 44 address: 004CC878

Allocating-->block: 45 address: 004D08A8

Allocating-->block: 46 address: 004D48D8

...

[trimmed]

...

Allocating-->block: 68 address: 00530128

Allocating-->block: 69 address: 00534158

Allocating-->block: 70 address: 00538188

Allocating-->block: 71 address: 0053C1B8

Allocating-->block: 72 address: 005401E8

Allocating-->block: 73 address: 00544218

Allocating-->block: 74 address: 00548248

Allocating-->block: 75 address: 0054C278

Allocating-->block: 76 address: 005502A8

Allocating-->block: 77 address: 005542D8

Allocating-->block: 78 address: 00558308

Allocating-->block: 79 address: 0055C338

Allocating-->block: 80 address: 00560368

Allocating-->block: 81 address: 00564398

^CPress any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index