www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is it safe to read to memory after it has been allocated with

reply rempas <rempas tutanota.com> writes:
In other terms, do these functions auto-initialize memory to be 
ready for use? I test it out using `printf` to print a string. 
"%s" expects for a null terminated string and it seems to work so 
I suppose that these functions auto-initialize the bytes to `\0`.

However, memory is tricky and I still don't know how memory (and 
virtual memory) works under the hood so it may just randomly work 
and it may give me a segmentation fault at some point.

Does anyone knows what's going on here?
Apr 04 2022
next sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
On Monday, 4 April 2022 at 07:32:00 UTC, rempas wrote:
 Does anyone knows what's going on here?
Source code?
Apr 04 2022
parent reply rempas <rempas tutanota.com> writes:
On Monday, 4 April 2022 at 07:39:08 UTC, Salih Dincer wrote:
 On Monday, 4 April 2022 at 07:32:00 UTC, rempas wrote:
 Does anyone knows what's going on here?
Source code?
Why does it matter? ``` import core.memory; import core.stdc.stdio; import core.stdc.stdlib; extern (C) void main() { char* str = cast(char*)pureMalloc(23); str[0] = 'J'; str[1] = 'o'; str[2] = 'h'; str[3] = 'n'; printf("My name is: %s\n", str); exit(0); } ``` Maybe, I didn't explained it properly. The example works. However, I wonder if it randomly works or if it is safe to do something like that as if the bytes have been initialized to '\0'.
Apr 04 2022
parent Salih Dincer <salihdb hotmail.com> writes:
On Monday, 4 April 2022 at 07:48:40 UTC, rempas wrote:
 
 Maybe, I didn't explained it properly. The example works. 
 However, I wonder if it randomly works or if it is safe to do 
 something like that as if the bytes have been initialized to 
 '\0'.
```d import core.memory : pureMalloc; import core.stdc.stdio : printf; //extern (C) void main() { int limit = 23; auto str = cast(char*)pureMalloc(limit); //while (limit--) str[limit] = '\0'; // You need this initialize ---^ str[0] = 'J'; str[1] = 'o'; str[2] = 'h'; str[3] = 'n'; printf("My name is: %s\n", str); // Wrong Output: "My name is: John�U" } ``` I found no problems when using ```extern()```. But it doesn't inspire confidence. There is obviously a memory conflict when it removes ```while()``` loop and does not use ```ekstern()```. SDB 79
Apr 04 2022
prev sibling parent reply Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Monday, 4 April 2022 at 07:32:00 UTC, rempas wrote:
 In other terms, do these functions auto-initialize memory to be 
 ready for use?
No. Neither `malloc` nor `realloc` (for which D's `pure...` variants are mere wrappers) are specified to initialize allocated memory. `calloc`, however, is - it initializes allocated block with zeroes.
Apr 04 2022
parent rempas <rempas tutanota.com> writes:
On Monday, 4 April 2022 at 09:26:13 UTC, Stanislav Blinov wrote:
 No. Neither `malloc` nor `realloc` (for which D's `pure...` 
 variants are mere wrappers) are specified to initialize 
 allocated memory. `calloc`, however, is - it initializes 
 allocated block with zeroes.
Thanks, that's what I was looking for! I'll switch to `calloc` instead. Have a great day!
Apr 04 2022