THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 10/08/2012 at 18:45, xxxxxxxx wrote:
User Information:
Cinema 4D Version: R12
Platform: Windows ;
Language(s) : C++ ;
---------
Hey Guys,
I have a rather newbish question about class instances.
I don't use my own classes with C4D plugin very often because the current SDK classes generally give me everything I need. But when I do use them, I was wondering about which way to create the instance of the class in C4D plugins.
It's my understanding that instances ( Examle: MyClass mc; ) are created on the stack. So the advantage to doing this is when it goes out of scope memory is automatically freed up for me.
And class pointers ( Examle: MyClass *mc = gNew MyClass; ) are created on the heap. Which requires manual freeing them from memory using gDelete(mc);.
It's also my understanding that the stack might not be large enough to create a class instance.
So here's my question:
Since we get memory freeing for free using instances. I'd prefer to use that method as much as possible to keep handling memory simpler. But how do I know if the class I'm trying to instance is too big for the stack?
What kind of errors do we get if we got beyond the available stack memory?
Sometimes we can get away with doing things in C4D plugins that might not be as ok in general programming. Like using global variables because each plugin can never use another plugin's variables.
So when creating custom classes in C4D plugins. Am I safe if I cheat and take the easy way out. And always use instances?
Or should I get into the habit of always using pointers to create new class instances. And force myself to always free the memory myself?
I'm trying to keep things as simple as possible by using class instances. And not have to worry about freeing memory by hand. But I don't want to use them if they are bad.
Plus. Right now I don't even know how to see how much memory I have on my stack. And tell if my class runs the risk of being too large for it.
-ScottA