POV-Ray : Newsgroups : povray.off-topic : Teach yourself C++ in 21 days : Re: Days 5- Server Time
29 Jul 2024 10:30:23 EDT (-0400)
  Re: Days 5-  
From: Darren New
Date: 28 Apr 2012 13:38:55
Message: <4f9c2b2f$1@news.povray.org>
On 4/28/2012 10:14, Warp wrote:
>    If your stack can't hold hundreds of recursive function calls, then
> you should upgrade your computer to the new millenium.

Well, that's what I said. If I have a machine with enough power to support 
that sort of thing, I'm probably not going to be using C. :-)

PS: Pro-tip. I work for google. Don't try to spider the web recursively. ;-) 
As the machines get bigger, so do the problems people throw at them.

But you have a database with a B-Tree index? Sure, chances are you're not 
going to blow the stack walking down that tree.

I'm not saying recursion isn't useful. I'm saying that one must keep in mind 
the possible depth of recursion, because C doesn't tell you when you overrun 
the stack. You just get the same random errors you get when you run off the 
end of an array, except you haven't actually violated the spec, as far as I 
know.

>> Maybe I'm just used to working on machines with actual limited address space
>> and RAM. 50 levels of recursion sounds like a lot of stack space used, to me.
>
>    What? Even if each function call took like a kilobyte of space (which is
> quite a lot for one single function call), it would still be just 50 kB.
> I think stack sizes are counted in megabytes in modern systems.

Depends on the system. Credit card terminal? No. Last card terminal I worked 
on had 14K of space free for code and data and stack. :-)

In any case, would you call malloc() inside each level of recursion as you 
go without checking whether malloc() returned NULL? I wouldn't think so. So 
if malloc() can fail and you want to check, why not recursion? Why not just 
use the returned value from malloc() and let it crash out when you try to 
write to offset 0?

> doing it wrong.

Yep. As I said, that's why people don't do it that way.

Note, however, that we're also comparing to things like Haskell, where the 
"naive recursive algorithm" *is* the iterative algorithm.

>    I suppose the standard probably as a minimum recommended amount (like
> it has for many other things, like the length of variable names). However,
> if you surpass that limit your program won't even compile, so it's not
> a question of running out of memory when running the program. (And most
> C++ compilers have an option to increase the space dedicated to type
> names.)

Yep. All to get around problems with naive recursive algorithms.

In any case, that's the sort of thing I'm talking about. If you add a level 
of template indirection and your compiler crashes, that's one thing. If you 
add another web page to the list of web sites you're selling ads on and your 
revenue stream collapses, that's another thing.

Hope that you don't wind up adding another level to your templates and your 
compiler finishes but generates the incorrect code.

-- 
Darren New, San Diego CA, USA (PST)
   "Oh no! We're out of code juice!"
   "Don't panic. There's beans and filters
    in the cabinet."


Post a reply to this message

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