1

I was trying something out, and I made this code: http://cpp.sh/4x435 (Shown here below too)

#include <iostream>

using namespace std;

class Thing{
    public:
        int height;
        Thing(int h): height(h) {};
        Thing(): height(10) {};
        ~Thing(){};

        int display(){ return this->height; }
};

Thing* get(){
    Thing* x = new Thing(33);
}

int main(){
    Thing* a = new Thing();
    std::cout<<"1: "<<a->display()<<std::endl;
    a = get();
    std::cout<<"2: "<<a->display()<<std::endl;
    return 0;
}

I forgot to add a return in the "get"-function before I compiled and ran it, surprisingly it played out correctly anyway(i.e. "1: 10, 2: 33" was the output).

Now I saw that in the shell online it only displays "1: 10", whereas when I try having it return an int or string by value, like so:

int get(){ int a = 30; }

int main(){
    int b = get();
    std::cout<<"1: "<<b<<std::endl;
    return 0;
}

it doesn't function correctly(outputs "1: 1"), this is expected.

What's happening there? Shouldn't "Things* get()" malfunction, cause an error or at least make it spit out some gibberish onto the screen or something?

I'm using Code::Blocks 16.01, C++11, GNU GCC Compiler, no extra flags set(so only any already set by Code::Blocks)

EDIT:

It's not exactly the same as the suggested duplicate, because the example of int get() { int a = 30; } returns 1, had it returned 30, then it would have been the same problem.

However, I tried this:

int* get(){
    int* x = new int(4);
}
int main(){
    int* a;
    a = get();
    std::cout<<"2: "<<*a<<std::endl;
    return 0;
}

And here I get the same problem that I found when trying the first version of the code where get() was of the return type Thing*. So it seems like it has to do with pointers.

Jack Of Blades
  • 495
  • 7
  • 16
  • why get() function doesnt return anything ? – Abr001am May 08 '18 at 20:55
  • 2
    @Abra001 The OP has already addressed it in the question: _"I forgot to add a return in the "get"-function before I compiled and ran it"._ The question being asked is why this error (forgotten i.e. omitted return) was not caught by the compiler or runtime – ShreevatsaR May 08 '18 at 21:20
  • 2
    Possible duplicate of [Why does flowing off the end of a non-void function without returning a value not produce a compiler error?](https://stackoverflow.com/questions/1610030/why-does-flowing-off-the-end-of-a-non-void-function-without-returning-a-value-no) – Mgetz May 08 '18 at 21:26
  • @Mgetz Not quite. I explained that case in "it doesn't function correctly(outputs "1: 1"), this is expected.", I see that as a logical consequence of the absence of vital keywords, i.e. it showed me something in the output so I could see that it did not do what I wanted it to. But had "int get()" returned 30, then it would have been the same problem. The "Thing"-object returned the correct result regardless, that's what I'm surprised about. – Jack Of Blades May 08 '18 at 21:36
  • @JackOfBlades It returned something that just happened to be the thing you considered correct but could just as easily have been something that you considered incorrect. The output is totally unsurprising but for the fact that you happened to consider it correct. If you stop considering it correct, the mystery goes away. The implementation did something you wouldn't expect given the code you gave it -- which is just what you should expect. – David Schwartz May 08 '18 at 22:06
  • this is UB since the compiler tries to address a nonvalid pointer. thanks @ShreevatsaR i missed this part. – Abr001am May 08 '18 at 23:11

1 Answers1

0

Use the -Wall option when compiling to have an appropriate warning, like:

gcc program.c -o program -Wall

Otherwise this question and its accepted answer explains why it happens, even providing an example similar to your observation:

Why does flowing off the end of a non-void function without returning a value not produce a compiler error?

If you are curious, check the assembler output! You can generate it by:

gcc program.c -S

Since the behavior is undefined, anything may happen, what you are experiencing is most likely caused by that within the function, the compiler left the right value in the register normally used for returning.

Jubatian
  • 2,171
  • 16
  • 22