NG Structs and Types

BlitzMax Forums/BlitzMax NG/NG Structs and Types

col(Posted 2016) [#1]

I'm sure you've been waiting for this question so I may as well ask it :p

I know that structs are generally allocated on the stack. So how are you handling things if you want to allocate the exact same data structure on the heap? Just duplicate everything within a Type? If that's the case how do we go about functions/methods that use them as parameters. Or are they intended purely for simple fast local allocations only?

Brucey(Posted 2016) [#2]
Hello what?

You'll need to write a wee example of what you think you want to do ;-)

col(Posted 2016) [#3]
Never mind,

I've been working in c++ all day and was thinking about the way that you can create an instance of a class/struct on the stack and then also an instance of the same class/struct on the heap using the 'new' operator. I was just thinking of the same paradigm using structs in NG.

Brucey(Posted 2016) [#4]
I've been working in Java all day, so not to worry ;-)

Structs are stack only. You can create them on the heap (perhaps via memalloc, or from C/C++) but you'll be using them via a Ptr, and they won't be GC managed.
Struct methods are basically functions that take a ptr to the struct as the first argument. (similar to how Type methods work, but Structs don't track them as function pointers, as they aren't inheritable.)

col(Posted 2016) [#5]
Can you confirm something for me please...

Say I have a Type with a Field, and that Field is a Struct instance. In an 'Init' method I create the struct instance and then directly copy the Struct to the Field, does it copy as I'd expect or is there going to be trouble further down the line with possible stack corruption? I would expect the Field instance to be safe until the Type instance is no longer 'reachable' and gets collected. When the Init method returns then the Local 'temp' would go out of scope - that doesn't mean that the values for the Field one has gone out of scope too does it?

The reason I take this opportunity to ask is that I'm getting a stack corruption when using the Vulkan module with Structs ( I've now changed the code back to Types for stability ) but I don't want to blame the Structs feature if I'm doing something obviously wrong. I'm well aware that it could be other areas of my code but things aren't working how I'm expect them to and, typical that, I can't reproduce the corrupted stack with a small example :p

Struct TestStruct
	Field value:Int

Type TTestType
	Field _inst:TestStruct
	Method Init()
		Local temp:TestStruct
		temp.value = 20
		_inst = temp

Brucey(Posted 2016) [#6]
Since Structs are pure C-Structs, if you want to pass them around and be referring to the original instance, you will need to pass by reference - when you pass by value, the struct is copied.
The copy will be invalid when scope is lost.
When you pass by reference, the contents should be valid for as long as the instance stays in scope - if it is a field of a type, that should be until the object is collected.

col(Posted 2016) [#7]
Yeah that's how I thought they would be. Thanks for confirming.