www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - A bug?

reply berni <berni example.com> writes:
I'm not sure if this is considered a bug:

import std.stdio;
import std.string;

int c = 0;

void main()
{

    try {
        write(++c," ");
        stdout.flush();
        int[100000] tmp;
        throw new Exception(format("%s",tmp));
    } finally
    {
        main();
    }
}
Output:
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Segmentation 
 fault
Feb 15
parent reply drug <drug2004 bk.ru> writes:
15.02.2017 19:00, berni пишет:
 I'm not sure if this is considered a bug:

 import std.stdio;
 import std.string;

 int c = 0;

 void main()
 {

    try {
        write(++c," ");
        stdout.flush();
        int[100000] tmp;
        throw new Exception(format("%s",tmp));
    } finally
    {
        main();
    }
 }
Output:
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Segmentation fault
No, you recursively call main() and get segfault (due to stack overflow) as expected If you downsize tmp array then you get segfault later
Feb 15
parent reply berni <berni example.com> writes:
On Wednesday, 15 February 2017 at 16:11:36 UTC, drug wrote:
 No, you recursively call main() and get segfault (due to stack 
 overflow) as expected
I thought, that an stack overflow leeds to an exception. But that's not true, as I now see. Thanks for your answer.
Feb 15
parent bauss <jj_1337 live.dk> writes:
On Wednesday, 15 February 2017 at 19:56:31 UTC, berni wrote:
 On Wednesday, 15 February 2017 at 16:11:36 UTC, drug wrote:
 No, you recursively call main() and get segfault (due to stack 
 overflow) as expected
I thought, that an stack overflow leeds to an exception. But that's not true, as I now see. Thanks for your answer.
That's a language implementation and may not be true in *all* languages. Theoretically a stackoverflow cannot be a thrown exception (Even in languages where it appears so such as c#), because it would require space in the stack, which obviously you just overflowed, so there's none. That's why exceptions such as that would be reallocated at runtime before program start, but that may not be the case for all languages. I'm not sure what D does, but it seems like it does nothing, hence why there's no exception for it and you just end up with a segfault. Also remember that D is a system's programming language, so things like that may not be optimal in all environments and all operating systems.
Feb 17