Re: Price of Creating Local Objects

From: David Hilsee (davidhilseenews_at_yahoo.com)
Date: 08/13/04


Date: Fri, 13 Aug 2004 00:05:04 -0400


"cppaddict" <hello@hello.com> wrote in message
news:ttdoh0hr2he6lovcu3v3mjigtdrcglujcu@4ax.com...
> >How large is large? 50 bytes? 1 kilobyte? 1 megabyte?
>
> About 1 kilobyte.

That's probably nothing to be concerned about. I've dealt with C programs
that, in nearly every function, allocated around 500 bytes just for a
temporary sprintf() buffer.

> >In general, variables that are declared locally within a function are
> >allocated quickly. If the object were monstrous, then I could see how
one
> >might want to avoid declaring it as a local variable to a function, but
that
> >generally deals with issues of <implementation>stack
space</implementation>.
> >I wouldn't worry too much about what actually happens. Instead,
concentrate
> >on questions like "How fast is it?"
>
> I was hoping to get a feel for "How fast is it?" by understanding what
> goes on behind the scenes. Of course, I could just time it and see,
> but then I would have no insight. I'm actually pretty sure that speed
> would not be an issue, but only because I vaguely know that vaguely
> similar code has executed quickly.

If you really want to know exactly what is going on behind the scenes, you
should examine the implementation. If you're more interested in how fast it
executes, then measurements will be more helpful. In general, it is
relatively quick, because local variables usually result in some fiddling
with the stack, which is usually a cheap operation. Of course, any comments
mentioning to a "stack" are referring to a possibly common implementation.

> > If you're concerned that
> >the program might fail because the object is not a static member of a
class,
> >then, by all means, consult the implementation.
>
> That is not a concern. Is there any reason you think it should be?

Well, as I mentioned before, there can be a "stack space" exhaustion issue,
but that's not usually a problem for most applications. It's more of a
problem for applications that use highly recursive algorithms.

-- 
David Hilsee


Relevant Pages

  • Re: Scheme Virtual Machine
    ... Minimal c/c++ made core for the executable file everything else in the ... I had a similar idea in mind when writing my Implementation of scheme ... The VM uses a stack to pass arguments. ... executes an if operation with a 'then' and an 'else' branch. ...
    (comp.lang.scheme)
  • Re: DEFCON 16 and Hacking OpenVMS
    ... the overflow that executes the code in the logical. ... that these exploits work by finding some OS-managed storage which is ... stack based buffer overflow scribbling over a return address. ... associated RTL routines are a NICE idea, one day maybe Billco and UNIX ...
    (comp.os.vms)
  • Re: Exception Handling - help!
    ... described it...the first pass searches for a handler, executing user filters ... Since the stack has not yet been unwound then any methods ... original exception occurred (this could be of use to a clever tool ... After the user filter executes, if it returns a value that tells the runtime ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Item advice needed.
    ... search I mentioned or trying it in the mines despite my mentioning ... I tried the search you recommended, which was simply "if you google ... Googling "stack" turned up nothing similar ... and none of them suggest such a bug. ...
    (rec.games.roguelike.nethack)
  • Re: C-programmer needs Forth advice
    ... You do not translate C code expression by ... Ordering parameters for minimum stack ... Native code compilers (NCC) can use register allocation in the ... Forth applications are often less than half the size ...
    (comp.lang.forth)