www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Question about Allocating

reply "Gan" <avisaria me.com> writes:
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?
Jan 25 2015
parent "wobbles" <grogan.colin gmail.com> writes:
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
Jan 26 2015