Thread overview
Is it safe to read to memory after it has been allocated with `pureMalloc` and `pureRealloc`?
Apr 04, 2022
rempas
Apr 04, 2022
Salih Dincer
Apr 04, 2022
rempas
Apr 04, 2022
Salih Dincer
Apr 04, 2022
Stanislav Blinov
Apr 04, 2022
rempas
April 04, 2022

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?

April 04, 2022

On Monday, 4 April 2022 at 07:32:00 UTC, rempas wrote:

>

Does anyone knows what's going on here?

Source code?

April 04, 2022

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'.

April 04, 2022

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.

April 04, 2022

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'.

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

April 04, 2022

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!