-1

I am trying to set the value of an unassigned memory location in order to get an exception, so what I have been doing is taking an integer (with a valid memory location) getting its address, adding 4 to it (to go the the next integer hopefully), dereferencing it and assigning it a value (1):

int i = 20;
(*((&i) + 4)) = 1;

This line is throwing an error saying that

CL.exe exited with code 2.

Is this a compiler bug?

Note: I am using the Microsoft C++ compiler on VS 2019.

EDIT: here is a screen shot of the dark side of the code (added in order to make it clear that this is the only error that I get):

enter image description here

I just wanted to know if this was a compiler bug since it was not showing up an appropriate error message.

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
  • Could you put entire function for review? It may be caused by somewhere else like a missing return value: https://developercommunity.visualstudio.com/solutions/579027/view.html – Ali Tavakol Jun 09 '19 at 11:59
  • 2
    Could you provide a [mcve], please? – Jesper Juhl Jun 09 '19 at 11:59
  • 3
    Very probably you got some more of an error message than that - why don't you post that? – tofro Jun 09 '19 at 11:59
  • 3
    If `i` is an integer then `((&i) + 4))` is not the next integer but the fourth next integer. We need a [mcve] to help you. – Richard Critten Jun 09 '19 at 12:07
  • I'm guessing `Undefined Behaviour`. Can you show the declaration of `i`? Your code is probably generating a segfault rather than the hoped for exception (in the C++ sense). – G.M. Jun 09 '19 at 12:09
  • 3
    VS2019 has an ugly bug that throws off too many programmers. Just focus on the *other* error messages it spits out and fix those, then the "exited with code 2" message disappears as well. – Hans Passant Jun 09 '19 at 12:12
  • Possible duplicate of [How dangerous is it to access an array out of bounds?](https://stackoverflow.com/questions/15646973/how-dangerous-is-it-to-access-an-array-out-of-bounds) – G.M. Jun 09 '19 at 12:55
  • I can't repro the issue from the posted screenshot. This problem is worse than I thought. Use Help > About, the version I use is 16.0.3 (not updated in a while). The error I see is "error C4789: buffer 'i' of size 4 bytes will be overrun; 4 bytes will be written starting at offset 16". Which reminds you that this code stomps all over the stack frame, arbitrarily overwriting variables. Very naughty. Use Help > Send Feedback > Report a Problem. – Hans Passant Jun 09 '19 at 12:58
  • @HansPassant thank you, I will do so! – meJustAndrew Jun 09 '19 at 13:00

1 Answers1

2

Apparently it was indeed a compiler bug. The fix for it was implemented and the release is pending.

Thanks to Hans Passant's comments I have opened a bug and it will be handled in a future update of Visual Studio 2019.

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76