Programming Windows D Examples are now Online!

Walter Bright newshound2 at digitalmars.com
Tue Jun 21 13:30:46 PDT 2011


On 6/21/2011 12:35 PM, Andrej Mitrovic wrote:
> Float/Doubles are initialized to NaN in D, however I don't know what
> they're initialized to in C.

In C they're initialized to 0 for globals, and garbage for locals and allocated 
data.


> A big issue with porting is that D's character arrays are always
> initialized to invalid values. This wreaks havoc in code, in
> particular with static arrays. For example:
>
>      char[100] buffer;
>      APIWriteToBuffer(buffer.ptr);
>      myDStringFunction(buffer[]);  //<- buggy code

In C buffer is not initialized to 0, if it is not global, it is initialized to 
garbage.

> The problem is twofold. First, the API call might not work because it
> could easily expect a null terminator to be present as a first
> character:
>      char[100] buffer;
>      APIWriteToBuffer(buffer.ptr);  // Might not work!

Fix:

        char[100] buffer = 0;

> So when you finally call a D function with such a static array, it
> could print out garbage or throw an exception:
>      char[100] buffer;
>      APIWriteToBuffer(buffer.ptr);
>      myDStringFunction(buffer[]);  // what will this function do when
> stumbles on '\0' or char.init?

Fix:
	char[100] buffer;
	APIWriteToBuffer(buffer.ptr);
         auto p = buffer[0 .. strlen(buffer.ptr)];
	myDStringFunction(p);


More information about the Digitalmars-d-announce mailing list