Multi-use structure element - pointers or unions?

Just starting out? Need help? Post your questions and find answers here.
Harbinger
User
User
Posts: 10
Joined: Mon Mar 18, 2019 6:40 pm

Multi-use structure element - pointers or unions?

Post by Harbinger »

Im creating multiple lists that reference particular entries by ID. In other words, each list entry will have an initial index of so many bytes that is called by other lists, to help separate the data but keep it linked. Imagine multiple lists whose first number in each line is the same, indicating that the data is linked.
So the structure of each line of the list uses a quad for its ID, but I am calling this variable in every list structure. This means that the memory for these lists will be filled with a repetition of these quads found in different lists.
I think I can use pointers to reserve memory instead if quad variables, but I'm hesitant about using them because I don't know how to use them in actual code. They're a little bit above my head and hard to understand why they're even necessary. As opposed to just using a variable.
However, I just saw in the manual something called a structure union, and even though it's a little bit complicated I wondered if I could use that to call for the same variable in each list, since they're all accessing the same exact value.

I see very little info on structure unions, so could someone help me to understand when theyre helpful, and the strategy for using them? Would they helpful in my situation, trying to save memory by referring to the same variable?
infratec
Always Here
Always Here
Posts: 7654
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Multi-use structure element - pointers or unions?

Post by infratec »

How many bytes are used for a quad variable?

And how many bytes are used for a pointer? (x64)

If you think about the answer, than you will notice that it makes not much sense to use a pointer instead of the value.
Only if you use x86, it makes a small difference.

Maybe you should think about maps instead of lists.
As key you can use the list name plus the index value.
Harbinger
User
User
Posts: 10
Joined: Mon Mar 18, 2019 6:40 pm

Re: Multi-use structure element - pointers or unions?

Post by Harbinger »

I did think about using Maps, but they cant be sorted, and i need to sort my list of numbers often. (Altho i could use maps for the secondary lists, since the initial list will have the reference to an element in the map.)

But i still didnt get my answer of when structure unions are helpful.
infratec
Always Here
Always Here
Posts: 7654
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Multi-use structure element - pointers or unions?

Post by infratec »

You use union when you want to access something by different names and maybe the variable size is different,
but it is guaranteed that the name is only needed once. Ok you can also use an array and a variable to access the bytes of the variable.

Code: Select all

Structure Test_Structure
  StructureUnion
    LongVar.l
    LongVarByte.a[4]
  EndStructureUnion
EndStructure


Define Test.Test_Structure

Test\LongVar = $12345678

Debug Hex(Test\LongVarByte[0])
Debug Hex(Test\LongVarByte[1])
Debug Hex(Test\LongVarByte[2])
Debug Hex(Test\LongVarByte[3])

Code: Select all

Structure Test_Structure
  StructureUnion
    kmh.i
    mph.i
  EndStructureUnion
  rpm.i
EndStructure


Define Test1.Test_Structure
Define Test2.Test_Structure

Test1\kmh = 100
Test1\rpm = 4000

Test2\mph = 80
Test2\rpm = 4000
Post Reply