[Lemon-user] Some beginner confusion...
Alpar Juttner
alpar at cs.elte.hu
Mon Mar 30 22:46:55 CEST 2015
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.
> 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.
> 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.
Regards,
Alpár
More information about the Lemon-user
mailing list