Question about Allocating

wobbles via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 26 01:09:47 PST 2015


On Monday, 26 January 2015 at 07:52:06 UTC, Gan wrote:
> I've been working on my game and am getting some pretty gnarly 
> memory problems. I think it's how I'm allocating.
>
> Sometimes when I use variables I can do Color(255, 255, 255). 
> But why is that different than new Color(255, 255, 255)?
> Same when I'm making arrays. new int[](0) vs [].
>
>
> What's the difference?

Color(255, 255, 255) will return Color.
new Color(255, 255, 255) will return Color*.
The difference is the * is stored in general purpose memory and 
is allocated at runtime, meaning the GC will come into play.

Allocating without the new keyword makes the compiler set aside 
the correct amount of memory for the object at compile time, thus 
avoiding requesting new memory from the OS at runtime.

Theres a bit about it in ali's book that may help: 
http://ddili.org/ders/d.en/class.html


More information about the Digitalmars-d-learn mailing list