static array of structs clarification questions
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Feb 12 13:30:14 PST 2016
On 12.02.2016 22:08, WhatMeWorry wrote:
> question #1: The static array must contain the fat pointers to str
> variables. But where is the string data itself actually held: the stack?
> the heap? somewhere else? (does it vary depending on location or scope)
Depends on how the string was created. You can create dynamic arrays
over any memory. (Remember: string is an alias of immutable(char)[],
i.e. a dynamic array.)
I'm not sure where strings from literals are located. Could be some
static data section in the executable, or some such. That's beyond me.
> question #2: If the above struct was to contain the same struct and the
> second one contains a third, how would the lower structs be allocated?
> Is it "turtles all the way down?
Struct data is put right where the variable is. Unlike classes and
arrays, structs are not references to some other location.
When a struct has a struct member, then the data of the member is put
right next to the parent's data. The size of the member is added to the
parent's size.
One consequence of this is that you can't have trees with just structs:
`struct Node {Node left; Node right;}` - not gonna fly.
> question #2: Of what use is the nulls in the array elements? When I took
> out the member function: void info(), the nulls went away.
My guess is that you declared the struct in a function (e.g. main), and
the null is the context pointer. Put the struct declaration at module
scope, or make it `static`, and the null thing should go away.
A context pointer is needed when the struct references data from the
surrounding function scope. You don't do that here, but the compiler is
apparently not smart enough to figure that out.
More information about the Digitalmars-d-learn
mailing list