Jump to page: 1 2
Thread overview
Memory management and Html embeded
May 16, 2005
G.Vidal
May 16, 2005
G.Vidal
May 16, 2005
G.Vidal
May 17, 2005
Mike Parker
May 17, 2005
Mike Parker
May 17, 2005
Derek Parnell
May 17, 2005
G.Vidal
May 18, 2005
Mike Parker
May 18, 2005
Derek Parnell
Re: opEquals Ambiguity (was: Memory management and Html embeded)
May 18, 2005
Brian White
May 18, 2005
Ben Hinkle
May 18, 2005
Derek Parnell
May 16, 2005
Derek Parnell
May 16, 2005
Hello.

I'm a D begginner, I wrote my first class (a matrix class) today. Although it seems to work, I doubt the way I manage the memory is OK.

This is the class: http://geoffrey.vidal.neuf.fr/matrix.d.html If someone could check it out and tell me what could be improved.

Also, this is as you saw D code embedded in HTML (gvim syntax highlightning output) so it should compile directly.

But this is what I get:

jeff@Anna:~/dmd$ dmd matrix.d.html
matrix.d.html: module matrix.d has non-identifier characters in filename, use module declaration instead
jeff@Anna:~/dmd$

Hmmm ?

Thanks a lot. D rox.


May 16, 2005
Use the name matrix.html or add to the very top of the file:

module matrix;

Whic you've done, except that I believe code needs to be inside <code> tags - but I could be wrong.

For some datatypes, such as classes and arrays, you may wish to make a copy of the data when creating the matrix.  Other than that, off hand, the memory management looks fine to me.

-[Unknown]


> Hello.
> 
> I'm a D begginner, I wrote my first class (a matrix class) today.
> Although it seems to work, I doubt the way I manage the memory is OK.
> 
> This is the class: http://geoffrey.vidal.neuf.fr/matrix.d.html
> If someone could check it out and tell me what could be improved.
> 
> Also, this is as you saw D code embedded in HTML (gvim syntax
> highlightning output) so it should compile directly.
> 
> But this is what I get:
> 
> jeff@Anna:~/dmd$ dmd matrix.d.html
> matrix.d.html: module matrix.d has non-identifier characters in filename, use module declaration instead
> jeff@Anna:~/dmd$
> 
> Hmmm ?
> 
> Thanks a lot. D rox.
> 
> 
May 16, 2005
Le Mon, 16 May 2005 02:15:22 -0700, Unknown W. Brackets a écrit :

> For some datatypes, such as classes and arrays, you may wish to make a copy of the data when creating the matrix.

I haven't tried but if I do:
Matrix a = new Matrix(2, 2, 1, 2, 3, 4);
Matrix b = a;

What happen ? I suppose the content of the array is not copied, and is b a class on the stack with the content of b, or a pointer equal to a ?

How do I make b = a act so as the content of the array in a is copied in the array in b ? there's no = overloadable operator

All that seems very confusing to me.




Other than that, off hand,
> the memory management looks fine to me.

If I write something like:

( ((mat.trace()*mat + mot) * (mat.transpose() - mot)*4) + 2*mot).print();

A lot of memory is beeing allocated, when is it freed ?


Thank you






May 16, 2005
On Mon, 16 May 2005 10:59:30 +0200, G.Vidal wrote:

> Hello.
> 
> I'm a D begginner, I wrote my first class (a matrix class) today. Although it seems to work, I doubt the way I manage the memory is OK.
> 
> This is the class: http://geoffrey.vidal.neuf.fr/matrix.d.html If someone could check it out and tell me what could be improved.
> 
> Also, this is as you saw D code embedded in HTML (gvim syntax highlightning output) so it should compile directly.
> 
> But this is what I get:
> 
> jeff@Anna:~/dmd$ dmd matrix.d.html
> matrix.d.html: module matrix.d has non-identifier characters in filename, use module declaration instead
> jeff@Anna:~/dmd$
> 
> Hmmm ?
Only source code inside a <CODE> </CODE> block is compiled. You need to change the <PRE> to <CODE> etc... and get rid of the line numbers. Also, rename the file to "matrix.html" otherwise the linker (optlink) gets confused.

-- 
Derek Parnell
Melbourne, Australia
16/05/2005 7:45:00 PM
May 16, 2005
> I haven't tried but if I do:
> Matrix a = new Matrix(2, 2, 1, 2, 3, 4);
> Matrix b = a;
> 
> What happen ? I suppose the content of the array is not copied, and is b a class on the stack with the content of b, or a pointer equal to a ?

Since Matrix is a class, only the pointer is copied.  Nothing is on the stack, and no real copying occurs.

Both b and a are pointers to the heap.  After your example, they will point to the same location.  Arrays have a .dup property to make a copy of themselves.

> How do I make b = a act so as the content of the array in a is copied in
> the array in b ? there's no = overloadable operator
> 
> All that seems very confusing to me.

