troubleshooting Question

Local Var

Avatar of NoobSabot
NoobSabot asked on
C++
5 Comments1 Solution305 ViewsLast Modified:
Most of the time, it's not a good idea to return a pointer / reference to a local variable.

function
    returns ptr

main
   calls function (stack memory might get corrupted)


But what about this example. Is it the same thing

struct Dlist {
    DList *prev
    DList *next
}

main {

    Dlist head;
    Dlist last(head);
    while loop {
        Dlist temp;
        last.next = temp;
        last = temp;
    }

}


The var temp goes out of scope at each end of the loop, but it's the same function. How does memory allocation work here ?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros