Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default constructor for C++ pointer?

I have code like this:

class MapIndex { private:     typedef std::map<std::string, MapIndex*> Container;     Container mapM;  public:     void add(std::list<std::string>& values)     {         if (values.empty()) // sanity check             return;          std::string s(*(values.begin()));         values.erase(values.begin());         if (values.empty())             return;          MapIndex *&mi = mapM[s];  // <- question about this line         if (!mi)             mi = new MapIndex();         mi->add(values);     } } 

The main concern I have is whether the mapM[s] expression would return reference to NULL pointer if new item is added to the map?

The SGI docs say this: data_type& operator[](const key_type& k) Returns a reference to the object that is associated with a particular key. If the map does not already contain such an object, operator[] inserts the default object data_type().

So, my question is whether the insertion of default object data_type() will create a NULL pointer, or it could create an invalid pointer pointing somewhere in the memory?

like image 471
Milan Babuškov Avatar asked Jun 01 '09 21:06

Milan Babuškov


People also ask

What is default constructor?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .

Is pointer null by default?

N@G3 " [...] a variable or pointer is given a null "value" by default [...] " Pointers have no specific default value.

What is constructor in C?

A constructor is a special type of member function that is called automatically when an object is created. In C++, a constructor has the same name as that of the class and it does not have a return type. For example, class Wall { public: // create a constructor Wall() { // code } };

What is a default constructor C#?

A constructor with no parameters is called a default constructor. A default constructor has every instance of the class to be initialized to the same values. The default constructor initializes all numeric fields to zero and all string and object fields to null inside a class. Example : C#


2 Answers

It'll create a NULL (0) pointer, which is an invalid pointer anyway :)

like image 157
mmx Avatar answered Sep 23 '22 01:09

mmx


Yes it should be a zero (NULL) pointer as stl containers will default initialise objects when they aren't explicitly stored (ie accessing a non-existant key in a map as you are doing or resizing a vector to a larger size).

C++ Standard, 8.5 paragraph 5 states:

To default-initialize an object of type T means:

  • If T is a non-POD class type (clause class), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor)
  • If T is an array type, each element is default-initialized
  • Otherwise, the storage for the object iszero-initialized.

You should also note that default initialisation is different to simply ommiting the constructor. When you omit the constructor and simply declare a simple type you will get an indeterminate value.

int a; // not default constructed, will have random data  int b = int(); // will be initialised to zero 
like image 30
Jamie Cook Avatar answered Sep 22 '22 01:09

Jamie Cook