Structure
kennyann
Posts: 113
I have used array to store by buttons number in my Netlinx program. I am wondering if I can use the structure to store those information and then use them in the Button_Event.
0
Comments
DEFINE_DEVICE
dvPANEL = 128:1:0
DEFINE_VARIABLE
nBTN_MYBUTTONS[] = { 5,6 }
DEFINE_EVENT
BUTON_EVENT[dvPANEL,nBTN_MYBUTTONS]
{
PUSH:
{
SWITCH(GET_LAST(nBTN_MYBUTTONS))
{
CASE 1: { } // means a PUSH[dvPANEL,nBTN_MYBUTTONS[1]]
CASE 2: { } // means a PUSH[dvPANEL,nBTN_MYBUTTONS[2]]
}
}
}
With that kind of coding, at Compiling time internally single BUTTON_EVENTs with any possible combination of DEVICE and CHANNEL are created. So inside the master, you'll have 2 BUTTON_EVENTs [dvPANEL,5] and [dvPANEL,6]
You can pass any combination of single DEVICE/CHANNEL , but also a mix like above, or a combination of a DEV array and a "CHANNEL" array.
AFAIK there is a limitation of about 1000 combinations into one single BUTTON_EVENT.
It's a cool way for programming, because aou are managing your Device and Channels only at the top of your code, and then you are only working with indices.
Hope this helps.
Marc
Check out tech note 383 a further explanation:
Netlinx Event Programming Rules and Parameters
Cheers,
Joe
- Chip
CHAR sCDS[3][400][30]
The first bracket is the group where 1 = country, 2= rock, 3=alternative. The second bracket is the location of the CD in the changer and the third bracket is for the character string of the CD title (30 Characters is the most you can use in the CD title.) When this array is created, it uses 3*400*30 bytes of memory. Even if you only have 1 CD in the rock group, the rock group still uses 12000 bytes of memory. The other problem with this is that you have to enter the CD in one time for each group it is in so if you have a CD that belongs in the Rock group and the Alternative group, you would have the enter the info 2 times. This makes for a greater possibility of entry errors and makes it more difficult to update when CDs change locations. Even if you feel like overcoming the programming required to manage this data, the memory use is very ineffiecient as is demonstrated by adding another group say 4=classical. Now you have 48000 bytes and each CD may have to be entered up to 4 times. Every group you add increases the memory usage by 12000 bytes. Another issue is track info such as song names and play time. With arrays, you would have to enter the song info for each CD once for each group it was in and the SONG names would take up even more memory, regardless of the number of CDs in each group. For example:
CHAR sTRACK[3][400][15][30]
This would allow 15 tracks per CD with 30 characters per track name. The memory use would be 3*400*15*30 or 540000 bytes. So, for 3 groups in a 400 disc changer allowing up to 15 tracks per CD, you would use 576000 bytes of memory.
With a structure, it is very easy to group different types of data efficiently. such as:
DEFINE_TYPE
STRUCTURE _sCD_INFO
{
CHAR Title[30]
INTEGER Group[3]
CHAR Track[15][30]
}
DEFINE_VARIABLE
_sCD_INFO uCDS[400]
Each instance of the structure uses 30 + 3*2 + 15*30 or 486 bytes of memory so the entire array uCDS uses 194400 bytes of memory. So, to get the same information, you can use 576000 bytes of memory with an array or 194400 bytes with a structure.
I also lied when I said the structure is providing the same information. The structure actually allows more information. Each CD can belong to only 3 groups, but with an array of group names, you could use the uCDS.Group array to point to up to three different group names and the memory usage increase would be almost nothing compared to the increase in adding groups to the array method.
Does this mean structures run quicker than arrays? I doubt it, but they do make for more efficient use of available memory.
With that said, to address the initial poster wanting to use structures over arrays because they use less memory. The data is allows going to take up the same ammount of space... one byte for each character is always going to be one byte in size. The reason structures use less memory than arrays is because they allow you to reduce the number of time you have to use that one byte. I don't think that the program will allow you to use structures, but more importantly, I don't think it will save you any memory nor increase your performance. Now, I am speaking from a Netlinx stand point and I have very little experience with Axcess, but if you are running with an Axcess processor and having problems with performance, I would highly suggest moving to a Netlinx processor. The small gains you might see by modifying your code will never compare to the improved performance of a netlinx processor.
I hope this helps a little and if you need any further clarification, lemme know... I'll see if I can reword something. Also, don't be afraid to tell me I'm wrong..... I've only been programming AMX for a short time and I am constantly learning new things )
Jeff
INTEGER CD_Type[400]
CHAR CD_Name[400][30]
CHAR CD_Track[400][15][30]
That would let you keep track of info for CDs in a 400 disk changer efficiently - or at least more efficiently than the example...
Yes, you can do this with a structure instead:
STRUCTURE CD
{
INTEGER CD_Type
CHAR CD_Name[30]
CHAR CD_Track[15][30]
}
Then in DEFINE_VAR
CD My_CDs[400]
But... That doesn't save any memory. Just makes the code easier to read, and perhaps run a little quicker.
- Chip
I agree that it is poor use of arrays. That is the example they used in Programming 2 or at least as close to it as best I can remember. Sorry to promote poor array usage.
Jeff
A structure is simply a user defined data type that is used to group intrinsic data types (i.e., INTEGER, CHAR) together and/or other user defined data types together in a logical and object-like manner.
If one compares apples to apples, using arrays or structures makes no difference in memory allocation. (There might even be a little overhead for a structure, I'm not sure.) The example in class was a poor one and it would have been a better comparison had they used something like what Chip posted just above.
Even though the example wasn't fair, that doesn't diminish the importance of structures. I love structures and use them extensively since they make the code much easier to read, maintain, debug, and modify. Structures are also great for VARIABLE_TO_XML and XML_TO_VARIABLE. I don't know if structures run the code any quicker or not.
As far as I know, there is no way to dynamically "new" up or release memory in Netlinx (other than data that is pushed onto and popped off of the stack) If there is, I'd love to hear about it.
Hope this clears it up for you a bit. If I've mislead you in any way hopefully someone from AMX will set it straight.
Cheers,
Joe
"What? You want to add a 101th element to a list that only has 100? Sure! Just call 'NewPtr' after checking the available memory pool size!"
<sigh> I should be counting my blessings. I'd much rather be writing software to control A/V gear than making the next spreadsheet or page layout package.
- Chip
Now proposing some form of well thought out "dynamic" list mechanism (stack or queue) would be a middle ground. You would not be able to directly manipulate memory, just add or delete something from the list. (list elements being structures of course).
Fred
In practice anyway, what would be the practical benefit of using dynamic memory? By using arrays, you will never "run out" of memory dynamically, you will know it at compile time...
Fred
Now what if the user wants to add CD #401? You?ll need to go into the code and increase the upper limits of the structure. It would be nice to be able to read data out of a file (like CD information) and then new up memory as CDs are added. There would be no need to define your limits ahead of time.
With that said though, I agree 100% that the cons out number the pros by a great margin. Memory leaks or stepping out of bounds and writing over memory that you shouldn?t be writing over is a bad bad thing. If you?re in the control business and your program goes out of control that would be another bad bad thing. Netlinx does a great job in making sure that you don?t shoot yourself in the foot. So I agree we're definitely better off with Netlinx keeping the gun locked up and out of our hands.
Joe
And wasn't that statement by Bill Gates 640K?
- Chip
Jeff
As Michael Keaton told Martin Mull in Mr. Mom - 220,221, whatever it takes.
Joe
Your arrays are static once compiled, but it's only a matter of editing the variable declarations and re-compiling to make any changes.