You are not logged in.
Pages: 1
Topic closed
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)
Offline
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);
func(bar);
2: If your return a 'new' pointer, it will work just fine no matter who has it. But someone, somewhere, must delete it.
Offline
Thanks! I'll mark this as solved :]
Offline
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
you.
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.
Offline
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)
Fully Recovered help-vampire (at some point late 2021)
Linux journey (approximately) Ubuntu -> Arch -> Debian -> Manjaro -> NixOs -> (attempted: unresolved hardware issues) gnu guix -> Gentoo -> Fedora -> Arch (current)
Offline
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.
https://wiki.archlinux.org/index.php/Co … bumping%22
Closing.
Offline
Pages: 1
Topic closed