Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Advantage / disatvantage "new"

Author
Message
TheComet
16
Years of Service
Recently Online
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 24th Apr 2012 09:21 Edited at: 24th Apr 2012 10:52
Hey all


What is the advantage/disatvantage of using the keyword new when creating an object? There are two ways of creating an object, both examples are below:





TheComet

WLGfx
16
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 24th Apr 2012 13:33
If you have a large class or an array then usually the new operator is preferred as it allocates the memory from the heap at run time. Also to note is that an un-initialised array or class will be assigned to the un-itialised data segment of your program (ie taking no space in the exe) and usually has random data in it and will need to be filled by your program anyway. Where-as is you do something like MyClass myObj = { 10,10 }; then that will be stored in the initialised segment of your exe.

There's advantages to all cases. Allocating memory at run-time is useful for data that is going to be used temporarily (deleted, re-initialise, etc) were having initialised data is more local to your code, although this very rarely makes much difference in terms of speed these days.

I don't think there's a standard of how you setup your classes, but in some cases of C++, the constructor will allocate memory and the destructor will free it.

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
TheComet
16
Years of Service
Recently Online
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 24th Apr 2012 14:26
Thanks for the quick response. So let me see if I understood you correctly. New allocates memory at runtime for your object, where something like MyClass MyObj; stores it in static memory? Or do I explicitely have to say static MyClass MyObj;?

That's where I'm still a little unclear, because you are able to use MyClass MyObj; at runtime, so is the memory for that also allocated on the heap?

TheComet

Hassan
14
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 24th Apr 2012 15:11 Edited at: 24th Apr 2012 15:13
one of the biggest advantages is the scope, objects allocated using the new keyword won't be destroyed as soon as they go out of scope, unlike the stack allocation.

WLGfx
16
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 24th Apr 2012 15:21 Edited at: 24th Apr 2012 15:27
If you use MyClass myObj inside a function then it dissappears when the function ends because it uses the stack to store the local data. If you use new inside a function, the pointer will dissappear when the function ends but the allocated memory, unless you 'delete' it stays and if not checked can cause memory leaks.

ie:


^ hoping that's correct

EDIT: you can use MyClass myObj at runtime because if it's in a function then it uses the stack, if it's outside of all functions then its in the un-ititialise data segment.

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
TheComet
16
Years of Service
Recently Online
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 24th Apr 2012 16:08
Aah, that makes a lot of sense now! Thank you all for clarifying.

TheComet

Login to post a reply

Server time is: 2024-04-23 08:55:44
Your offset time is: 2024-04-23 08:55:44