Inability to dup/~ for const arrays of class objects

Maxim Fomin maxim at maxim-fomin.ru
Thu May 30 02:17:41 PDT 2013


On Thursday, 30 May 2013 at 08:11:08 UTC, Diggory wrote:
> But it's clearly not the case that all slices are dynamic 
> arrays... A dynamic array is already a well-established term to 
> mean an array allocated on the heap. Slices can point to arrays 
> on the stack.

Confusion comes from calling a dynamic array as a slice and 
runtime memory as a dynamic array. Memory kind allocation and 
dynamic/static kind of array are quite ortogonal issues with high 
correlation between dynamic array and heap memory which is not 1.

extern(C) int printf(const char*,...);

struct S
{
	int[3] s;
}

int[] foo()
{
	S* s = new S; //static array in heap
	int[] ret = s.s;
	return ret; //dynamic array in heap
}

void bar(int[] data...)
{
	printf("%p\n", data.ptr); // dynamic array in stack
}

void main()
{
	int[] arr = foo();
	printf("%p\n", arr.ptr);
	bar(0,1,2);
}

http://dpaste.dzfl.pl/4df8108d

Note, that s.s in foo() is clearly a static array, but it is 
still in the heap. By the way, this kills idea that casting from 
static array to dynamic array always leads to stack memory 
pointer leak, as well as idea that 'slice to static array' can 
only point to the stack memory.


More information about the Digitalmars-d mailing list