Jump to content
  • Advertisement
Sign in to follow this  

Allocating and stuff

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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
Can i get an example for likes games?and I'am not familiar with new..and delete..

DAM I FEEL like an asshole waisting you time guys.

Share this post

Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!