Thread overview
Segfaults on Constant Definitions
Feb 20, 2005
John Reimer
Feb 20, 2005
zwang
Feb 23, 2005
Kris
Feb 24, 2005
John Reimer
Feb 24, 2005
kris
Mar 12, 2005
Thomas Kühne
February 20, 2005
OS: Gentoo Linux with Kernel 2.6

D Compiler: dmd version 0.113

CODE:
# const int a1 = 50;
# const int a2 = 50;
# const int a3 = a1 * a2;
#
# const int b1 = a1 - 1;
# const int b2 = a2 - 1;
#
# const int c1 = 50*50;
# const int c2 = (a1-1)*(a2-1);
# const int c3 = b1*b2;
#
# int[4*c1]  array1;  // illegal!
# int[4*c2]  array2;  // illegal!
# int[4*c3]  array3;  // illegal!
#
# int[a3]    array4;  // valid! no error!
#
# void main()
# {
# }

OUTPUT:
test4(8):50*50 is not an expression
test4(9):a1 - 1 * a2 - 1 is not an expression
Segmentation fault

PROBLEM:

The compiler balks on calculated constant expressions. In arrays 1-3, take out the "4*" in the array dimension to get a different set of errors (no segfault).  Comment out the individual array declarations to get more complaints from the compiler.  Furthermore, remove all array declarations to get /no/ complaints from the compiler. The compiler error only shows up when you attempt to use the constants.

Since const values are the only replacement solution for the C preprocessor "#define", why are these expressions incalculable?

Win32 version not tested!

- John R.
February 20, 2005
John Reimer wrote:
> Win32 version not tested!
> 
> - John R.

dmd win32 fails too.
February 23, 2005
In article <pan.2005.02.20.14.41.58.234018@yahoo.com>, John Reimer says...
>
>OS: Gentoo Linux with Kernel 2.6
>
>D Compiler: dmd version 0.113
>
>CODE:
># const int a1 = 50;
># const int a2 = 50;
># const int a3 = a1 * a2;
>#
># const int b1 = a1 - 1;
># const int b2 = a2 - 1;
>#
># const int c1 = 50*50;
># const int c2 = (a1-1)*(a2-1);
># const int c3 = b1*b2;
>#
># int[4*c1]  array1;  // illegal!
># int[4*c2]  array2;  // illegal!
># int[4*c3]  array3;  // illegal!
>#
># int[a3]    array4;  // valid! no error!
>#
># void main()
># {
># }
>
>OUTPUT:
>test4(8):50*50 is not an expression
>test4(9):a1 - 1 * a2 - 1 is not an expression
>Segmentation fault
>
>PROBLEM:
>
>The compiler balks on calculated constant expressions. In arrays 1-3, take out the "4*" in the array dimension to get a different set of errors (no segfault).  Comment out the individual array declarations to get more complaints from the compiler.  Furthermore, remove all array declarations to get /no/ complaints from the compiler. The compiler error only shows up when you attempt to use the constants.
>
>Since const values are the only replacement solution for the C preprocessor "#define", why are these expressions incalculable?
>
>Win32 version not tested!
>
>- John R.

consts are sometimes astonishingly weak in D. I bet it would be fine if you used enum instead

- Kris


February 24, 2005
Kris wrote:

> consts are sometimes astonishingly weak in D. I bet it would be fine if you used
> enum instead
> 
> - Kris
> 
> 

Thanks, Kris.  If there's so much trouble with const's, I think this should be fixed.  I'll see what enum can do for me.  I guess a template could even be a solution, if not overkill.

- John R.
February 24, 2005
John Reimer wrote:
> Kris wrote:
> 
>> consts are sometimes astonishingly weak in D. I bet it would be fine if you used
>> enum instead
>>
>> - Kris
>>
>>
> 
> Thanks, Kris.  If there's so much trouble with const's, I think this should be fixed.  

No question about it :)

Frankly, I'd prefer Walter to remove const completely until he's spent some time thinking about how to correctly implement read-only variables, and the related ROM-based reference data. I doubt that will happen though.

- Kris
March 12, 2005
John Reimer wrote:
| OS:
| Gentoo Linux with Kernel 2.6
|
| D Compiler:
| dmd version 0.113
|
| CODE:
| # const int a1 = 50;
| # const int a2 = 50;
| # const int a3 = a1 * a2;
| #
| # const int b1 = a1 - 1;
| # const int b2 = a2 - 1;
| #
| # const int c1 = 50*50;
| # const int c2 = (a1-1)*(a2-1);
| # const int c3 = b1*b2;
| #
| # int[4*c1]  array1;  // illegal!
| # int[4*c2]  array2;  // illegal!
| # int[4*c3]  array3;  // illegal!
| #
| # int[a3]    array4;  // valid! no error!
| #
| # void main()
| # {
| # }
|
| OUTPUT:
| test4(8):50*50 is not an expression
| test4(9):a1 - 1 * a2 - 1 is not an expression
| Segmentation fault

Added to DStress as
http://dstress.kuehne.cn/run/const_14.d

Thomas