0

Below, I have provided three different scenarios in which the codes spit out "segmentation fault" and I am not sure why! Scenario 1 only performs the first line (This is i: 42) and then it crashes with the segmentation fault message. In scenario 2, where I just declare the pointers (but do not initialise them), the code works up to std::cout << "This is *pi3: " << *pi3 << std::endl; (it does not perform this line and the rest). Finally, in scenario 3, once again it only performs the first line (i.e. "This is i: 42") even though I only added int j = 5;!

Scenario 1:

#include <iostream>
#include <cstdlib>

int main(){
    
    int i    = 42;
    int *pi  = nullptr;
    int *pi2 = &i     ;
    int *pi3 = nullptr;

    std::cout << "This is i: "    << i    << std::endl;
    std::cout << "This is *pi: "  << *pi  << std::endl;
    std::cout << "This is pi: "   <<  pi  << std::endl;
    std::cout << "This is *pi2: " << *pi2 << std::endl;
    std::cout << "This is pi2: "  << pi2  << std::endl;
    std::cout << "This is *pi3: " << *pi3 << std::endl;
    std::cout << "This is pi3: "  << pi3  << std::endl;
    pi3 = pi2;
    std::cout << "This is *pi3: " << *pi3 << std::endl;
    std::cout << "This is pi3: "  << pi3  << std::endl;
    return 0;
}

Scenario 2:

#include <iostream>
#include <cstdlib>

int main(){
    
    int i    = 42;
    int *pi      ;
    int *pi2 = &i;
    int *pi3     ;

    std::cout << "This is i: "    << i    << std::endl;
    std::cout << "This is *pi: "  << *pi  << std::endl;
    std::cout << "This is pi: "   <<  pi  << std::endl;
    std::cout << "This is *pi2: " << *pi2 << std::endl;
    std::cout << "This is pi2: "  << pi2  << std::endl;
    std::cout << "This is *pi3: " << *pi3 << std::endl;
    std::cout << "This is pi3: "  << pi3  << std::endl;
    pi3 = pi2;
    std::cout << "This is *pi3: " << *pi3 << std::endl;
    std::cout << "This is pi3: "  << pi3  << std::endl;
    return 0;
}

And Scenario 3 (where I just add int j = 5;):

#include <iostream>
#include <cstdlib>

int main(){
    
    int i    = 42;
    int j    = 5 ;
    int *pi      ;
    int *pi2 = &i;
    int *pi3     ;

    std::cout << "This is i: "    << i    << std::endl;
    std::cout << "This is *pi: "  << *pi  << std::endl;
    std::cout << "This is pi: "   <<  pi  << std::endl;
    std::cout << "This is *pi2: " << *pi2 << std::endl;
    std::cout << "This is pi2: "  << pi2  << std::endl;
    std::cout << "This is *pi3: " << *pi3 << std::endl;
    std::cout << "This is pi3: "  << pi3  << std::endl;
    pi3 = pi2;
    std::cout << "This is *pi3: " << *pi3 << std::endl;
    std::cout << "This is pi3: "  << pi3  << std::endl;
    return 0;
}

I understand that initialising pointers (and to a greater extend all variables) is good practice. Then, why does it not work here? And why does it only work half way through the code when I do not initialise *pi and *pi3 in scenario 2? I would be thankful if you could explain to me what is happening in each scenario and the source of error(s). I use both g++ and clang++ on a 2019 MacBook Pro!

Thank you.

15
  • 3
    You can't dereference a nullptr. Commented Jul 7, 2020 at 18:57
  • 2
    Can you share what you expect *nullptr to result in? Commented Jul 7, 2020 at 18:58
  • 2
    It's not the initialisation that is the problem, it's the dereferencing of a null pointer. I.e. in your first example std::cout << *pi is the problem, until that point there's been no errors. Commented Jul 7, 2020 at 19:01
  • 3
    @zhuser The problem is that it is not possible to learn C++ by trial-and-error. This is because not all compilable code necessarily has defined behavior. C++ has Undefined Behavior meaning it is possible to write invalid code that still compiles, that may appear to work in some way, but that behavior could change at any time, for no apparent reason. When trying to learn by trial-and-error it isn't possible to know if what you observe is actually well defined behavior or the result of Undefined Behavior. You need to be certain that the code you write is allowed before you can draw conclusions. Commented Jul 7, 2020 at 19:13
  • 2
    @zhuser That is the annoying thing about Undefined Behavior, it doesn't need to produce an easy to diagnose behavior like a crash. It can just (by coincidence) do something that seems reasonable when you try it making it harder to detect the actual cause of the problem. Commented Jul 7, 2020 at 19:17

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.