Dynamic Memory use and explination?

I have been going back over C++ and there seems to have been a lot of stuff that I didn't cover. I learned it from TheNewBoston so I blame him for not finishing his tutorial series. So while watching another video tutorial series on C++ I discovered something called 'Dynamic Memory'. I asked for help on the video a few days back but no luck. So I thought I would ask here for help.
I kind of understand Dynamic Memory but at the same time I just don't get it. Maybe because if it is what I am thinking of I cant think of a use for it for what I want to do which is game development. Correct me if there is a use for it in game development. Anyway, back on topic. Is dynamic memory just allocating memory to a variable at runtime? So if I were to do int *x;, that would allocate memory to it at compile time or before the program runs. I use a pointer because from what I know, at least for now, you can only use a pointer for this. But if I were to do int *x = new int;, that would give it the space of an integer (which for me is 4 bytes) variable at run time. So basically, is this:

int *x;

the same as this:

int *x = new int;

only the second one allocates memory at run time? If so then this was a whole lot easier than I thought and I was just over thinking it. Could you also give me a practical use for this in game development if you know of one? Thanks for reading :D
Thanks for the reply, but the documentation doesn't really make much sense. I don't understand what they are talking about. The way they have worded it doesn't make sense to me =(
Last edited on
You're getting there.

int* x; //Just a pointer. Holds a 4 (or 8) byte address. Stores address of an int.
int* y = new int; //Also just a pointer. Holds a 4 (or 8) byte address, though this time it is initialized to point to a place in memory, returned by the new call.
//So, these two are not the same thing.

//Can also do:
int x = 10;
int* y = &x; //Now y holds the address of x. You can point to objects on the stack just as you can objects on the heap. 

This is used all the time. Often it's not necessarily known at compile time how much memory you are going to need, so this is where dynamic memory comes into play. Ever use std::vector? That's just using a dynamically allocated array in the background.

thanks! This helped :D So int* x; points to the address of an integer, but int* y = new int; points to a place in memory? I understand what you mean but I just cant really explain it. Again, thanks for the help! :D
Sort of. int* x; points nowhere. It's not initialized. Just like int y; has no value. In all reality, both of these just store data. It's just how the computer interprets what the data is that defines a data type. If you wan't x to point somewhere:
int* x = &some_var; //Do it at declaration
int* y;
y = &some_var; //Do it after declaration
int* z = x; //Can also directly assign one pointer to another. 
Ok thanks for the help! =) So int* x; points to nothing (a null pointer) but int* y = new int; has no value but points to a memory address? I tried running it and it printed out a string of numbers that looked different from a normal memory address. It began with a hyphen. It was also the same numbers each time I print it out. Thanks for all your help =)

EDIT: Is this true:

If you make a pointer like this

int *x;

It just points to a null value (it points to nothing)

but if you do

int *y = new int;

it has no value but it sets memory aside for an integer value if you enter one later!
Last edited on
Topic archived. No new replies allowed.