Sign in to follow this  
thugkilla

Allocating and stuff

Recommended Posts

samuraicrow    325
Sometimes you don't know how many structures you will need in advance so you can't just statically declare a structure variable at the top of your code. Linked lists and trees are examples of times when this happens.

As for how you implement them it varies from language to language. In Java and C++ you have the New operator, in C there's malloc and free...

Share this post


Link to post
Share on other sites
Drew_Benton    1861
Quote:
Original post by thugkilla
I don't get the point of allocating memory can you tell me and show me how to do and what the hell it does.


Quick trivial example of why it's important.


#include <iostream>

using namespace std;

// 1kB struct
struct BigStruct
{
char str[1024];
};

void main()
{
// 10MB on stack, will crash program, uncomment to see
//BigStruct test[10000];

// 10MB on heap, will not crash (assuming you have at least 10mb memory lol)
BigStruct *test = new BigStruct[10000];
cout << "It worked ;)";
delete test;
}

Share this post


Link to post
Share on other sites
Drew_Benton    1861
We're here to help, so don't feel bad to ask a question!

A quick explanation of my last post is as follows: When you declare something in your compiler that is not a pointer, it is created on the "stack". The stack is a stack of memoery that can be used in very limited quantities before ruunning out. Think of it as cash in your wallet. Now the opposite of the stack is the "heap" which is all of the memory that you have on your computer to use. If you do memory allocations on the heap (using new/malloc) then you will have a much larget 'budget' to work with. Think of the heap as your bank account.

When you need temporary variables and variables that do not take up much memory space, you use the stack. You know, int x = blah, and string s = "name"; However, when you need to store a larger structure in memory, let's say an entire file, then you will want to use the heap.

I'll go with a more simple general game purpose example rather than getting into a game specific just because it'd be easy to refute.

Let's say you have a high score file that stores the names and scores of players.

1. Player 1 - 22000
2. Player 2 - 12000
3. Player 3 - 2000

Now you do not know how many total bytes you need for that file, since player names can range as well as the scores. If you were lazy about it, you could do something such as:

FILE *IF = fopen("hiscroes.txt");
char inFileData[1000] = {0};
fread( inFileData, 1, 1000, IF );

But what if that file got bigger than 1000 bytes? Then in that case you'd generate a memory fault hopefully and figure out why the program is crashing.

Now on the other had, if you used memory allocations, you could do something like this:

FILE *IF = fopen("hiscroes.txt");
// I just made this function up, it does not exist, you have to code it yourself
int total = GetFileBytes(IF);
char *inFileData = new char[total];
fread( inFileData, 1, total, IF );

That way, you can handle any sized file and not be wasting any memory.

Now that example is trivial and simple, but hopefully it gives you an idea of why you do need to worry about memory allocations and such. If you want a more game specific example, just apply that concept of reading from files to lets say, game maps. In C++, since you manage everything yourself, you must always make sure to properly free any memory allocated with new.

If you have simple games and such, you should not have to be as concerned with this topic. As you get more advanced, you will definitly want to get to know how to properly use this to your benefit. Note that this is a quite complex subject, so it will take some time to "sink in".

Of course there is *a lot* more to this subject than what has been discussed here. Take a look at some of these tutorials for additional information.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this