In most cases, you won't need that.  I meant for values:

Matrix!(char[]) test = new Matrix!(char[])(2, 2, "abc", "abc", "abc", "abc");

The array values may be on the stack, meaning that when the function exits they may evaporate.  Clearly, that's a bad thing.

> If I write something like:
> 
> ( ((mat.trace()*mat + mot) * (mat.transpose() - mot)*4) + 2*mot).print();
> 
> A lot of memory is beeing allocated, when is it freed ?

D is garbage collected.  It will be automatically freed when the garbage collector notices that there's nothing pointing to it any more.

-[Unknown]
May 16, 2005
> In most cases, you won't need that.  I meant for values:
> 
> Matrix!(char[]) test = new Matrix!(char[])(2, 2, "abc", "abc", "abc", "abc");
> 
> The array values may be on the stack, meaning that when the function exits they may evaporate.  Clearly, that's a bad thing.

I dont't understand what you mean, sorry.

First of all, a Matrix!(char[]) doesn't make any sense, as you can't add
nor multiply strings.

So given a matrix a, it's not possible to make "b = a" do a b.data = a.data.dup automatically ?

I don't remember clearly but in c++ I think we had an operator=() to do
stuff like that....


May 17, 2005
G.Vidal wrote:
>>In most cases, you won't need that.  I meant for values:
>>
>>Matrix!(char[]) test = new Matrix!(char[])(2, 2, "abc", "abc", "abc", "abc");
>>
>>The array values may be on the stack, meaning that when the function exits they may evaporate.  Clearly, that's a bad thing.
> 
> 
> I dont't understand what you mean, sorry.
> 
> First of all, a Matrix!(char[]) doesn't make any sense, as you can't add
> nor multiply strings.
> 
> So given a matrix a, it's not possible to make "b = a" do a b.data =
> a.data.dup automatically ?
> 
> I don't remember clearly but in c++ I think we had an operator=() to do
> stuff like that....
> 
> 
D also has an overloadable operator=, but it's called opAssign().

In the example you gave, *there is no copying going on* as Andrew pointed out:

Matrix a = new Matrix;
Matrix b = a;

b and a both reference the same object. D is more like Java than C++ in this case, as all class instances are references to heap allocated memory. In order to copy, you need two distinct objects:

Matrix a = new Matrix;
Matrix b = new Matrix;
b = a;

Assuming the Matrix class has an opAssign method that does what you want it to, this will work as you expect.
May 17, 2005
Mike Parker wrote:

> In the example you gave, *there is no copying going on* as Andrew pointed out:

That should read: "as Unknown pointed out".
May 17, 2005
On Tue, 17 May 2005 11:12:36 +0900, Mike Parker wrote:

> G.Vidal wrote:
>>>In most cases, you won't need that.  I meant for values:
>>>
>>>Matrix!(char[]) test = new Matrix!(char[])(2, 2, "abc", "abc", "abc", "abc");
>>>
>>>The array values may be on the stack, meaning that when the function exits they may evaporate.  Clearly, that's a bad thing.
>> 
>> I dont't understand what you mean, sorry.
>> 
>> First of all, a Matrix!(char[]) doesn't make any sense, as you can't add
>> nor multiply strings.
>> 
>> So given a matrix a, it's not possible to make "b = a" do a b.data = a.data.dup automatically ?
>> 
>> I don't remember clearly but in c++ I think we had an operator=() to do
>> stuff like that....
>> 
> D also has an overloadable operator=, but it's called opAssign().

Ummmm... don't think it does have an opAssign. I wish it did! It would make my coding life a lot easier.

> In the example you gave, *there is no copying going on* as Andrew pointed out:
> 
> Matrix a = new Matrix;
> Matrix b = a;
> 
> b and a both reference the same object. D is more like Java than C++ in this case, as all class instances are references to heap allocated memory. In order to copy, you need two distinct objects:
> 
> Matrix a = new Matrix;
> Matrix b = new Matrix;
> b = a;
> 
> Assuming the Matrix class has an opAssign method that does what you want it to, this will work as you expect.

No, you can't do that. Some people get around this limitation in D by
coding a 'dup' method (or similar) or a 'value' method (or similar)...

   Matrix dup(Matirx x)
   {
      Matrix temp = new Matrix;
      ... code to copy the elements from x ...
      return temp;
   }

used as  ...

   b = a.dup;

   void value(Matrix x)
   {
      ... code to copy the elements from x to this ...
   }

used as

  b.value = a;


-- 
Derek
Melbourne, Australia
17/05/2005 1:44:43 PM
May 17, 2005
Le Tue, 17 May 2005 13:50:32 +1000, Derek Parnell a écrit :

> Ummmm... don't think it does have an opAssign. I wish it did! It would make my coding life a lot easier.

Correct. What's wrong with opAssign ?

I made a .dup method but it's a lot uglier.

« First   ‹ Prev
1 2