Skip to main content
GameDev.net gamedev.net
🔒 Locked

Understanding memory allocation

Started by NSDuo Nov 17, 2007 at 7:27 PM 10 replies 1.8k views
Original Post
NSDuo
NSDuo
I've read all these different tutorials, I must have done a thousand google searches, and I still can't understand memory allocation. I can understand a little bit how to request memory, the tutorials explained that sort of well, but I can't figure out how to use the memory once my application has it.
Macbook Pro 2.66Ghz dual core, 4GB ram, 512MB vram, MacOSX 9.1, Windows 8.1
Xcode 5.0.2, C++, lua
andyZER0
andyZER0
What language are you using to write your application?
NSDuo
NSDuo
C, C++ I guess i forgot to mention that... If I learn it in these languages I'll probably be able to figure out how to use it for other languages (such as Objective C)
Macbook Pro 2.66Ghz dual core, 4GB ram, 512MB vram, MacOSX 9.1, Windows 8.1
Xcode 5.0.2, C++, lua
andyZER0
andyZER0
Could you be more specific about what you can't figure out? (e.g. pointers or something)
NSDuo
NSDuo
Pointers, that too. I can't really be very specific about what I'm having trouble with because I'm having trouble with everything about it. One of my problems is that none of my books cover pointers and memory.
Macbook Pro 2.66Ghz dual core, 4GB ram, 512MB vram, MacOSX 9.1, Windows 8.1
Xcode 5.0.2, C++, lua
Jan-Lieuwe
Jan-Lieuwe
Hmm... you're being vague, but I'll give it a shot:

Memory allocation:

Your application has access to something that's called a heap. A heap is a pool of memory that you can [ab]use by calling functions/operators like:

new
malloc

You can release allocated heap-memory by calling

delete
free

Pointers are the memory addresses that the CPU uses to index into memory. New and Malloc return memory addresses (pointers) in the heap space of your application that you can use to store data.
Everything that's stored in memory has a pointer associated with it, basically:

// global array (stored in .bss section of your program)
char g_myArray[256];
// anoter array (the pointer is stored in the .bss section, the array itself is on the heap)
char *g_pAnotherArray = new char[256];

...you can get the memory address (pointer) where the array is stored and print it by doing:

char *pArray = g_myArray; // or &g_myArray[0]
printf("my array's address: %08x\n", pArray);
printf("another array's address: %08x\n", g_pAnotherArray);


Maybe I'm going into much detail already :) Anyway, these are some of the basics.
ZigguratVertigo
ZigguratVertigo
Hey NSDuo.

I was wondering, which books do you have... Seems like memory allocation, especially pointers, is something that most generic C/C++ books tend to explain properly ;).

Then again, if I could recommend one, check this one out...

http://www.amazon.com/C-Programming-Language-Special-3rd/dp/0201700735

PS: The Schaum series is nice as well ;)
NSDuo
NSDuo
The books I have are C for Dummies (I know not the best, but I learned quickly with it....) I think it only mentioned pointers somewhere. Cocoa Programming for Mac OS X covered memory management, but by using core data instead of malloc, useless if I want my code portable.
Macbook Pro 2.66Ghz dual core, 4GB ram, 512MB vram, MacOSX 9.1, Windows 8.1
Xcode 5.0.2, C++, lua
mfawcett
mfawcett
Quote:
Original post by bulgurmayo
a good link on memory management : http://www.memorymanagement.org/


...that is a great link for people who are writing their own memory manager, but NOT for someone who barely understands pointers.
--Michael Fawcett
MattJay
MattJay
Memory management largely depends on the operating system, and to a lesser extent the processor.

In general, memory allocation is simply requesting the underlying kernel to provide your task (or 'process') access to a range of memory for you to use. The reason you must request access memory is due to you are not the only process (or executive) running; everyone needs to play fair.

The most simplest way to think of memory is as a linear array of individual datums (most commonly 8-bit bytes), each byte has a unique numerical address which you use to refer to whichever byte you wish to read/write. (Technically, this view directly corrosponds to real-mode on the processor)

Pointers are simply an address of a byte.

--
Obviously if a program could simply address any byte it wished (such as in real-mode, mentiond above) the operating system would not only be prone to crashes due to misbehaving applications, but also at a security risk. That said, modern kernel's have their own memory architecture that sits upon the underlying process's memory system, abstracting the user application from the internals and also protecting it from corruption.

I could blab on all day about memory architectures and modes, but, just ask if you want to know anything more.

Wingtoad
Wingtoad
Hey, try reading this book.

http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

Its a free online C++ book that at least to me seemed to do a good job at talking about pointers and memory allocation and a whole bunch of other goodies as well. Its a little old, but its still C++, and most importantly, its free. You can also buy it in paper back, but the online one is free. Good Luck with your studies.

Wingtoad
Wingtoad

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.