[Lemon-user] Some beginner confusion...
degski
degski at gmail.com
Tue Mar 31 06:40:58 CEST 2015
Thanks for your response, this clears up things alot for me...
On 30 March 2015 at 23:46, Alpar Juttner <alpar at cs.elte.hu> wrote:
> Hi,
>
> > lemon::ListDigraph dg1;
> > lemon::ListDigraph::Node n1 = dg1.addNode ( );
> > lemon::ListDigraph* dg2 ( new lemon::ListDigraph );
> > lemon::ListDigraph::Node n2 = dg2->addNode ( );
> > [...]
> > Where are n1 and n2, respectively allocated (stack and heap?)...
>
> (Assuming this code is inside a function block) They are both normal
> local variables, therefore they are allocated on the stack.
> In fact, they are simple classes containing nothing else but a single
> integer (the id of the node it referres to).
> Therefore --- when compiler optimization is on -- the chances are that
> the whole allocation will be optimized out and they will be stored
> directly in a CPU register.
>
> > and what are they, pointers, references or objects?
>
> Objects (classes), see above.
>
> > Somewhere in the user-list-archives there's a post asking about
> > constructing large graphs, with an advice to use the heap (case 2) and
> > std::vector for (automatic) memory management...
>
> It's probably a misunderstaning. This comment - I guess - was referring
> to the use case when you need to allocate a large number of graphs. The
> size of the graph does not matter, because the actual contents of the
> graph will always be stored in the heap, even is the graph was allocated
> on the stack (as a local variable). This is analogous to std::vector<>
> and similar containers.
>
This is the info that I was seeking... and worrying about (...because the
actual contents of the
graph will always be stored in the heap...)! So with a couple of graphs,
the stack is perfectly fine .-).
>
> > When using dg2, does everything that is attached to it (f.e. NodeMap)
> > end up on the heap?
>
> Yes, but same happens in case of dg1, too.
>
> > When doing:
> > lemon::ListDigraph::Node n1 = n2;
> > has anything happened to either graph?
>
> No, nothing.
> Nodes are created only when you call addNode().
> n1 and n2 are not the node itself, but a kind of reference to it. Each
> graph-node has an ID, and type lemon::ListDigraph::Node just stores this
> ID.
>
I reckoned so much, but the documentation is totally silent about it...
>
> > Is n1 now a copy of n2, or is it now n2?
>
> See above.
>
> > Sorry for my ignorance. Thanks in advance for any enlightenment
> > offered!
>
> Don't worry, this mailing-list is for asking questions. I hope I was
> able to help.
>
This absolutely solved my questions... the message I get is: "Get on with
it, it's all properly done behind the scenes..." Thanks again... for this
great library...
Have a good day,
Degski
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lemon.cs.elte.hu/pipermail/lemon-user/attachments/20150331/b7659758/attachment.html>
More information about the Lemon-user
mailing list