Memory allocation faile on string concat

Jonathan M Davis jmdavisProg at gmx.com
Wed Nov 10 10:31:52 PST 2010


On Wednesday, November 10, 2010 08:33:45 Xie wrote:
> Can't run a simple program. What's wrong, GC?
> 
> import std.stdio;
> import std.date;
> 
> void f0()
> {
> 	wstring a[];
> 
> 	foreach(i; 0 .. 100_000_000)
> 	{
>   		a ~= " "w;
>   	}
> }
> 
> void main()
> {
> 	auto r = benchmark!(f0)(1);
> 	writeln(r, "ms");
> }
> 
> DMD 2.047

You just tried to create an array with 100_000_000 elements, and arrays try and 
maintain extra capacity for extra appending, so you're actually trying to create 
one bigger than that. That's a _lot_ of memory, and it's _contiguous_ memory. 
You probably hit some sort of memory limit in there somewhere and would have the 
same problem if you tried to create an array that large in C or C++. That's an 
_enormous_ array. Maybe it's a 32-bit barrier which will go away once 64-bit dmd 
is complete, but it's not exactly normal to try and use an array wich 100 
million elements in it. The computer does have limits you know.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list