|
 |
Invisible <voi### [at] dev null> wrote:
> I always used to think that creating a data structure who's size is not
> statically known required handling it by reference. However, C++ somehow
> manages to do this by value, so it seems my assumptions are incorrect.
No, dynamically allocated objects can only be handled with a pointer.
You can of course wrap that pointer so that it looks and behaves like
the allocated object itself, but as if handled by-value (including being
scope-bound).
For example you can handle an instance of std::vector or std::string
by value, but internally they contain a pointer to dynamically allocated
memory (which they manage).
> In fact, thinking about it, you probably /could/ implement Haskell
> without GC. It would just be far less efficient, that's all.
It would be less efficient because it would require always deep-copying
data when it's passed around?
> Anyway, as an example: Consider the function
> linear a b x = a*x + b
> You probably think that this function takes three numbers and returns a
> number computed from them. Strictly speaking, this isn't true; in fact
> this function takes three numbers and returns /an expression/ containing
> them. What the caller actually gets back is a reference to the root of
> an expression tree. For example, if I do "linear 2 3 5", I get back a
> reference to the root of a tree that looks like
> +
> / \
> * 3
> / \
> 2 5
> If I try to /use/ this value, this will execute the (+) function. The
> first thing this does is inspect its arguments, which causes the (*)
> function to execute. After it executes, the tree looks like
> +
> / \
> 10 3
> 2 5
> There are now two items of garbage: the 2 and the 5. Further, after (+)
> finishes executing, we have
> 13
> 10 3
> 2 5
> So now there's 4 items of garbage (2, 5, 10, 3).
> The real situation is of course a bit more complicated than this, but
> you get the idea.
It looks like a really inefficient way of computing a simple calculation.
--
- Warp
Post a reply to this message
|
 |