Stack or heap? What's that?

Manfred Hansen m.hansen at kielnet.net
Mon Jan 15 09:05:33 PST 2007


orgoton wrote:

> 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).
> 
> 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;
> }
> 
> 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.

One question. What do the destructor ~this ?
Delete he also the reference on the stack to the object
in the heap ? 

Manfred



More information about the Digitalmars-d mailing list