What is the default constructor for C++ pointer?

31,637

Solution 1

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

Solution 2

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

Solution 3

UPDATE: I completed my program and that very line I was asking about is causing it to crash sometimes, but at a later stage. The problem is that I'm creating a new object without changing the pointer stored in std::map. What is really needed is either reference or pointer to that pointer.

MapIndex *mi = mapM[s];  // <- question about this line
if (!mi)
    mi = new MapIndex();
mi->add(values);

should be changed to:

MapIndex* &mi = mapM[s];  // <- question about this line
if (!mi)
    mi = new MapIndex();
mi->add(values);

I'm surprised nobody noticed this.

Solution 4

The expression data_type() value-initializes an object. For a class type with a default constructor, it is invoked; if it doesn’t exist (or is defaulted), such as pointers, the object is zero-initialized.

So yes, you can rely on your map creating a NULL pointer.

Share:
31,637
Milan Babuškov
Author by

Milan Babuškov

Software developer, owner of a small ISV company, project manager of the open source FlameRobin project. Specialized in Linux, C++, PHP and Relational databases. You can read my software related blog at http://www.BackwardCompatible.net You can also buy my shareware software at http://www.GuacoSoft.com

Updated on December 12, 2020

Comments

  • Milan Babuškov
    Milan Babuškov over 3 years

    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?