POV-Ray : Newsgroups : povray.off-topic : You lose some... : Re: Some code Server Time
7 Sep 2024 03:22:16 EDT (-0400)
  Re: Some code  
From: Slime
Date: 5 Oct 2008 06:00:38
Message: <48e89046@news.povray.org>
> const Tree& build_tree(std::vector<SymbolInfo> info)
> {
>   std::vector<Tree> trees;
>
...
>
>   return trees[0];
> }

You are returning a reference to a local variable. When the function 
finishes, trees' destructor will be called and all of its contents will be 
deallocated. So you're returning a reference to invalid memory. If you're 
not worried about copying Trees around, you can just remove the & from the 
return type.

You might also want to assert( trees.size() > 0 ) to make sure the function 
worked properly and there's something there to return.

That's the only actual problem I see. Otherwise I'm a little worried, 
efficiency-wise, about how many vectors you're copying around, but that may 
not be worth worrying about at this stage.

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.