www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - When I should create structures with new keywords?

reply Suliman <evermind live.ru> writes:
In the past I asked Adam about when I should use keyword `new` 
with structures and got next answer:

"The File in the first one is put on the stack as a reference 
counted
local object whereas the second one would be on the garbage 
collected
heap, which often isn't what you want for files since you want to 
close
them when you're done.

Generally, D's structs often work best without the `new` keyword 
since
they can be made in place."

But I have read that stack size is very limited I tried to test 
two variants of reading big (20MB) text file:

auto file = File("test.txt", "r");
and
auto file = new File("test.txt", "r");

Tests does not show any significant difference in performance. 
But I still not understand where the data is location in first 
variant?
Jan 26
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 26 January 2017 at 19:30:54 UTC, Suliman wrote:
 But I still not understand where the data is location in first 
 variant?
It is stored in the file itself. The File struct is pretty small, it just provides access to the contents of the file, it doesn't actually hold that content itself.
Jan 26