Actual Size of undeclared object?

BlitzMax Forums/BlitzMax Beginners Area/Actual Size of undeclared object?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

What has me confused is that in the example above, the field Big:TBig in Type TBiggest only adds 4 bytes to the size of the type. Before any TBiggests are instantiated it is acting as a pointer. Obviously if I create a new TBiggest the compiler is going to have to set aside the memory for "Big", but if I simply declare the variable it shouldn't need to.


Anyhow, I ran some more tests - this time with GCMemAlloced() and now I'm more confused

Print GCMemAlloced()
Local Big:TBig
Print GCMemAlloced()

Print GCMemAlloced()
Local Bigger:TBigger
Print GCMemAlloced()

Print GCMemAlloced()
Local Biggest:TBiggest
Print GCMemAlloced()


Each call to GCMemAlloced showed that 22 more bytes were alloced.


Okay, nevermind. I just did some more tests. I put a bunch of Print GCMemAlloced() all by themselves and it seems that *just the call to GCMemAlloced() (or perhaps Print)* caused the allocation of the 22 bytes.

So, in reality, no memory is set aside when a new variable is *declared*.

My next test indicated that the above sizeof() values + 8 bytes were allocated whenever the variable was actually defined. E.G:

Global Big:TBig
Big = new TBig


That allocated 42 bytes total. Subtract 22 for the GCMemAlloc() calls and we're left with 20 bytes. The original size + 8. TBigger and TBiggest, after subtracting the 22 bytes, returned 44 and 48 bytes respectively - which confirms that the size of the type + 8 bytes were allocated.

And now to test what happens when you assign an undeclared variable to an already declared variable.

Print GCMemAlloced()
Local Bigger:TBigger
Local Bigger2:TBigger        ' <---same whether commented out or not
Bigger = New TBigger
Bigger2 = Bigger                 '<---same whether commented out or not
Print GCMemAlloced()


What's strange is 66 bytes were allocated whether the lines in question above is commented out or not. Subtract the 22 bytes for the GCMemAlloced() calls and once again, 44 bytes were allocated. Sizeof(TBigger) + 8.

This also allocated only 66 bytes

Print GCMemAlloced()
Local Bigger:TBigger
Local Bigger2:TBigger
Local Bigger3:Tbigger
Local Bigger4:TBigger
Local Bigger5:TBigger

Bigger = New TBigger
Bigger2 = Bigger
Bigger3 = Bigger
Bigger4 = Bigger
Bigger5 = Bigger
Print GCMemAlloced()


So it looks like you can in fact use an undefined object as a pointer without BMax allocating memory for the full size of the type (in fact...No memory is allocated???)

Strange.