0

I encountered a problem while initializing a pointer data member i.e int* apex; inside a constructor having parameter as int i = 0; as *apex = i; but unfortunately nothing is executed after compiler strikes this line.

#include <iostream>
using namespace std;

class base{
    int *apex;      
public:
    explicit base(int i = 0){
        cout << "this does executes" << endl;
        *apex = i; // <<<<<--- problem???
        cout << "this doesnt executes" << endl;
    }
};

int main(void){
    base test_object(7);
    cout << "this also doesnt executes";
}


// I know how to avoid this but i want to know what
// exactly the problem is associated with *apex = i;

THANKS IN ADVANCE note-no error is generated

  • 2
    You never intialize the pointer to point at valid memory. so `*apex` invokes *undefined behavior* – UnholySheep Sep 02 '20 at 12:52
  • 2
    This has nothing to do with constructors or classes. It has everything to do with dereferencing an uninitialized pointer. – PaulMcKenzie Sep 02 '20 at 12:53
  • Does this answer your question? [Where exactly does C++ standard say dereferencing an uninitialized pointer is undefined behavior?](https://stackoverflow.com/questions/4285895/where-exactly-does-c-standard-say-dereferencing-an-uninitialized-pointer-is-un) – Superlokkus Sep 02 '20 at 13:08
  • 1
    You can only initialize member variables in the constructor's initializer list. What you're doing is assignment, and you're not assigning to the pointer but to some non-existent thing. – molbdnilo Sep 02 '20 at 13:16

3 Answers3

3

What you wrote is equivalent to:

int *apex;
*apex = 42;

which is undefined behavior (UB), which includes that the compiler might just include code to stop execution or to start playing the song Never Gonna Give You Up by Rick Astley.

Even

int *apex = nullptr;
*apex = 42;

would be UB because the int* pointer has to point to a valid int when dereferencing via *

Just write

class base{
    int apex{};      
public:
    explicit base(int i) : apex(i){}
};

And be done for

Superlokkus
  • 4,731
  • 1
  • 25
  • 57
  • Yes, or `explicit base(int i = 0) : apex{ new int(i) } {}` if `apex` really needs to be a pointer for some reason. – Frodyne Sep 02 '20 at 13:19
  • Nope in general than this should be in a `std::unique_ptr` and using `std::make_unique(i)` then, otherwise it very much depends on the reason why this has to be a pointer. – Superlokkus Sep 02 '20 at 13:24
0

I got it. Trust me I am ashamed of myself after this silly doubt.

#include <iostream>
using namespace std;

class base{
    int *apex;      
public:
    explicit base(int i = 0){
        apex = new int;
        // this is what i was supposed to do
        *apex = i;
    }
};

int main(void){
    base test_object(7);
}
-1

Your pointer points to invalid address you didn't initialize it This will fix what you have asked to be done.

using namespace std;

class base{
    int *apex{nullptr};      
public:
    explicit base(int& i ): apex{&i} {
        cout << "this does executes" << endl;
        cout << "this doesnt executes" << endl;
    }
};

int main(void){
    int a = 7
    base test_object(a);
    cout << "this also doesnt executes";
}

Make sure something (int) given to ctor has longer lifetime than an instance.

Owl66
  • 147
  • 12