|
|
Chris Cason <del### [at] deletethistoopovrayorg> wrote:
> In the case of FixedSimpleVector, that's more difficult than it seems. The
> hard-coded limit is needed because that class is explicitly designed to use
> stack for storage, rather than the heap, which thus means the compiler
> needs to know its size at build time.
The POSIX standard defines the alloca() function which works like malloc
but allocates space on the stack instead of the heap (and the allocated
space, being on the stack, is automatically freed when the function is
exited). This is often much more efficient than a new/malloc. Of course
there are drawbacks:
- It's "only" POSIX, not C/C++ standard, so it's not really portable.
- It allocates space on the stack of the current function being executed,
which means that it has to be called explicitly everywhere where you need it
(which means you can't create eg. a utility class that would use it, because
any member function of that class that would use alloca() would not work,
as the space would be allocated on *that* function's stack space, and hence
freed immediately after the member function returns).
The newer C standard somewhat circumvents the need for alloca() by
allowing stack-allocated arrays which sizes are determined at runtime
(in other words, doing effectively what alloca() does). However, this
is not part of the C++ standard.
So the problem is not a trivial one to solve.
--
- Warp
Post a reply to this message
|
|