-3
#include <iostream>

using namespace std;

#define SELECT 0

class Z
{
    private:
        int *z1; int *z2;
    public:
        Z(const int x1 = 0, const int x2 = 0);
        Z(const Z &X);
        int *first (void) const {return z1;};
        int *second (void) const {return z2;};
        ~Z(void);
};

Z::Z(const int x1,const int x2){
    z1 = new int(x1);
    z2 = new int(x2);
}

#if SELECT == 1
Z::Z(const Z &X){
    z1 = new int(*X.first() );
    z2 = new int(*X.second() );
}
#else
Z::Z(const Z &X){
    z1 = X.first();
    z2 = X.second();
}
#endif

Z::~Z(){
    delete z1;
    delete z2;
}

int main()
{
    Z firstZ;
    Z secondZ(4,7);
    Z thirdZ(secondZ);

    Z *zp;
    zp = new Z(3,5);
    Z a(6, *(zp->first() ) ), b=a, c(0,0);
    c = *zp;
    cout << "Content of c: " << *c.first() << " and " << *c.second() << endl;
    delete zp;
    cout << "Content of c: " << *c.first() << " and " << *c.second() << endl;

    return 0;
}

Hello I have this code, when I run it I get

Content of c: 3 and 5
Content of c: 14900448 and 5

however I was expecting something like

Content of c: 3 and 5
Content of c: Garbage and Garbage

However somehow c.second still points the value 5. Can you explain how this occurs?

I also wonder following: When I commented(or deleted) these lines

    Z firstZ;
    Z secondZ(4,7);
    Z thirdZ(secondZ);

and run again I get what I expected

Content of c: 3 and 5
Content of c: 1924224 and 1907344

Can you explain how this happens?

Thanks

  • 3
    how can you tell the difference between garbage and `5` ? – 463035818_is_not_an_ai Nov 17 '20 at 14:15
  • is it possible that all this code is just to dereference a non-initialized pointer? Please reduce it to a [mcve] – 463035818_is_not_an_ai Nov 17 '20 at 14:18
  • I did not understand what you mean @idclev463035818 – BlackSmith0040 Nov 17 '20 at 14:18
  • When your code has undefined behavior then you are not guaranteed to get any meaningful output. You are **not** guaranteed to get meaningless output. – 463035818_is_not_an_ai Nov 17 '20 at 14:18
  • You're still not following the rule of three, and this time it leads to undefined behaviour. Read the answer to [your previous question](https://stackoverflow.com/questions/64874330/c-dynamic-memory-destroying-memory-pointed-by-pointer) again. – molbdnilo Nov 17 '20 at 14:19
  • 1
    i mean: I suppose you are not expecting the string "Garbage" to show up on your screen, but rather some "garbage value". `5` is a garbabe value just like any other – 463035818_is_not_an_ai Nov 17 '20 at 14:19
  • @molbdnilo I understood that but I am asking totally different question. – BlackSmith0040 Nov 17 '20 at 14:20
  • @idclev463035818 That 5 is not a garbage because it occurs each time I run – BlackSmith0040 Nov 17 '20 at 14:21
  • @BlackSmith0040 The problem is still the same. – molbdnilo Nov 17 '20 at 14:21
  • that doesnt really answer it. I claim: "Every second time you run your code you get garbage and that garbage is 5, every other time you get a correct value and that value is 5". You cannot possibly tell the difference – 463035818_is_not_an_ai Nov 17 '20 at 14:22
  • I don't think so. Here I am asking how does these lines change the output? ``` Z firstZ; Z secondZ(4,7); Z thirdZ(secondZ); ``` – BlackSmith0040 Nov 17 '20 at 14:22
  • @idclev463035818 Then let me ask you how that garbage 5 , occurs as 5 at each run – BlackSmith0040 Nov 17 '20 at 14:24
  • I am just trying to convince you that expecting some garbage output, wont help you much, its a misled expectation. Either your code has no UB then you get correct output or it has UB then any output can appear, and any does include the correct output – 463035818_is_not_an_ai Nov 17 '20 at 14:25
  • Do you think that in the garbage case the compile will insert a random number generator to fill in all places in memory with _proper_ garbage, or that you'll just get whatever junk was left over at that space in the stack, and with a determinate machine that'll likely be somewhat stable? – Mike Vine Nov 17 '20 at 14:27
  • This has been explained to you over and over in the comments. The only disrespect anyone's showing here is you, @BlackSmith0040. – Tanveer Badar Nov 17 '20 at 14:32
  • 2
    Read the hotel analogy from this answer https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope/6445794#6445794 maybe you will understand why your question is meaningless. – Slava Nov 17 '20 at 14:44
  • @BlackSmith0040 -- The bottom line is that if you are willing to spend (or more realistically, waste) time on trying to figure out undefined behavior, go ahead and do so -- no one is stopping you. The problem is trying to get groups of persons to follow along and spend time on the hunt. C++ programmers are trained to *not* have code that produces undefined behavior, or recognize code that produces undefined behavior. That's where your focus should be, to be honest with you -- recognition and prevention. – PaulMcKenzie Nov 17 '20 at 14:52

1 Answers1

2

However somehow c.second still points the value 5. Can you explain how this occurs? ... When I commented(or deleted) these lines ... I get what I expected ... Can you explain how this happens?

This is something that you might see happening whenever your program has undefined behavior.

Enabling or disabling optimizations, changing unrelated lines (like you do in this case), changing compiler version, Etc, can all cause your program to output "garbage", or something that does not look like garbase, or crash.

Unless you step through the generated code, there is no way to tell what the result will be.

Acorn
  • 24,970
  • 5
  • 40
  • 69
  • Finally a proper answer thanks for that first. I tried to debug but I couldn't find. That is why I asked actually. – BlackSmith0040 Nov 17 '20 at 14:32
  • You're welcome! It is hard to debug undefined behavior when you are new, in particular if you don't know assembly. In Linux, try to learn GDB and related tools. On Windows, Visual Studio. – Acorn Nov 17 '20 at 15:06