• Advertisement
Sign in to follow this  

Structure initialization

This topic is 3579 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there! Consider this two structures: struct s_1{ float x,y; }; struct s_2:s_1{ float z; }; One can initialize an instance of type s_2 this way: s_2 s_2_instance={1.0f}; // here only z will be initialized Is there any way to initialize fields x and y of s_2_instance at design time? Actually i rarely need this but as i'm very performance minded programmer (several years programming in assembly) i'd like to know the way for such initialization. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Jay I

Is there any way to initialize fields x and y of s_2_instance at design time? Actually i rarely need this but as i'm very performance minded programmer (several years programming in assembly) i'd like to know the way for such initialization. Thanks in advance.


If you were performance-minded, you'd write both examples in a real application, then examine the generated assembly. Or better yet, profile the real application. Right now, you just have an obsolete notion of performance.

This isn't meant in a derogatory way, it's just a common trait for assembly programmers to worry about a grain of sand while the sea is eroding the beach.

How many structures will you initialize, how often? 1? Per application life-time? Or 20 million on every loop, 60 loops per second? What kind of algorithm? Heap or stack allocates?

struct s_1 {
s_1(float x_ = 0, float y_ = 0)
: x(x_)
: y(y_)
{}
float x;
float y;
};

struct s_2 : public struct s_1 {
s_2(float x_ = 0, float y_ = 0, float z_ = 0)
: s_1(x,y)
, z(z_)
{}
float z;
};

int main() {
s_2 s_2_instance(1f,2f,3f);
std::cout << s_2.x << s_2.y << s_2.z << std::endl;
}



If you examine the generated assembly, you may find that the result will be equivalent to:
int main() {
std::cout << 1.0f << 2.0f << 3.0f << '\n';
}

Share this post


Link to post
Share on other sites
Hey, take it easy and be more attentive! Note that i used the phrase "design time" that means that memory is allocated when the program is being compiled. No malloc's or other runtime calls. Actually all what you guys posted was about runtime memory allocation. The only way i know to allocate (or reserve, if allocate is confusing for you) memory at design time in C/C++ is to define a global variable. When you use constructors it requires some code to be executed at runtime no matter how small constructors are. In assembly as in C/C++ 'design-time' memory is initialized using initializers. So my question may be transformed to this: is there any way to use initializers to initialize fields that are inherited and not explicitly defined in some structure type. I hope now you see that it's dumb to use a profiler or debugger to find an answer for a question about C++ syntax. ...For assembly programmers it's common to worry about every water drop caz it makes sence when your ocean becomes a pool.

Share this post


Link to post
Share on other sites
A programmer who has spent much of his time programming in assembly may come to assume that a compiler is a stupid beast who performs by rote and is incapable of making clever decisions. This is an apt description of an assembler, but not of modern compilers. Think of the compiler as a pretty good assembler programmer, who might not have the creativity to refactor a large-scale algorithm, but who at the very least doesn't crank out manifestly dumb code.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jay I
Hey, take it easy and be more attentive! Note that i used the phrase "design time" that means that memory is allocated when the program is being compiled.
The phrase you're looking for is "compile time".

Now to answer your original question. Brace-enclosed initialiser lists can only be used to initialise aggregates and scalar types. Aggregates cannot have base classes. So no, there is no way to initialise the members of a base class using brace-enclosed initialiser lists.

Σnigma

Share this post


Link to post
Share on other sites
Thanks Enigma. Short and simple answer. What about my phrase 'design time' it is used interchangeably with 'compile time' in lots of docs and specs and means absolutely the same.

Share this post


Link to post
Share on other sites
Design-time is the UML charts and non-code thing, pencil and paper, Visio.
Compile-time is the pre-processor/template instantiation/assembly generation/compile-time polymorphism.
Run-time is dynamic polymorphism and stuff that is evaluated when code is running.

Share this post


Link to post
Share on other sites
The reason nobody answered your question is because they were trying to help you with the actual problem that led you to ask your question. IMO helping you get a better idea of how C++ works and giving you tips to use it effectively is infinitely more valuable than saying "no you can't do that" and sending you on your way.

Share this post


Link to post
Share on other sites
And with C++09 incoming: C++09 will have brace initializer lists options for non-aggregates.

Share this post


Link to post
Share on other sites
The only thing i needed to know was whether one can initialize inherited fields with initializers and no more so all your assumptions about what led me to the question are automatically wrong. However thanks for your help. I got the answer. Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jay I
When you use constructors it requires some code to be executed at runtime no matter how small constructors are.

Incorrect.

Here's Antheus' code corrected for compiler errors containing a global initialized with a constructor.

struct s_1 {
s_1(float x_ = 0, float y_ = 0)
: x(x_)
, y(y_)
{}
float x;
float y;
};

struct s_2 : s_1 {
s_2(float x_ = 0, float y_ = 0, float z_ = 0)
: s_1(x_, y_)
, z(z_)
{}
float z;
};

s_2 s_2_instance(1.f, 2.f, 3.f);



Here's the assembly generated for s_2_instance by MSVC 2003 in release mode for s_2_instance:

PUBLIC ?s_2_instance@@3Us_2@@A ; s_2_instance
_DATA SEGMENT
?s_2_instance@@3Us_2@@A DD 03f800000r ; 1 ; s_2_instance
DD 040000000r ; 2
DD 040400000r ; 3
_DATA ENDS
END

Note the complete lack of anything done at runtime. All the values are, in fact, initialized at compile time.

This fact you could have easily determined for yourself had you actually listened to people rather than ignoring them when they didn't tell you exactly what you wanted to hear.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement