www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Static array initialisation

reply DLearner <bmqazwsx123 gmail.com> writes:
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
Mar 31 2021
next sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
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?
Mar 31 2021
parent reply DLearner <bmqazwsx123 gmail.com> writes:
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}.
Mar 31 2021
next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
Mar 31 2021
parent reply DLearner <bmqazwsx123 gmail.com> writes:
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.
Mar 31 2021
parent Steven Schveighoffer <schveiguy gmail.com> writes:
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
Mar 31 2021
prev sibling parent reply russhy <russhy gmail.com> writes:
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?
Mar 31 2021
parent reply DLearner <bmqazwsx123 gmail.com> writes:
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; } }
Apr 01 2021
next sibling parent user1234 <user1234 12.de> writes:
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.
Apr 01 2021
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 4/1/21 2:30 AM, DLearner wrote:

     immutable uint  MemSize=100;  // Memory size in bytes.
     ubyte[MemSize]  MemPool = 8;  // Initialised to 8 for debugging.
Valid index values there are from 0 to 99, inclusive.
     WkPtr = &MemPool[0];

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

        idx = counter - 1;
So, idx will have the incorrect values of 100 and 101.
        WkUbyte = *(WkPtr + idx);
That's undefined behavior. We can talk about variable placement on the stack, padding, alignment, etc. but we cannot attempt to prove how much memory is allocated for ubyte[100] by using undefined behavior like that code. We can have a ubyte[100] as a member of a struct and then look at the bytes of an instance of that struct but we can't walk over the bytes of the function call stack randomly. I mean, we can, but the observations may not make sense. Ali P.S. MemPool.sizeof is the amount of memory used for ubyte[100]. Here is proof: void main() { alias T = ubyte[100]; T[2] arr; assert(arr.sizeof == 2 * T.sizeof); // Passes } As you can see, there is nothing other than 100 bytes used for ubyte[100]. (The results may be different for different types due to alignment requirements.)
Apr 01 2021
prev sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
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.
Mar 31 2021