stirling numbers and multidimensional arrays

ag0aep6g anonymous at example.com
Sun Apr 8 16:51:14 UTC 2018


On 04/08/2018 06:15 PM, popgen wrote:
> I am trying to implement some code to calculate Stirling numbers.  The 
> code shown below provides the correct calculation but throws a 
> Segmentation fault: 11 once it is done running. I suspect there is 
> something with the way I am setting up the multidimensional array.
[...]
> int stirling1(int n, int k)
> {
>          auto matrix = new int[][](n+1,k+1) ;
[...]
>          for(int i = 1; i <= n ; i++)
>          {
>              for(int q = 1; q <= i ; q++)

Should it be `q <= k` here? You're using q as an index into an array of 
length k + 1. If you go up to i, you'll exceed that and go out of bounds.

That you're seeing a segfault instead of a range error indicates that 
you're compiling with -release. Better not do that when debugging.


More information about the Digitalmars-d-learn mailing list