memory management - Do c++ values created in an initialization list go on the stack? -


in other words, following code valid? -- works gcc 4.7, however, not sure in standard or implementation dependent.

struct {     int data[3] = { 1, 2, 3 }; };  struct b {     b(int data[])      ...  struct c : public b {     c()          : b(a().data)      ... 

i think question misleading.

struct {     int data[3] = { 1, 2, 3 }; }; 

a new feature of c++11, see [class.mem]: syntax initializes data member data during construction of object of type a. has nothing initializer-lists, it's language feature (list-initialization using braced-init-list).

it's equivalent to:

struct {     int data[3];     inline a() : data{1,2,3}     {} }; 

therefore, data on stack if you've created instance of a on stack, , it's on heap if instance has been created on heap.

i guess data initialization resides (= 1,2,3) implementation-defined.

as michael crawford points out, there might issues calling b(int data[]) (which decayed b(int* data)), don't make program ill-formed (it should still compile).


as passing a().data ctor of b:

a() creates temporary object; lifetime specified in [class.temporary]/3

temporary objects destroyed last step in evaluating full-expression (1.9) (lexically) contains point created.

using them in mem-initializer-list no exception; full-expression b::b( a().data ). full-expression includes ctor of b, temporary introduced a() , data member data destroyed after the ctor b::b has been executed.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -