Thread overview
Unexpected result with -betterC
Nov 03, 2024
DLearner
Nov 03, 2024
monkyyy
Nov 03, 2024
DLearner
Nov 03, 2024
Juraj
Nov 03, 2024
monkyyy
Nov 03, 2024
Paul Backus
November 03, 2024

The two fragments below compiled and ran as expected using dmd -betterC under Windows.

   string Scrn = "OPO NAM='DspVar1' POS='1,1' VAR=('IntVar1','I');E";


   printf("\nWR_Createtest entered.\n");


   OpStructFstPtr = WR_CreateFormatFile(Scrn);

OpStruct* WR_CreateFormatFile(string parm_Format) {

   import core.stdc.stdio: printf;
   import core.stdc.stdlib: malloc;


   OpStruct* FstOpStructPtr;


   if (parm_Format[0..3] == "OPO"[0..3]) {
      printf("OPO Found.\n");
   } else {
      printf("OPO NOT Found.\n");
   }

   if (parm_Format[$-1..$] == "E"[0..1]) {
      printf("E Found.\n");
   } else {
      printf("E NOT Found.\n");
   }

However, the docs say dynamic arrays are not allowed with betterC, and 'string' implies a dynamic array.
So I expected DMD to complain that my code was invalid.

Any ideas?

November 03, 2024

On Sunday, 3 November 2024 at 19:00:33 UTC, DLearner wrote:

>

The two fragments below compiled and ran as expected using dmd -betterC under Windows.

   string Scrn = "OPO NAM='DspVar1' POS='1,1' VAR=('IntVar1','I');E";


   printf("\nWR_Createtest entered.\n");


   OpStructFstPtr = WR_CreateFormatFile(Scrn);

OpStruct* WR_CreateFormatFile(string parm_Format) {

   import core.stdc.stdio: printf;
   import core.stdc.stdlib: malloc;


   OpStruct* FstOpStructPtr;


   if (parm_Format[0..3] == "OPO"[0..3]) {
      printf("OPO Found.\n");
   } else {
      printf("OPO NOT Found.\n");
   }

   if (parm_Format[$-1..$] == "E"[0..1]) {
      printf("E Found.\n");
   } else {
      printf("E NOT Found.\n");
   }

However, the docs say dynamic arrays are not allowed with betterC, and 'string' implies a dynamic array.
So I expected DMD to complain that my code was invalid.

Any ideas?

Id expect this to fail, but nothing I see in yours

string foo="foo";
foo~="bar";

[]'s are both slices and dynamic arrays depending on use, theres a debate about that decision(im of the opinion a [?] should be a dynamic array and clean up the api), but the current situation will only break on appends

November 03, 2024

On Sunday, 3 November 2024 at 19:47:31 UTC, monkyyy wrote:
[...]

>

Id expect this to fail, but nothing I see in yours

string foo="foo";
foo~="bar";

[]'s are both slices and dynamic arrays depending on use, theres a debate about that decision(im of the opinion a [?] should be a dynamic array and clean up the api), but the current situation will only break on appends

Surely the line:

   string Scrn = "OPO NAM='DspVar1' POS='1,1' VAR=('IntVar1','I');E";

creates, by the definition of 'string', a dynamic array?

November 03, 2024

On Sunday, 3 November 2024 at 20:04:19 UTC, DLearner wrote:

>

On Sunday, 3 November 2024 at 19:47:31 UTC, monkyyy wrote:
[...]

>

Id expect this to fail, but nothing I see in yours

string foo="foo";
foo~="bar";

[]'s are both slices and dynamic arrays depending on use, theres a debate about that decision(im of the opinion a [?] should be a dynamic array and clean up the api), but the current situation will only break on appends

Surely the line:

   string Scrn = "OPO NAM='DspVar1' POS='1,1' VAR=('IntVar1','I');E";

creates, by the definition of 'string', a dynamic array?

By default, a string literal is typed as a dynamic array, but the element count is known at compile time. So all string literals can be implicitly converted to an immutable static array. spec

November 03, 2024

On Sunday, 3 November 2024 at 19:00:33 UTC, DLearner wrote:

>

However, the docs say dynamic arrays are not allowed with betterC, and 'string' implies a dynamic array.
So I expected DMD to complain that my code was invalid.

Any ideas?

The docs are wrong. Dynamic arrays themselves are allowed; you're just not allowed to perform any operations on them that would cause memory allocation.

This includes, for example,

  • concatenating two dynamic arrays with the ~ operator.
  • appending to a dynamic array with the ~= operator.
  • extending a dynamic array by assigning to its .length property.
  • copying a dynamic array using the built-in .dup method.
November 03, 2024

On Sunday, 3 November 2024 at 20:04:19 UTC, DLearner wrote:

>

Surely the line:

   string Scrn = "OPO NAM='DspVar1' POS='1,1' VAR=('IntVar1','I');E";

creates, by the definition of 'string', a dynamic array?

I believe thats stored in the binary