You are not logged in.

#1 2011-05-05 14:39:42

Registered: 2011-05-04
Posts: 8

[Solved]C++ and dynamic allocation

Hey, I've been pondering this for a while:
if I have a func that accepts a pointer to struct as argument and I allocate the struct on the function call, like this:

void func(mystruct *foo) { /* do stuff */ }
func(new mystruct(constructor params));

Is the struct deallocated automatically or do I have to deallocate it manually when I don't need it anymore?

Also, if I
return new mystruct(constructor params);
will that pointer get dereferenced and not work?

Last edited by KoutaOyamada (2011-05-05 15:01:26)


#2 2011-05-05 14:58:22

From: Medford MA USA
Registered: 2009-12-15
Posts: 479

Re: [Solved]C++ and dynamic allocation

Question 1: No, 'new' pointers are never automaticaly deallocated. You must do it. Or look into 'auto_ptr'. Or don't use new, use a reference:

void func( mystruct & foo )
{ ..}

mystruct bar(construct,parameters);

2: If your return a 'new' pointer, it will work just fine no matter who has it. But someone, somewhere, must delete it.


#3 2011-05-05 15:01:01

Registered: 2011-05-04
Posts: 8

Re: [Solved]C++ and dynamic allocation

Thanks! I'll mark this as solved :]


#4 2011-05-05 15:20:12

Registered: 2009-04-14
Posts: 278

Re: [Solved]C++ and dynamic allocation

Memory allocated via 'new' always suffers from the problem of unknown ownership.
There are various ways to craft your software to make it as clear as possible,
but in my opinion, smart pointers are the only valid one. They clearly express
who owns a pointer and when it is going to be destroyed.

Read about std::auto_ptr, boost::shared_ptr, boost::intrusive_ptr,
boost::weak_ptr and boost::shared_array to see what options are available to

Btw, this is valid for more than just memory allocation. Generally, you should
make sure every resource you acquire from the system is properly released when
you're done with it, even if exceptions occur.


#5 2020-07-04 16:02:47

Registered: 2020-03-22
Posts: 20

Re: [Solved]C++ and dynamic allocation

So I don't mean to necro bump this but I think my question is important for beginners to both Arch & C++.

I recently got back into C++ coding and kinda assumed the OS handles de-allocation or freeing the heap when a program is terminated. So should I bother tracking down those bytes that were never de-allocated? if so how should I go about this? Luck for me I just very recently got back into C++ & remember mostly what I've done & I've not ran any fancy/complicated or big programs (less than 300 lines of code). I've opened, ran and terminated this program probably only 3-4 times and it contains like one or two pointers in total.

I can hop on my computer and paste the code, it's just a fun project that administers a "big five" personality test. Very basic cout then cin some value then some basic math for score calculations.

Warning recovering help-vampire:
Do not feed me help (July, 8th 2020)


#6 2020-07-04 16:13:30

Forum Moderator
Registered: 2012-01-15
Posts: 3,240

Re: [Solved]C++ and dynamic allocation

Rather than necrobumping a nine year old solved thread, please start a new thread with your question and link back to this one if you think it still applies. … bumping%22



Board footer

Powered by FluxBB