Does declaring a structure initializes it?

When I declare a structure instance is it initialized or do I have to make a constructor?

How much will the constructor affect the performance since I need an array of 1000 structures?

The structure I need is very simple and it contains 4 integers. I need them to be initialized to 0.

This is my code:

struct MyStruct { int a; int b; int c; int d; MyStruct() : a(0), b(0), c(0), d(0) {} }; int main() { MyStruct array[1000]; }

-------------Problems Reply------------

You are declaring a variable in main, but only as a side-effect of defining it.

And all objects are default-initialized, unless you ask for something else.

Still, that's not quite as much as you might thing. A default-initialized basic type, which is neither in static nor in thread-local storage, is still indeterminate after that.

But never fear, your type has a user-defined default-ctor, so default-initialization means that is called.
And as that value-initializes all members, everything is initialized.

If it was an aggregate, this would zero-initialize it:

MyStruct array[1000] = {};

Regarding performance, there's nothing to be done but measuring, and asking the compiler to optimize it. Keep in mind that the fastest code is code that isn't there.

Category:c# Views:0 Time:2015-02-02

Related post

  • What is tagged structure initialization syntax? 2010-06-10

    struct file_operations scull_fops = { .owner = THIS_MODULE, .llseek = scull_llseek, .read = scull_read, .write = scull_write, .ioctl = scull_ioctl, .open = scull_open, .release = scull_release, }; This declaration uses the standard C tagged structure

  • gSoap generated client-side structure initialization and use 2011-04-29

    gSoap generated client-side structure initialization and use (using ANSI C bindings) First of all, I searched and although there are a number of struct initialization solutions offered, I did not find anything directly answering this issue. Also, thi

  • C++ GNU designated structure initialization not recognized in Eclipse 2013-01-14

    The CDT parser reports a syntax error for the structure initialization: typedef struct MyStruct { int a; float b; }; int main( void ) { // GNU C extension format MyStruct s = {a : 1, b : 2}; // C99 standard format // MyStruct s = {.a = 1, .b = 2}; re

  • How to declare a structure in a header that is to be used by multiple files in c? 2008-10-23

    If I have a source.c file with a struct: struct a { int i; struct b { int j; } }; How can this struct be used in another file (i.e. func.c)? Should I create a new header file, declare the struct there and include that header in func.c? Or should I de

  • C#: Definition of the scope of variables declared in the initialization part of for-loops? 2011-08-05

    Possible Duplicates: confused with the scope in c# C# Variable Scoping I am curious about the design considerations behind the scope of variables which are declared in the initialization part of for-loops (etc). Such variables neither seem to be in-s

  • C structure initialization with variable 2011-10-09

    I've run into a problem that seems to not be addressed by any of the C Standards after C89 save for the mention that structures initialization limits had been lifted. However, I've run into an error using the Open Watcom IDE (for debugging) where the

  • Help with declaring C++ structure, with a float array as one of its members 2009-03-09

    I was wondering if anyone could spot what is wrong with my structure declaration and use. At the moment I have a structure and wish to store float array as one of it's members. My code: struct Player{ float x[12]; float y[12]; float red,green,blue; f

  • Is it preferred/okay to use structure-initialization ({-}) over memset, etc.? 2010-12-12

    Code: WINDOWPLACEMENT wplcmt = {sizeof(WINDOWPLACEMENT)}; Looks so much cleaner than: WINDOWPLACEMENT wplcmt; memset(&wplcmt, 0, sizeof(WINDOWPLACEMENT)); wplcmt.length = sizeof(WINDOWPLACEMENT); The assembly output of this thing is also pretty n

  • How to forward declare a structure used as a member variable 2011-08-04

    I have a struct called CardState defined in Application.h: #ifndef APPLICATION_H #define APPLICATION_H #include <Session.h> // Note that both files include each others struct CardState { bool property1; bool property2; }; class Application : pu

  • Structure initialization in class objects 2011-12-20

    How do I initialize the structure variables of type class objects? I have the following code: #include<iostream> using namespace std; class bitmap { public : bitmap() { clear() ;} get(); set(); clear(); static const int a=10; }; bitmap::get() {

  • Structure Initialization Unmatched variables 2012-04-03

    I am confused as to what this is doing: #define AIR_LP 1 tw_lptype airport_lps[] = { { AIR_LP, sizeof(Airport_State), (init_f) Airport_StartUp, (event_f) Airport_EventHandler, (revent_f) Airport_RC_EventHandler, (final_f) Airport_Statistics_CollectSt

  • Function Declarations and Structure declarations in C 2011-02-22

    int f(struct r); struct r { int a; int b; }; The above snippet in a source file throws an error that warning: its scope is only this definition or declaration, which is probably not what you want for the line int f(struct r) and the following snippet

  • Declare a Structure and a Function Reference that use Each Other 2011-03-05

    I need to declare a (typedef'd) structure and a (typedef'd) function reference in pain old C. This is my code: typedef void (*monitor_calback)(monitor_data*, short int, short int, void*); typedef struct { int port; unsigned char port_state; monitor_c

  • Static structure initialization with tags in C++ 2011-04-26

    I've searched stackoverflow for an answer but I cannot get something relevant. I'm trying to initialize a static structure instance with initial values by specifying their tags, but I get an error at compilation time: src/version.cpp:10: error: expec

  • unable to declare class without initialization? 2011-07-01

    For standard data objects such like int, the following can be done int number; number = 0; Basically, you can declare number before initializing it, useful if you initialize inside various if statements and you don't want number going out of scope. C

  • How do I fix multiple declaration and multiple initialization errors? 2011-08-12

    Sample 1> namespace Exercise { int ivar = 0; } int ivar = 0; using Exercise::ivar; // <<== error error C2874: using-declaration causes a multiple declaration of 'Exercise::ivar' Sample 2> namespace Exercise { double dvar = 0; } int main(i

  • Structure in structure, initialization 2011-11-07

    How to initialize an inner structure inside the outer structure? struct TOuter { struct TInner { bool b1, b2; TInner () : b1 (false), b2(false) {} }; bool b3; TOuter (): TOuter::TInner(), b3(true) {} // Error }; --------------Solutions------------- I

  • Distinguish instance and static inner classes declared in the initializer 2012-01-19

    While trying to answer a question here on SO, I came across an theoretic problem where I'm not sure whether a better solution exists: Suppose we have the following setup: class A { public A(Outer o) { } } class Outer { static A staticA = new A( new O

  • C++11 Proper Structure Initialization 2012-03-04

    Having a structure like this in C++11: struct von { std::string Name; unsigned int ID; std::vector<std::string> Checks; }; Should it be initialized like this: von v = {"",0,{}}; Or like this: von v = {}; Both ways seem to work, but the compiler

Copyright (C), All Rights Reserved.

processed in 0.135 (s). 11 q(s)