Pointers

Started by
16 comments, last by Trienco 11 years, 1 month ago

I get them somewhat. But what is the difference really with something like this?


int *pnValue = new int;
*pnValue = 7;

and

int nValue = 5;
int *pnValue = new int;
pnValue = &nValue;

With the first being fine but the second isnt.

Advertisement

Once you overwrite the value in pnValue you got from operator new without storing it somewhere you have no way to delete it, so you have a memory leak.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

So the first is changing the value at the address while the second is changing the address?

While not directly related to the question, I suggest also you to take a look into std::vector (and other std:: containers). Std::vector is a handy solution for most of the cases where you need array allocation, deallocation (or even resizing). Cleaner / shorter code, less strange memory bugs, less memory leaks, all that a programmer needs.

Not always because you still get a raw pointer back from e.g. glMapBuffer(Range).

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Sort of... pointers always store an address, and that's what new returns... but a pointer is just a variable whose value is an address...

If you allocate something with new (or new[], or malloc, calloc), it is the programmers responsibility to keep hold of the address returned somewhere and delete it (or delete[], free, and you must use the corresponding deallocation function/operator as well) exactly once, or else that is a bug.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Ahh okay. So i read an article about deallocation and whether or not you need to perform it. With the OS just releasing the memory when the program ends. Just wondering about this.

That does happen (memory is released by the OS) but people will be having so much fun playing your game they will never quit to the OS. So don't do it ok?

If a function leaks memory a bit but you call it a lot it's going to start giving you problems (and more importantly, hard to track down bugs).

So the usual answer is: always remember to delete stuff you allocate. If you need to do an emergency exit (e.g. calling abort() or exit()), then you can relax this a little. But you're not going to be calling abort() or exit() very often.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Ahh okay. So i read an article about deallocation and whether or not you need to perform it. With the OS just releasing the memory when the program ends. Just wondering about this.

To illustrate. I'm sure you have been playing games that started stuttering after playing for an hour or suddenly took forever to load things, showed random bugs or flat out crashed. That's what you get when a programmer is being sloppy and/or, figures "the OS will clean it up in the end anyway". If you don't want to worry about cleaning up what you don't need anymore, don't use C++ (or at the very least make use of smart pointers and standard containers). Though that doesn't mean languages with a garbage collector will magically take care of everything in every situation.

I'm a little surprised there are actual articles discussing this. Unless they compare different languages, that's a bit like "filling your tank... is it really necessary?".

f@dzhttp://festini.device-zero.de

This topic is closed to new replies.

Advertisement