Thats pretty much what I figured :-)
When you say you grew up with assembly... are we talking 8 bit time, 16 bit time or some other time?
Thats pretty much what I figured :-)
When you say you grew up with assembly... are we talking 8 bit time, 16 bit time or some other time?
:: AthenaOfDelphi :: My Blog :: My Software ::
MSX time ..
No oop present there.. We are spoiled today!
Marmin^.Style
It is faulty because:Originally Posted by marmin
myObject: TObject;
here myObject is just a "name" of some sort. Object's data in memory is not where this points to, data is elsewhere. Records work differently which is why you can use New() on them.
(You can see it yourself if you print out integer(@myObject) and integer(@myObject.[put some variable name here]) )
AND New() just couldn't know how much to allocate in memory. Sizeof() or any other method cannot tell how much memory an object uses.
And as mentioned before you don't have to put anything in constructor if you don't want to and still have full control.
But I can use New () on Objects. I have checked for memory leaks, there are none.It seems, when there are no uncertain 'elements' , such as dynamic arrays, it is ok to use it on an object. In delphi, and also in freepascal, I get no errors. I think it only raises an exception when there is uncertain element in any object (such as dynamic array). Eventually, as I like to experiment much with programming, I may change all to records , and see how much memory usage is.
Marmin^.Style
I checked with delphi basics, this is what itm said:
The New procedure comes in two flavours.
The older version is an obsolete method of creating objects (you should now call the class constructor instead).
The first version allocates storage for a pointer type variable VariablePointer.
New is used when the storage is requirement is fixed in size. Use GetMem to dictate the exact storage size allocated.
Marmin^.Style
You can use New() to create objects (not classes!).
But there is a nuance. AFAIR New() does not initialize memory as a class constructor does. It means that if you have a field which must be initialized before first use (strings, dynamic arrays, something more?) you should do it manually. It's enough to simply fill the memory allocated by New() with zeros.
Did a little pointer test that gave interesting results, some may find this useful:
[pascal]type
TMyClass = class
var1: integer;
var2: byte;
end;
var
arr: array[0..3] of TMyClass;
test: TMyClass;
procedure TForm1.FormCreate(Sender: TObject);
var i: integer;
begin
for i:=0 to 3 do arr[i]:=TMyClass.Create;
test:=TMyClass.Create;
memo1.Lines.Add(
inttostr(integer(@arr))+#13+#10
+inttostr(integer(@arr[0]))+' '+inttostr(integer(@arr[0].var1))
+' '+inttostr(integer(@arr[0].var2))+#13+#10
+inttostr(integer(@arr[1]))+' '+inttostr(integer(@arr[1].var1))+#13+#10
+inttostr(integer(@arr[2]))+' '+inttostr(integer(@arr[2].var1))+#13+#10
+inttostr(integer(@arr[3]))+' '+inttostr(integer(@arr[3].var1))+#13+#10
+inttostr(integer(@test))+' '+inttostr(integer(@test.var1))
+' '+inttostr(integer(@test.var2))+#13+#10
+inttostr(sizeof(TMyClass))+#13+#10
+inttostr(sizeof(test))+#13+#10
+inttostr(test.InstanceSize)
);
test.Free;
for i:=0 to 3 do arr[i].Free;
end;[/pascal]
Prints out:
4533204
4533204 9517872 9517876
4533208 9517888
4533212 9517904
4533216 9517920
4533220 9517936 9517940
4
4
12
@arr
@arr[0] @arr[0].var1 @arr[0].var2
@arr[1] @arr[1].var1
@arr[2] @arr[2].var1
@arr[3] @arr[3].var1
@test @test.var1 @test.var2
sizeof(TMyClass)
sizeof(test)
test.InstanceSize
Ok the last bit actually does know size for class but couldn't find pointer to data area start (var1).
Update.
I found it slightly puzzling why it would not work. It is not common practice, but it should work. As long as I use SetLength (), and I did, and stay within the array boundaries it should work.
So I tested the exact same code in Lazarus and FPC 2.2.1, and all is working fine.
And, I encountered another strange behaviour in Delphi 6 Personal (which I use) which costed me a day of frustration!. This was a fixed array! There should bo no problem with that.And still I got a strange , disgusting access violation error. I tested the code in FPC and it worked all fine.
Whast does this tell me. Is the Delphi 6 compiler outdated? Maybe the FPC compiler is smarter in allocating memory. Is it a good idea to update to turbo delphi or is the compiler the same?
Marmin^.Style
Bookmarks