ClassPointer vs. ClassInstance
-
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
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 11/08/2012 at 03:58, xxxxxxxx wrote:
Hi ScottA,
I think you should use MyClass mc; as much as possible.
Using gNew or new increase memory leak possibility and is not easy to use.
If you really need to use gNew or new then you should use smart pointers like AutoNew , AutoPtr or other from ge_autoptr.h .
One can also use unique_ptr or shared_ptr from C++11 in some cases.What kind of errors do we get if we got beyond the available stack memory?
You will get Stack Overflow.
Am I safe if I cheat and take the easy way out. And always use instances?
You do not cheat! Yes you should be safe in most cases.
Of course if you do something like this then you can still get Stack Overflow.class MyClass { Vector my_big_stupid_grid[1024][1024]; }
Obviously the better way to do this is:
class MyClass { c4d_misc::BaseArray<Vector> my_big_grid; //then access as 2d grid. }
or
class MyClass { std::vector<Vector> my_big_grid; //then access as 2d grid. }
Actually you can even use sizeof(MyClass) to see if it is not too big.
But this should be not necessary if you classes uses heap for big memory blocks.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.
They are good
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.
You can use VMMap from Sysinternals on Windows if you really want to see all the memory usage.
One important exception are (deep) recursive functions.
1: Do not use them. (can be usually replaced with some kind of loop)
2: If you really want to use them then try to use as lees stack memory as possible, preferable zero.Remo
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 11/08/2012 at 09:12, xxxxxxxx wrote:
Thanks Remo. That helps to put my mind at ease about using instances instead of pointers.
I'm still learning about the stack and the heap.
In the example you posted : BaseArray <Vector> my_big_grid;
Is the array created on the heap or the stack?-ScottA
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 11/08/2012 at 10:11, xxxxxxxx wrote:
In the example you posted : BaseArray <Vector> my_big_grid;
Is the array created on the heap or the stack?I mean c4d_misc::BaseArray <> this is new and is from C4D R14.
And of course is uses heap, to be more precise GeAllocNC() .What I wanted to say is do not use big stack arrays in you classes.
Like thisclass MyClass { Vector array[512]; //too big }; class MyClass { std:array<Vector,512> array; //too big too };
Try to avoid them and replace with dynamic/heap arrays !
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 11/08/2012 at 10:37, xxxxxxxx wrote:
Gottcha.
I will follow this rule:
Don't use deep iterations in classes.
And don't set hard coded array sizes(especially large ones) by hand in classes. Because that will end up on the stack. Instead of the heap.Thanks for the lesson.
-ScottA