Stack or heap? What's that?

Carlos Santander csantander619 at gmail.com
Fri Jan 12 11:38:34 PST 2007


orgoton escribió:
> One difference between C and D is that data is allocated on the heap instead
> of the stack or something like that. What does that mean?
> 
> Also, I have a problem in a program of mine. It's something like
> 
> Open up a file using C functions FILE * file=fopen(filename, 'r');
> I read in two numbers which represent the size of my table. On the private
> part of the class I have int table[][];
> Not i put table.length=size1; Next foreach (int[] segment; table[])
> segment.length=size2;
> 
> Next I want to read the data from the file and voil�! Function fread fails! (0
> bytes read).
> 

Use foreach (inout int[] segment; table[])

> I rewrote it:
> table.length=size1;
> for auto i=0; i<size1; i++)
> for (auto j=0; j<size2; j++)
> {
> int number;
> fread(file, &number);
> table[i]~=number;
> }
> 

You could also set the length before the second for.

> This works as expected. This is slower of course, because at each iteration it
> has to reserve more memory. The algorith is basically the same. Initially I
> blammed the GC but when I disable() it or even addRoot(file) it still doesn't
> work, so I settled for the second way.


-- 
Carlos Santander Bernal



More information about the Digitalmars-d mailing list