![]() |
Default initialization of objects??
I am new to C++ and was trying to find if objects in C++ are
intialized by default. I wrote a sample program as follows: #include <iostream> int main() { class A { int a, b, c; public: int getVal() { return c; } }; A obj; std::cout << obj.getVal() << std::endl; return (0); } This program prints a value of 0 when compiled with g++ and run on a linux system. However when run in Visual Studio's C++, it crashes (but it does not crash if I initialize obj before trying to read a member's value. I tried googling about the behaviour but could not find anything definitive. I would like to know if the use of an uninitialized object like this results in an undefined behaviour and if there is any initialization (maybe, to zeroes) provided by default for the objects. From the results I have, it does seem to be the former case, but would like to double check as to what the language standard says. |
Re: Default initialization of objects??
On Nov 22, 1:03*pm, CodeTrooper <anujagrawa...@gmail.com> wrote:
> I am new to C++ and was trying to find if objects in C++ are > intialized by default. In C++, you have primitive types (bool, numbers, pointers, character types, enumerations) who are not initialized at all when declared. All sorts of aggregation consisting solely of such types (arrays, structures, unions) are not initialized either. You can initialize them to their default values (e.g. 0 for numbers) by doing e.g. int i = int(); When you create your own type (a class), you can add it a constructor and initialize what you want in it. E.g. class MyType { int a; public MyType() : a() {} // "a" is default-initialized }; If you leave out a() (and if you don't assign it in constructor body), then, since a is a primitive type, it's not going to be initialized at all. > I wrote a sample program as follows: > > #include <iostream> > > int main() > { > * * class A > * * { > * * * * int a, b, c; > * * * * public: > * * * * int getVal() { return c; } > * * }; > > * * A obj; > * * std::cout << obj.getVal() << std::endl; > * * return (0); > > } > > This program prints a value of 0 when compiled with g++ and run on a > linux system. However when run in Visual Studio's C++, it crashes (but > it does not crash if I initialize obj before trying to read a member's > value. I am surprised that it crashes (I would expect it to print garbage), but, you tried to read uninitialized variable, which, I believe, is undefined behavior. > I tried googling about the behaviour but could not find anything > definitive. I would like to know if the use of an uninitialized object > like this results in an undefined behaviour and if there is any > initialization (maybe, to zeroes) provided by default for the objects. > From the results I have, it does seem to be the former case, but would > like to double check as to what the language standard says. There's no default 0-init in C++. Goran. |
Re: Default initialization of objects??
Hi CodeTrooper
CodeTrooper wrote: > I am new to C++ and was trying to find if objects in C++ are > intialized by default. No they are not initialized You have to use an initializer list and You should move the class out of main. Most of the time a class gets its own unit a cpp and a h file, something like this: in filename.h file: #ifndef FilenameH #define FilenameH class A { int a, b, c; public: int getVal() { return c; } A(); }; #endif in finename.cpp file #include "finename.h" A::A() : a(0) , b(0) , c(0) { } include <iostream> #include "finename.h" int main() { A obj; std::cout << obj.getVal() << std::endl; return (0); } > > This program prints a value of 0 when compiled with g++ and run on a > linux system. However when run in Visual Studio's C++, it crashes (but > it does not crash if I initialize obj before trying to read a member's > value. > > I tried googling about the behaviour but could not find anything > definitive. I would like to know if the use of an uninitialized object > like this results in an undefined behaviour Yes as much as the initial value of the objects internal variables are important to the objects behaviour. In other words the fact that You are not initializing a, b and c have nothing to do with Your program crashing. You should just see a undefined number. I don't use Visual C++ so I cant test. Best regards Asger-P |
Re: Default initialization of objects??
Goran <goran.pusic@gmail.com> wrote:
> I am surprised that it crashes (I would expect it to print garbage), > but, you tried to read uninitialized variable, which, I believe, is > undefined behavior. In debug mode, VS includes some runtime checks. It fills uninitialized data with some specific bit pattern (like 0xabababab, 0xcdcdcdcd, or 0xefefefef, I don't recall which one. The others are for similar cases like already freed data on the heap) and throws an exception when reading them. By examining the exception that leads to the crash, you can find out the reason quickly. Tobi |
| All times are GMT. The time now is 08:57 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.