www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Memory management and Html embeded

reply "G.Vidal" <gyvidal wanadoo.fr> writes:
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
next sibling parent reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
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
parent reply "G.Vidal" <gyvidal wanadoo.fr> writes:
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
parent reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
 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
parent reply "G.Vidal" <gyvidal wanadoo.fr> writes:
 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 16 2005
parent reply Mike Parker <aldacron71 yahoo.com> writes:
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 16 2005
next sibling parent Mike Parker <aldacron71 yahoo.com> writes:
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 16 2005
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
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 16 2005
next sibling parent "G.Vidal" <gyvidal wanadoo.fr> writes:
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.
May 17 2005
prev sibling parent reply Mike Parker <aldacron71 yahoo.com> writes:
Derek Parnell wrote:
 On Tue, 17 May 2005 11:12:36 +0900, Mike Parker wrote:
 Ummmm... don't think it does have an opAssign. I wish it did! It would make
 my coding life a lot easier.
Until I read this post I was utterly convinced D does have an opAssign and I was sure I had used it in my code. Going back and reading the language reference and looking at the code in question, I was wrong on both counts. So why don't we have an opAssign?
May 17 2005
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Wed, 18 May 2005 15:39:21 +0900, Mike Parker wrote:

 Derek Parnell wrote:
 On Tue, 17 May 2005 11:12:36 +0900, Mike Parker wrote:
 Ummmm... don't think it does have an opAssign. I wish it did! It would make
 my coding life a lot easier.
Until I read this post I was utterly convinced D does have an opAssign and I was sure I had used it in my code. Going back and reading the language reference and looking at the code in question, I was wrong on both counts. So why don't we have an opAssign?
I think its because Walter believes that the '=' symbol is ambiguous. It doesn't tell you if the value or the reference is being copied/assigned. And to overload this symbol would only cause bugs to happen if people didn't know this. What would be good would be to have a symbol (operator) that could (must be?) be overloaded that only means assign-by-value regardless of whether the identifier is a built-in (native type), struct, array, or class object. Now that *would* be useful. Many people over the years have suggested that ":=" be used for this purpose. Walter is yet to agree with this idea. -- Derek Melbourne, Australia 18/05/2005 5:09:57 PM
May 18 2005
parent Brian White <bcwhite precidia.com> writes:
 I think its because Walter believes that the '=' symbol is ambiguous. It
 doesn't tell you if the value or the reference is being copied/assigned.
 And to overload this symbol would only cause bugs to happen if people
 didn't know this. 
The source of the opEquals ambiguity comes from the ambiguity that instantiating a type sometimes creates an instance of that type and sometimes creates a reference to that type. It seems to me that the best option would be to remove the ambiguity. And the best way I can see to do this is to remove the definition that a class is always a reference and then add the explicit restriction (it's currently implicit) to not allow classes to be instantiated on the stack or as a structure member. This boils down to requiring class instances to be defined, not as MyClass cvar; // is always a null reference but instead as MyClass* cvar; // always defaults to null Everything else would behave the same. Then, "=" is always a bit copy, the ambiguity is removed, and you can safely allow it to be overloaded. Trying to do "*cvar1 = *cvar2" would result in a call to an undefined method unless that class had specifically had an operator= defined. This would also fix a common confusion when first beginning (myself included) of the following: MyClass cvar; cvar.foo(); // segmentation fault This is perfectly legal in C++ and so it's a natural thing to try when first learning D. Brian ( bcwhite precidia.com ) ------------------------------------------------------------------------------- It's not the days in your life, but the life in your days that counts.
May 18 2005
prev sibling parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
"Mike Parker" <aldacron71 yahoo.com> wrote in message 
news:d6enuc$1htn$1 digitaldaemon.com...
 Derek Parnell wrote:
 On Tue, 17 May 2005 11:12:36 +0900, Mike Parker wrote:
 Ummmm... don't think it does have an opAssign. I wish it did! It would 
 make
 my coding life a lot easier.
Until I read this post I was utterly convinced D does have an opAssign and I was sure I had used it in my code. Going back and reading the language reference and looking at the code in question, I was wrong on both counts. So why don't we have an opAssign?
http://www.digitalmars.com/d/faq.html#assignmentoverloading
May 18 2005
parent Derek Parnell <derek psych.ward> writes:
On Wed, 18 May 2005 09:59:20 -0400, Ben Hinkle wrote:

 "Mike Parker" <aldacron71 yahoo.com> wrote in message 
 news:d6enuc$1htn$1 digitaldaemon.com...
 Derek Parnell wrote:
 On Tue, 17 May 2005 11:12:36 +0900, Mike Parker wrote:
 Ummmm... don't think it does have an opAssign. I wish it did! It would 
 make
 my coding life a lot easier.
Until I read this post I was utterly convinced D does have an opAssign and I was sure I had used it in my code. Going back and reading the language reference and looking at the code in question, I was wrong on both counts. So why don't we have an opAssign?
http://www.digitalmars.com/d/faq.html#assignmentoverloading
Yes I understand this, but I think that Walter has a case of myopia. He seems to assume that because he can't see any good reason then it also means that nobody else can either. In any case, the concept of 'copy-by-value' is used by all programmers at some point and would be very useful to have an overloadable operator symbol that represented that operation. If you don't care to use the current '=' that's fine by me, but some other operator token is surely possible, no? And copy-by-value is *not* the same as a bit copy as it could involve some data manipulation (read: conversion-cast). I can imagine some class or struct in which this below is reasonable ... Foo a = new Foo; double x; a := "1234"; // Convert the string to a numeric value. . . . a := 34.56; // Convert the floating point to Foo's internal format. x := a * 3; -- Derek Parnell Melbourne, Australia 19/05/2005 2:03:19 AM
May 18 2005
prev sibling parent Derek Parnell <derek psych.ward> writes:
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