Difference between: "Array of T" and T^?

Hellooooo,

i dont really know where is the actual difference between those array-Versions?

which to prefer and why?

–Shpend

You mean ^T right? ^T = pointer to T. Array of T is a managed dynamic array of T. Arrays have a length, are bounds checked and controlled by the compiler. ^T is a pointer to any T (or nil).

1 Like

Ok, so for Array better never use ^T, except for some C-lvl-Code?

like, "memcpy(void* from, void* to, size_t size);

Ok and what do you mean with: “managed”

Do you mean with it, what you wrote furthermore, or managed like in .NET?

And you said once a while, that internally, the ^T gets garbage collected very fast, what do you mean by that^^

would really like to know more about this!

a pointer is a type that points to something else. There’s no garbage collection involved at all, it always points to something else.

An array of T is an actual array, you create it, it has X elements, you can ask for its length.

You generally should always use an array. It’s a safe type, the compiler will ensure things go right with it.

2 Likes

OK and where is the difference between :

Var a: ^T := New array of T

Is that still not garbage collected

That’s going to be lost eventually when the GC hits. Don’t do that.

As I said before, pointer types are NOT considered GC objects.

Ok, and why is it possible then?

As i said, for C-lvl-Functions? or to enhence the possibilities in working with low-lvl api’s?

I am not pretty sure :confused:

Ah and how would they be deleted or freed?

via DisposeAndNil(ptr)?

Why pointers are a low level concept. And yes, among other things we use them to talk C apis.[quote=“shpend_hoti, post:7, topic:12839”]
Ah and how would they be deleted or freed?

via DisposeAndNil(ptr)?
[/quote]

Depends on where you got them, if they point to an existing object you can’t free them at all.

What do you mean now by that??

when i get a “^MyCar(record)” cant i do just: DisposeAndNil(carPtr)??

No. DisposeAndNil calls IDisposable(object).Dispose. It does NOT free anything, the gc would take care of that. Also how would you allocate a new MyCar in the first place?

var car: MyCar; //ofc some init of this record
var carPtr := @car;

or

 var carPtr : ^MyCar := new MyCar[1];  //i know, that better not to do but you asked ^^

or do you want to Point something different out?

carPtr points to car. It doesn’t copy it, it’s points to car. If car was on the stack, and carPtr stored somewhere else, it would point to a random place in the stack. You can’t free it at all (wouldn’t make sense?)

This creates a new array with 1 MyCar element, then points carPtr to the first element of it. This shouldn’t compile (there’s a bug report to disallow it). The underlying array will probably get gc’d the next time the GC hits.

You really should avoid pointers in most cases, unless you know what you are doing.

1 Like