Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does {0} mean when initializing an object?

Tags:

c++

c

When {0} is used to initialize an object, what does it mean? I can't find any references to {0} anywhere, and because of the curly braces Google searches are not helpful.

Example code:

SHELLEXECUTEINFO sexi = {0}; // what does this do? sexi.cbSize = sizeof(SHELLEXECUTEINFO); sexi.hwnd = NULL; sexi.fMask = SEE_MASK_NOCLOSEPROCESS; sexi.lpFile = lpFile.c_str(); sexi.lpParameters = args; sexi.nShow = nShow;  if(ShellExecuteEx(&sexi)) {     DWORD wait = WaitForSingleObject(sexi.hProcess, INFINITE);     if(wait == WAIT_OBJECT_0)         GetExitCodeProcess(sexi.hProcess, &returnCode); } 

Without it, the above code will crash on runtime.

like image 432
Mahmoud Al-Qudsi Avatar asked Sep 18 '08 00:09

Mahmoud Al-Qudsi


People also ask

Why do we initialize variables to 0?

In C programming language, the variables should be declared before a value is assigned to it. In an array, if fewer elements are used than the specified size of the array, then the remaining elements will be set by default to 0. Let us see another example to illustrate this.

Is initialized to zero when the first object is created?

A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.

Does New initialize memory 0?

Yes. That's kind of my point. If you make a new variable and see that's it's zero, you can't straight away assume that something within your program has set it to zero. Since most memory comes ready-zeroed, it's probably still uninitialised.

Which variables are initialized to zero?

Zero is initialized for every named variable with static or thread-local storage duration that is not subject to constant initialization (since C++14), before any other initialization.


1 Answers

What's happening here is called aggregate initialization. Here is the (abbreviated) definition of an aggregate from section 8.5.1 of the ISO spec:

An aggregate is an array or a class with no user-declared constructors, no private or protected non-static data members, no base classes, and no virtual functions.

Now, using {0} to initialize an aggregate like this is basically a trick to 0 the entire thing. This is because when using aggregate initialization you don't have to specify all the members and the spec requires that all unspecified members be default initialized, which means set to 0 for simple types.

Here is the relevant quote from the spec:

If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be default-initialized. Example:

struct S { int a; char* b; int c; }; S ss = { 1, "asdf" }; 

initializes ss.a with 1, ss.b with "asdf", and ss.c with the value of an expression of the form int(), that is, 0.

You can find the complete spec on this topic here

like image 128
Don Neufeld Avatar answered Oct 05 '22 22:10

Don Neufeld