On Friday, 1 April 2022 at 16:24:33 UTC, Ali Çehreli wrote:
void main() {
  auto a = new int[1_000_000];

  auto b = a[0..1];  // Hold on to the first element

  a = a[$-1..$];     // Drop all but the last element

  // Are the middle elements gone, or is b keeping
  // them alive just because it still holds the very
  // first pointer?

  // Attempt to access original elements through
  // a pointer to the first element. Bug?
  auto c = b.ptr[0..1_000_000];
}

One assumption might be that the GC remembers the single large allocation as one and all of the elements are still available? But if it considers slices and their lengths specially, then it would see that the mid section is not referenced any more.
[...]

```d
import std.stdio;

// -- I couldn't type 32 here --v
enum testLimit = 1024 * 1024 * 31;
void main()
{
  char[] first ="a-123".dup;
  char* oneHalf = &first[2];      // =>[1]

  first.length = testLimit;
  first[$-1] = 'z';

  // legal? Ok.

  auto sliceLeft = first[0..2];   // "a-"
  auto sliceRight = first[$-2..$];// "�z"

  // legal? Ok.

  assert(first.length == testLimit);
  first = sliceLeft ~ sliceRight;
  assert(first.length == 4);

  char[] second = "b-321".dup;
  second.length = testLimit/2;

  first.writeln; // "a-�z"
  first.length = testLimit/2;
  first[0..2].writeln;

  // legal? Ok.

  auto numsMid = oneHalf[0..3];  // "123"
  numsMid.writeln;
  second[0..2].writeln;          // "b-"
}
```

In this 2nd test, I couldn't get the memory up to 62 MBytes. It throws the following error:
**(not a compiler error!)**

core.exception.OutOfMemoryError@src/core/exception.d(702)

In the same system configuration and in my previous attempt, I had allocated all of the memory to an array. Let's get to the conclusion here: We need to do something to get the used memory back. But what?

SDB@79

Reply via email to