Static array initialisation

DLearner bmqazwsx123 at gmail.com
Thu Apr 1 09:30:28 UTC 2021


On Wednesday, 31 March 2021 at 23:21:59 UTC, russhy wrote:
> On Wednesday, 31 March 2021 at 17:54:38 UTC, DLearner wrote:
>> On Wednesday, 31 March 2021 at 17:46:25 UTC, Imperatorn wrote:
>>> On Wednesday, 31 March 2021 at 17:27:44 UTC, DLearner wrote:
>>>> Hi
>>>>
>>>> I did:
>>>>    immutable uint  MemSize=100;  // Memory size in bytes.
>>>>
>>>> // Memory Pool
>>>>    ubyte[MemSize]  MemPool = 8;
>>>>
>>>> And had a look in memory.
>>>> I think the compiler set up 101 '8's, not 100 in memory.
>>>>
>>>> Which I did not expect.
>>>>
>>>> Best regards
>>>
>>> When I look there are 100. What makes you think there are 101?
>>
>> I printed bytes from &MemPool[0] to just beyond &MemPool[99}.
>
> Can you show the print function?
>
> Maybe the problem lies there?

Using rdmd, I believe the code below demonstrates the situation.

import std.stdio;
void main() {
    immutable uint  MemSize=100;  // Memory size in bytes.
    uint counter;
    uint idx;
    ubyte* WkPtr;
    ubyte WkUbyte;

// Memory Pool
    ubyte[MemSize]  MemPool = 8;  // Initialised to 8 for 
debugging.

    WkPtr = &MemPool[0];

    counter = 1;
    while (counter <= 102) {

       idx = counter - 1;
	
	  WkUbyte = *(WkPtr + idx);

       writeln("idx = ", idx, "WkUbyte = ", WkUbyte);

       counter = counter + 1;
    }
}


More information about the Digitalmars-d-learn mailing list