Latest Uploads
Extraterre ... .0.1 (zip)

dantheman363

Monty Teas ... Screenie 1

steve_ancell

Santa Clau ... ed his bag

waroffice

manic_platdude.png

spinal

Tetris Clone

steve_ancell

Super blues bros.

spinal

Articles + Tutorials > Advanced Techniques ( Created 10 July 2008 | Last Edited 30 October 2008)

Dynamic Memory in C by Phoenix | No Votes
Explanation of the cryptic malloc()
Written in
Basic

Introduction

Last night I was in the MudChat, and Spinal was wondering how the malloc() function in C works. I tried to explain, but I probably wasn't elaborate enough. So I figured that he probably isn't the only one who wonders how to work with memory allocation, and therefore I wrote this article.

Prerequisites

This isn't a C tutorial. I assume that you have a basic understanding of C and some knowledge of pointers.

The Stack and the Heap

There are two ways to work with memory in C; on the stack and on the heap. An example of memory allocated on the stack can be seen here:

-->

"foo" needs to be stored in memory, and when variables are defined the way pictured above they are stored on the stack. The stack can be seen as a temporary storage place for memory, because the variable is removed when it goes out of scope.

Those curly braces - {} - define the beginning and the end of a scope. When a variable is allocated on the stack inside a scope, it "expires" when the } is reached. This shows a potential limitation of the stack:

-->

"foo" may or may not be a 5, because when returnFive's scope ends, its stack memory is freed. When memory is freed, its content isn't necessarily removed, but it is no longer reserved space in memory. The operating system will think that the memory can be reused for other purposes. So the five will remain in memory for a while, but can change at any time because other things might start using that particular block of memory.

So how do we overcome this problem? We work on the heap.

Dynamic Memory Allocation

This is where we get to the good stuff. Dynamic memory allocation (heap memory) is what we use to solve the issue of the previous example. I'll show some code first:

-->

This way works around the limitations of the stack. We tell the program that we want enough memory for an integer (usually four bytes), so it searches all the blocks of memory it has until it finds a place where there are four bytes available, and returns a pointer to that space. That is, unless it is impossible to find such a place. When that happens, it just returns zero. The pointer is actually created on the stack, and it is therefore removed when it reaches the end of the scope. But it is only a pointer, and not the data it points to. Since we return a copy of the pointer with "return bar", there is no loss of data. The block of memory still exists and its address is returned into foo in main().

The memory remains until we call free(), which tells the program that the memory is no longer in use and can be utilized by other operations -- perhaps another allocation on the heap.

The Syntax of malloc()

At first, the syntax of malloc() can look a bit confusing so I'll walk you through it.

malloc() returns a pointer, and therefore you need to have a pointer where you store the address to the memory block. Let's take a char as an example:

-->

Since it returns a void pointer (pointer to anything), the return value needs to be casted to your data type:

-->

Then you need to define the size of your memory block. This is usually accomplished using the sizeof() function, which returns how many bytes large the data type passed to it is. For example, sizeof(char) commonly returns 1, since it is one byte large.

-->

Often though, you want to store more than just a couple of bytes. This can be accomplished by multiplying sizeof(data_type) by the amount of elements you want. For example, let's say we wanted to store the word "Hi" (containing two letters) inside our heap memory.

-->

As seen above, you can access each char individually by using square braces. This is because heapMemory only points to the address of the first element of the memory block (&heapMemory[0]), as oposed to the whole block. Pointers don't contain any information about the size of the memoory block it points to. The same principle can be applied to pretty much any data type:

-->

Conclusion

I hope that that was understandable -- if not, feel free to ask and I'll try to explain even more.

(I'm eagerly awaiting a thousand corrections from Agent Smith)

Latest Comments

Posted : Thursday, 10 July 2008, 10:38
mike_g


I havent really read this article through yet, but malloc.h is included as part of stdlib

Posted : Thursday, 10 July 2008, 10:55
Phoenix


Thanks, I didn't know that. I'll correct it.

Posted : Friday, 11 July 2008, 19:41
JL235


There are also libraries and tools such as dmalloc that can be used to help debug all those memory leaks you'll be making with malloc.

Latest Posts
Dev Costs
steve_ancell Mon 14:43
Screen Burn of the Mind
steve_ancell Mon 11:38
RoadRash!
Mog Mon 10:56
Progress / Location Bars
Afr0 Mon 10:28
Noel's Graduation
rockford Mon 07:37
Development via GUI
waroffice Mon 02:48
Audio Rant
steve_ancell Sat 19:16
Wrong Partition!!!!?
spinal Sat 11:24
eBay Lies
spinal Fri 23:44
Shoutbox Topic - 968
dna Fri 19:42
More

Latest Items
News : Newsletter #176
Jayenkai Sat 04:49
News : Newsletter #175
Dabz Tue 09:38
Blog : Snow: More Material Junk
Cower Sat 23:17
Dev-Diary : Mutant Monty: Amstrad CPC to Windows conversion
rockford Fri 13:14
Techy : AppleTV
Jayenkai Thu 09:40
Blog : Graphviz
steve_ancell Sat 14:17
Pets : Top-Down Shadow Hack
Jayenkai Tue 05:52
Snippet : JNKrunch v1.0
Jayenkai Sat 07:20
News : Newsletter #173
waroffice Fri 04:47
Blog : Material Loading
Cower Fri 02:08
Pets : I Done Won A Thing
shroom_monk Sun 11:31
Pets : Repurposing A Lexer
Cower Mon 22:06
Bah : Feeling a Little Angry
spinal Mon 11:26
News : Newsletter #170
Dabz Sat 00:34
Showcase : sbfgen
Cower Sat 16:57
More

Who's Online
steve_ancell
Mon, at 14:45
Jayenkai
Mon, at 14:26
dna
Mon, at 14:20
CodersRule
Mon, at 13:58
shroom_monk
Mon, at 13:43
Mog
Mon, at 13:25
spinal
Mon, at 12:54
rockford
Mon, at 12:28
HoboBen
Mon, at 12:13
Dabz
Mon, at 10:31
Link to this page
Site : Jayenkai 2006-Infinity |
MudChat's origins, BBCode's former life, Image Scaler.