www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.typecons scoped question

reply "Martin" <martinbbjerregaard gmail.com> writes:
What is the reason behind the wasted memory when using the scoped 
template from std.typecons? For example

class A { }

class B
{

	typeof(scoped!A()) a;
	
	this()
	{
		a = scoped!A();
	}

}

void main(string[] args)
{
	writeln("Size of class A: ", __traits(classInstanceSize, A));
	writeln("Size of class B: ", __traits(classInstanceSize, B));
}

Prints the following:
"Size of class A: 8"
"Size of class B: 24"

An extra 16 bytes for alignment? Is there no options like 
align(1) to pack the memory of the class A instance inside class 
B?
Sep 07 2013
parent reply "Martin" <martinbbjerregaard gmail.com> writes:
Sorry, what I meant was:
An extra 8 bytes for alignment
Sep 07 2013
parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Saturday, 7 September 2013 at 09:24:29 UTC, Martin wrote:
 Sorry, what I meant was:
 An extra 8 bytes for alignment
AFAIK, the problem is note one of packing, but of aligning the "Payload" regardless of the where the "ScopedResult" itself is located on the stack. If "A" needs to be 16 aligned, but B is in a position of "4 + 16*N", then the payload will need to shift A an extra 12 bytes. And I don't think there's any way around this. THAT SAID, I *think* the current implementation is very conservative. On my machine, B's size is 48 (!). We could definitely reduce the extra padding. You should file this as a bug.
Sep 07 2013