Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2005

Tip: Looking for answers? Try searching our database.

recursion & object life

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ido - 15 Oct 2005 08:01 GMT
Hi!

Please read the code snippet below. What it is supposed to produce a chain:
first_node(value = 0) --> next_node(value =5) --> next_node(value =10) -->
next_node(value = 15)

However, I fear that objects created in deep levels of recursions are
ultimately detroyed, although they are returned one level up. So, please help
me if you know how to rectify this code and achieve the desired result. I
need all the objects created during the recursions to be alive when I get
back to the top level.

Thanks in advance,
Ido

===========================================

class node
{
public:
    node * next_node;
    int value;
   
    node() {
    next_node = NULL;
    value = 0;
    };
};

node MyRecursive(node previous_node)
{
node current_node;
current_node.value = (previous_node.value + 5);
previous_node.next_node = &(current_node);
if (current_node.value < 20) MyRecursive(current_node);
else return previous_node;
}

int _tmain(int argc, _TCHAR* argv[])
{
    node first_node;
    first_node = MyRecursive(first_node);
    return first_node.value;
}
Steve Alpert - 17 Oct 2005 17:12 GMT
Your code is incorrect (and dangerous).  When you leave a method, any local
objects created (normally on the stack) are destructed.  You need to have your
routines use POINTERS to classes and create via a new operator.  That way, there
will not be any destruction until you specifically delete the object.

/steveA

> Hi!
>
[quoted text clipped - 40 lines]
>     return first_node.value;
> }

Signature

Steve Alpert
my email Fgrir_Nycreg @ vqk.pbz is encrypted with ROT13 (www.rot13.org) and spaces


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.