Is this defined behaviour in D?

Steven Schveighoffer schveiguy at yahoo.com
Mon Sep 25 13:25:02 UTC 2017


On 9/25/17 9:06 AM, John Burton wrote:
> If I have a int* pointer for example, that points to the start of an int 
> array and step through the array until I get the value 123, is it 
> defined in D what happens if you step off the end of the array?
> 
> What I might expect to happen is for the code to just keep stepping 
> through sequential memory locations until it finds one that happens to 
> have a 123 in it, until it gets a memory protection fault of some kind, 
> or failing that wrap around memory and go into an infinite loop.
> 
> The options are really
> 1) I can rely on the compiler consistently doing what I'd "expect" on a 
> specific platform.
> 2) There is some other defined behaviour
> 3) The compiler can do whatever crazy thing it feels like as this is 
> considered broken code.
> 
> I'm not for a moment suggesting this is a good idea, but what I'm really 
> asking is does the D language say anything about this? C++ seems to have 
> chosen (3). Personally I'd prefer (1) but it's most to know if the 
> "standard" such as it is has anything to say on such matters.

I would tend to guess 3, but I don't know if the compiler has the 
capability to reason about it, so the current behavior might be what you 
expect. I wouldn't count on it though...

-Steve


More information about the Digitalmars-d-learn mailing list