Why do private member variables behaved like protected in the same module when creating deriving class?
Steven Schveighoffer
schveiguy at gmail.com
Thu Nov 1 20:27:16 UTC 2018
On 11/1/18 4:15 PM, Paul Backus wrote:
> On Thursday, 1 November 2018 at 19:26:05 UTC, Steven Schveighoffer wrote:
>>
>> My testing looks like it's the same:
>>
>> #include <stdio.h>
>> int main()
>> {
>> int arr[5];
>> int *ptr = arr + 1; // point at second element so we can do
>> negative indexing legally
>> int idx1 = 0;
>> ptr[--idx1] = 5;
>> printf("%d\n", arr[0]); // 5
>> size_t idx2 = 0;
>> ptr[--idx2] = 6;
>> printf("%d\n", arr[0]); // 6
>> return 0;
>> }
>>
>
> `ptr[--idx2]` is undefined behavior (out-of-bounds array access), so the
> fact that it works that way is just a coincidence.
Either way, I don't see it causing a segfault -- the purported benefit.
But maybe on Patrick's actual arch it did. In any case, undefined
behavior is bad whether you do it with an int or size_t :)
-Steve
More information about the Digitalmars-d
mailing list