Relative Content

Tag Archive for c++undefined-behavior

C++ cout statement causing variable change

I’m new to C++ coming from C# so I’m not too confident on memory stuff, I think I’m getting some undefined behaviour issues when trying to use my class (feel free to correct me if I’m wrong) and I don’t know why. If I put std::cout statements at the end of the Initialise() function it changes the values. My suspicion is the Piece class deleting things in the Initialise() function, but I have no idea how to stop that.
(Cube class is just two std::arrays in a struct) I had a look at the other question asked about undefined behaviour but couldn’t infer what my issue was. Let me know if more information is needed.
Thanks for reading.

Is the calculation of pointer offsets between non-array composition members defined?

I understand the reasoning behind expr.add#4.2, restricting + and - to pointers targeting elements of the same array, which was explained in several comments to my question regarding an offset pointer class. However, the very same could be accomplished by both pointer and pointer plus offset targets being within the same (nested) composition, not necessarily an array. If these differences could be anything the offsetof macro would be pointless.