arrays && functions && pointers

Regan Heath regan at netwin.co.nz
Mon Jun 19 16:43:12 PDT 2006


On Mon, 19 Jun 2006 23:14:01 +0000 (UTC), MM <MM_member at pathlink.com>  
wrote:
> I did a new test:
>
> import std.c.stdlib;
> import std.stdio;
> import std.perf;
>
> const int TILEW = 1000;
> const int TILEH = 1000;
>
> struct TILE { int x,y; int z[1]; }
>
> void foo(TILE arr[TILEW][TILEH], int i) {
> arr[i%TILEW][i%TILEH].x=i;
> }
>
> void main()
> {
> PerformanceCounter c = new PerformanceCounter();
> int i;
>
>
> TILE[TILEH][TILEW] arr2;
> c.start();
> for(i = 0; i < 10000000; i++) foo(arr2,i);
> c.stop();
> writefln("foo took: ",c.milliseconds());
>
> }
>
> It runs without a problem... I really don't know why and how, because  
> just yesterday it didn't :/
> Is this maybe because some other program was using the same stack?

No. Each program gets it's own stack space. I believe it's possible to set  
the stack size on linux, not sure about windows. I am not sure what the  
default stack size is on either OS.

> If this is true, how do I check how much is left on the stack?

I'm not sure. I think you can obtain the address of the bottom or top of  
the stack and then measure the distance between that and the most recent  
stacked variable.. but you still need the total stack size, no idea really.

> I'm really not into this, I'm sorry.

It's not a problem at all. All I wanted was someone to check if my code  
was doing something silly :)

> The maximum size of an static array should be 16MB.
> At least it says so here :)
> http://www.digitalmars.com/d/arrays.html

I think this limit has something to do with the size of the data segment  
in an executable, as opposed to the stack size of a running process,  
though the two may be related.

> Btw, is a stacked array a bad thing?

I think that depends. Allocating memory on the stack is faster than on the  
heap, but if you're just allocating one huge block there should be no  
noticable difference (as it only occurs once). Stack memory is limited by  
the stack space, so why would you want to use it all up for no gain in  
speed? (or anything else). I think generally it's just a better idea to  
allocate large arrays on the heap using calloc or new or similar.

Regan



More information about the Digitalmars-d-learn mailing list