www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - nothrow functions/methods

reply bearophile <bearophileHUGS lycos.com> writes:
Before filing a possible bug report I prefer to ask here, because my ideas are
often wrong.
This is a small D2 program:

nothrow void foo() {
    // auto a = new int[5]; // not allowed here
    int[int] aa;
    for (int i; i < 100_000_000; i++)
        aa[i] = i;
}
void main() {
    foo();
}

Inside that function foo() you can't put a "new int[5]" I presume because it
can throw a memory overflow exception. But then why are insertions into an
associative array allowed? Can't they produce the same memory overflow
exception? In theory nothrow functions can forbid adding new items to
associative arrays.


[Related: In Python stack overflows are normal exceptions that you can catch,
etc. But I presume it's OK for nothrow functions in D2 to not refuse the
possibility of a stack overflow error, because essentially all D2 functions can
produce stack overflows, if before calling them the stack was filled up.]

Bye,
bearophile
Feb 28 2010
parent reply Daniel Murphy <yebbliesnospam gmail.com> writes:
bearophile Wrote:

 Before filing a possible bug report I prefer to ask here, because my ideas are
often wrong.
 This is a small D2 program:
 
 nothrow void foo() {
     // auto a = new int[5]; // not allowed here
     int[int] aa;
     for (int i; i < 100_000_000; i++)
         aa[i] = i;
 }
 void main() {
     foo();
 }
 
 Inside that function foo() you can't put a "new int[5]" I presume because it
can throw a memory overflow exception. But then why are insertions into an
associative array allowed? Can't they produce the same memory overflow
exception? In theory nothrow functions can forbid adding new items to
associative arrays.
 
 
 [Related: In Python stack overflows are normal exceptions that you can catch,
etc. But I presume it's OK for nothrow functions in D2 to not refuse the
possibility of a stack overflow error, because essentially all D2 functions can
produce stack overflows, if before calling them the stack was filled up.]
 
 Bye,
 bearophile
Allocations are allowed inside nothrow functions. If I remember correctly, the reasons are that disallowing them would limit usefulness, and that OutOfMemoryError is not generally recoverable.
Feb 28 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Daniel Murphy:
 Allocations are allowed inside nothrow functions.  If I remember correctly,
the reasons are that disallowing them would limit usefulness, and that
OutOfMemoryError is not generally recoverable.<
OK. Then can you tell me why the following program produces: test2.d(1): Error: function test2.foo 'foo' is nothrow yet may throw nothrow void foo() { auto a = new int[5]; } void main() {} Bye, bearophile
Feb 28 2010
parent reply Don <nospam nospam.com> writes:
bearophile wrote:
 Daniel Murphy:
 Allocations are allowed inside nothrow functions.  If I remember correctly,
the reasons are that disallowing them would limit usefulness, and that
OutOfMemoryError is not generally recoverable.<
OK. Then can you tell me why the following program produces: test2.d(1): Error: function test2.foo 'foo' is nothrow yet may throw nothrow void foo() { auto a = new int[5]; } void main() {} Bye, bearophile
That's clearly a bug.
Feb 28 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Don:
 That's clearly a bug.
Thank you for your answer Don. http://d.puremagic.com/issues/show_bug.cgi?id=3864 (In future I will even start trying to fix them.) Bye, bearophile
Feb 28 2010