Jump to page: 1 2
Thread overview
Static array initialisation
Mar 31, 2021
DLearner
Mar 31, 2021
Imperatorn
Mar 31, 2021
DLearner
Mar 31, 2021
DLearner
Mar 31, 2021
russhy
Apr 01, 2021
DLearner
Apr 01, 2021
user1234
Apr 01, 2021
Ali Çehreli
Apr 01, 2021
Imperatorn
March 31, 2021
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

March 31, 2021
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?
March 31, 2021
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}.

March 31, 2021
On 3/31/21 1:54 PM, 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}.

When you look beyond the bounds of where you have access to, you are bound to see just about anything.

The answer is no, the compiler does not write to memory beyond the 100 elements. That memory *might* happen to have an 8 in there. That's not proof of anything though.

-Steve
March 31, 2021
On Wednesday, 31 March 2021 at 18:00:32 UTC, Steven Schveighoffer wrote:
> On 3/31/21 1:54 PM, 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}.
>
> When you look beyond the bounds of where you have access to, you are bound to see just about anything.
>
> The answer is no, the compiler does not write to memory beyond the 100 elements. That memory *might* happen to have an 8 in there. That's not proof of anything though.
>
> -Steve

I entirely agree - I wasn't saying anything was wrong, but I _was_ surprised.
Not least because it's not chance, I changed the initial value and the effect repeated with the new value.
March 31, 2021
On 3/31/21 2:03 PM, DLearner wrote:
> On Wednesday, 31 March 2021 at 18:00:32 UTC, Steven Schveighoffer wrote:

>> The answer is no, the compiler does not write to memory beyond the 100 elements. That memory *might* happen to have an 8 in there. That's not proof of anything though.
>>
> I entirely agree - I wasn't saying anything was wrong, but I _was_ surprised.
> Not least because it's not chance, I changed the initial value and the effect repeated with the new value.

It's by chance, the only correlation is probably that you are looking at stack data that was set up to call the function that initialized the static array. Maybe it's saving some registers and the register happens to contain 8 (not surprising).

Or some other reason. Again, you are looking at data that the array doesn't own, and therefore the compiler can put anything in there, it could be 42 on another compiler, on another platform, or maybe if you call some other functions first, it changes.

-Steve
March 31, 2021
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?
April 01, 2021
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

It's a bit interesting though.

The behavior seems to vary a bit between compilers.

DMD:
ubyte[100] example.MemPool:
        db      008h,008h,008h,008h,008h,008h,008h,008h     ;........
        db      008h,008h,008h,008h,008h,008h,008h,008h     ;........
        db      008h,008h,008h,008h,008h,008h,008h,008h     ;........
        db      008h,008h,008h,008h,008h,008h,008h,008h     ;........
        db      008h,008h,008h,008h,008h,008h,008h,008h     ;........
        db      008h,008h,008h,008h,008h,008h,008h,008h     ;........
        db      008h,008h,008h,008h,008h,008h,008h,008h     ;........
        db      008h,008h,008h,008h,008h,008h,008h,008h     ;........
        db      008h,008h,008h,008h,008h,008h,008h,008h     ;........
        db      008h,008h,008h,008h,008h,008h,008h,008h     ;........
        db      008h,008h,008h,008h,008h,008h,008h,008h     ;........
        db      008h,008h,008h,008h,008h,008h,008h,008h     ;........
        db      008h,008h,008h,008h,000h,000h,000h,000h     ;........
        db      000h,000h,000h,000h,000h,000h,000h,000h     ;........

LDC:
ubyte[100] example.MemPool:
        .zero   100,8

GDC:
ubyte[100] example.MemPool:
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8
        .byte   8

As you can see, all have 100 valid values, but DMD seems to be aligning to a multiple.
April 01, 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;
   }
}
April 01, 2021
On Thursday, 1 April 2021 at 09:30:28 UTC, DLearner wrote:
> On Wednesday, 31 March 2021 at 23:21:59 UTC, russhy wrote:
>> On Wednesday, 31 March 2021 at 17:54:38 UTC, DLearner wrote:
>>> [...]
>>
>> 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;
>    }
> }

memset would fill a whole page even if the specified size is in the middle of a page. Try with 64, 4096 for example, instead of 100.
« First   ‹ Prev
1 2