Basic memory question
a = malloc(10);
std::cout << "baz" << std::endl;
a = malloc(size);
std::cout << "bar" << std::endl;
foo *y = new foo;
I presume that the destructor in this case will destroy x when we are leaving the scope of bar, eventhough it's allocated on the heap, but what happens with y in baz where I use new? The constructor presumably takes care of the allocation, is the whole object a bit larger that the malloced memory in this case?
Is there something else besides malloc I can use here?
The foo object x is located on the stack, not the heap. The memory X allocates internally for pointer "a" is on the heap. When exiting bar, the destructor will be called freeing the memory on the heap pointed to by "a". X itself will then be popped off the stack.
With the function baz, the pointer itself "y" is on the stack, but the memory for the foo object that "y" points to is on the heap and again, the memory that this foo object on the heap allocates for the pointer "a" also comes from the heap. When delete is called on the pointer "y", the destructor for that object is called which once again will free the memory allocated and assigned to the pointer "a". The foo object pointed to by "y" will then be released and finally at the conclusion of the function baz, the pointer "y" will be popped off the stack.
A danger here is if you were to try and malloc that foo object in baz. Neither the constructor nor the destructor will be called if you use malloc/free. You should definitely stick with new/delete.[/edit]
Thanks hk_mp5kpdw, I figured something like that but the reason I had some thoughts on where the object would be placed in this case, is that the class contains nothing but the memory area of a in this case.
What options besides malloc is there for an arbitrary sized memory area then? Can i use new with  uninitialized?
Why do you want a blob of uninitialized heap memory?
In this case to gain understanding, I'm just fooling around really. But for this purpose inside a class is there something that is preferable to malloc in C++, I found some info about Allocator() in STL earlier but just skimmed through it briefly.
I believe a one-dimensional array can be initialized with a variable, such as
Where the size y will be evaluated at runtime, and could be received from input.
int y = 16;
char c = new char[y]
The better question would be, what isn't preferable to malloc, in C++!
Originally Posted by Subsonics
In C++ you use new and new, and these make malloc at best redundant or even harmful in many cases.
In a proper C++ program you neither use malloc, nor void pointers. Actually you seldom need new either, as std::vector is there to handle that for you.
You should also never write a class that disobeys the rule of three.
That certainly looks live Java.
Originally Posted by neandrake
In C++, new returns a pointer. It cannot be converted to a char array.
So it should ne
char* c = new char[y];
delete  c;
But again, it's better to use a vector.
It's not a better question if you want to find out what is preferable. :)
Originally Posted by iMalc
Point taken, "thou shalt never use malloc and free".
Originally Posted by iMalc
Any takers on allocators in STL?
What about them? Do you understand what they are and what they do?
Not really, I have looked over it very briefly. As I understand they offer something similar to what we are talking about here.
They don't. They control how containers allocate and destroy elements. They also need to allocate memory via new/malloc.
So the most basic means available for this is new , then. New and delete certainly seems to give me a safer and easier to manage interface than malloc and free, even though it all boils down to malloc and free in the background, so I'm not complaining. Just like to get some feedback on my initial ideas in this area, it help to gain understanding of this for me. Thanks.
Originally Posted by Elysia