Allocating and stuff

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

Recommended Posts

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.

Share on other sites
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 on other sites
Quote:
 Original post by thugkillaI 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 structstruct 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 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 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 - 220002. Player 2 - 120003. 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 yourselfint 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 on other sites
Thanks guys(escpically Drew)That really helped have you considered being a writer for books? :D

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
12

• 14
• 22
• 9
• 31
• 18
• Forum Statistics

• Total Topics
632617
• Total Posts
3007475
• Who's Online (See full list)

There are no registered users currently online

×