0

I have following codes, not sure how unique_ptr is initialized.

a.h

#ifndef EXPERIMENTAL_USERS_ALOUA_CPP_PROTECTED_CTOR_INTIALIZE_A_H_
#define EXPERIMENTAL_USERS_ALOUA_CPP_PROTECTED_CTOR_INTIALIZE_A_H_

#include <iostream>
#include <memory>

class A {
 public:
  void help() { std::cout << "help ! \n "; }

 protected:
 A() = default;
};

#endif  // EXPERIMENTAL_USERS_ALOUA_CPP_PROTECTED_CTOR_INTIALIZE_A_H_

main.cc

#include <iostream>
#include <memory>
#include "experimental/users/aloua/cpp/protected_ctor_intialize/a.h"
struct B {
  std::unique_ptr<A> a;
  // the default constructor
  // A aa;
};

int main() {
  B b;
  b.a->help();   // this outputs "help !"
}

how can the program compile and run without any issue ? is it because of std::unique_ptr<A> a is zero initialized according to value initialization and no actual constructor is actually called ?

===========

Tested that the pointer is actually nullptr.

sthbuilder
  • 561
  • 1
  • 7
  • 22
  • 3
    `std::unique_ptr` has a default constructor, which initializes it as a null pointer: https://en.cppreference.com/w/cpp/memory/unique_ptr/unique_ptr – UnholySheep Dec 13 '18 at 18:44
  • 1
    if it is a null pointer, how can we do `b.a->help()` function call then ? @UnholySheep – sthbuilder Dec 13 '18 at 18:55
  • 2
    Welcome to the wonderful and wacky world of Undefined Behaviour. A test to ensure the program is not accessing a null pointer is a unnecessary performance penalty for all programs that do not attempt to access a null pointer, so **no check is made**. It's on you, the programmer, to provide a valid program. – user4581301 Dec 13 '18 at 19:00
  • @javarookie _"if it is a null pointer, how can we do b.a->help() function call then ?"_ You initialize that to a proper instance of `A` before calling? Check `std::make_unique` please. Did you fully understand how class instances are created in c++ at all? – πάντα ῥεῖ Dec 13 '18 at 19:01
  • @πάνταῥεῖ, I know how to intialize a unique_ptr, what I don't understand is in my main.cc as shown above, even though I don't intialize or intialized as null as per @UnholySheep, I can still make function call `b.a->help()`. – sthbuilder Dec 13 '18 at 19:04
  • As for how can this possibly appear to work, if the method does not use any non-`static` member variables, it's possible that the program doesn't even notice that `this` is invalid. – user4581301 Dec 13 '18 at 19:04
  • 2
    @javarookie Well, that's simply called _undefined behavior_, c++ doesn't prevent you from [shooting your leg](http://www.stroustrup.com/bs_faq.html#really-say-that). – πάντα ῥεῖ Dec 13 '18 at 19:05
  • 1
    Javarookie, you are confusing syntax and logic. The compiler ensures the program's syntax is correct, the source code can can be translated into executable code, and MAY warn you about any logical errors it spots. `b.a->help();` is syntactically correct and thus compiles. That the logic is wrong is not the compiler's concern. – user4581301 Dec 13 '18 at 19:08
  • @user4581301, it makes sense this can complie given it does not have any syntax error, but the fact it can run and print the expected output is a surprise to me. – sthbuilder Dec 13 '18 at 19:10
  • 1
    I tried to address that a few comments up. If `help` is something simple like `void help() {cout << " How to use this program ...";}` the invalid `this` parameter is not used can is unlikely to cause misbehaviour. That said, an implementation COULD (because the behaviour is not defined by the C++ Standard) test for null `this` and report an error, but it could also generate code that attempts to launch a nuclear missile at Madagascar. Undefined is undefined. Sometimes it results in something that looks like it works. It is insidious as hell and to be feared. – user4581301 Dec 13 '18 at 19:16
  • [Here is one of my personal favorites](https://www.youtube.com/watch?v=73wMnU7xbwE). How may times do you think this presentation worked before Bill Gates got up on stage for that demo? – user4581301 Dec 13 '18 at 19:19
  • @user4581301, just tested it is actully null, thanks for detailed explanation. – sthbuilder Dec 13 '18 at 19:24

0 Answers0