digitalmars.D.learn - Static array initialisation
- DLearner (9/9) Mar 31 2021 Hi
- Imperatorn (2/11) Mar 31 2021 When I look there are 100. What makes you think there are 101?
- DLearner (2/18) Mar 31 2021 I printed bytes from &MemPool[0] to just beyond &MemPool[99}.
- Steven Schveighoffer (7/27) Mar 31 2021 When you look beyond the bounds of where you have access to, you are
- DLearner (6/33) Mar 31 2021 I entirely agree - I wasn't saying anything was wrong, but I
- Steven Schveighoffer (10/19) Mar 31 2021 It's by chance, the only correlation is probably that you are looking at...
- russhy (3/22) Mar 31 2021 Can you show the print function?
- DLearner (22/45) Apr 01 2021 Using rdmd, I believe the code below demonstrates the situation.
- user1234 (4/32) Apr 01 2021 memset would fill a whole page even if the specified size is in
- =?UTF-8?Q?Ali_=c3=87ehreli?= (22/29) Apr 01 2021 So, idx will have the incorrect values of 100 and 101.
- Imperatorn (140/149) Mar 31 2021 It's a bit interesting though.
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
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 regardsWhen I look there are 100. What makes you think there are 101?
Mar 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:I printed bytes from &MemPool[0] to just beyond &MemPool[99}.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 regardsWhen I look there are 100. What makes you think there are 101?
Mar 31 2021
On 3/31/21 1:54 PM, DLearner wrote:On Wednesday, 31 March 2021 at 17:46:25 UTC, Imperatorn wrote: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. -SteveOn Wednesday, 31 March 2021 at 17:27:44 UTC, DLearner wrote:I printed bytes from &MemPool[0] to just beyond &MemPool[99}.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 regardsWhen I look there are 100. What makes you think there are 101?
Mar 31 2021
On Wednesday, 31 March 2021 at 18:00:32 UTC, Steven Schveighoffer wrote:On 3/31/21 1:54 PM, DLearner wrote: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.On Wednesday, 31 March 2021 at 17:46:25 UTC, Imperatorn wrote: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. -SteveOn Wednesday, 31 March 2021 at 17:27:44 UTC, DLearner wrote:I printed bytes from &MemPool[0] to just beyond &MemPool[99}.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 regardsWhen I look there are 100. What makes you think there are 101?
Mar 31 2021
On 3/31/21 2:03 PM, DLearner wrote:On Wednesday, 31 March 2021 at 18:00:32 UTC, Steven Schveighoffer wrote: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. -SteveThe 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.
Mar 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:Can you show the print function? Maybe the problem lies there?On Wednesday, 31 March 2021 at 17:27:44 UTC, DLearner wrote:I printed bytes from &MemPool[0] to just beyond &MemPool[99}.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 regardsWhen I look there are 100. What makes you think there are 101?
Mar 31 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: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; } }On Wednesday, 31 March 2021 at 17:46:25 UTC, Imperatorn wrote:Can you show the print function? Maybe the problem lies there?On Wednesday, 31 March 2021 at 17:27:44 UTC, DLearner wrote:I printed bytes from &MemPool[0] to just beyond &MemPool[99}.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 regardsWhen I look there are 100. What makes you think there are 101?
Apr 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: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.On Wednesday, 31 March 2021 at 17:54:38 UTC, DLearner wrote: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; } }[...]Can you show the print function? Maybe the problem lies there?
Apr 01 2021
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
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 regardsIt'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