View previous topic :: View next topic |
Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
Run time Global Struct declaration? |
Posted: Mon Apr 16, 2018 4:34 pm |
|
|
Hi, I have working code where 1 device runs multiple "instances" of it self, each "Virtual Device/Instance" contained in a Struct.
The amount of "Instances" depends on the amount of slave modules on a bus.
At initialization my code checks the BUS for slaves and figures its got 3 slaves and I would like to then create an array of structs of size 3...
I currently do this by setting the amount of instances on the declaration at compile time, since i know how many devices are on it. But i would like to automate this.
The Struct array is currently a global variable... it needs to be accessed by multiple drivers, sensors, etc...
Thanks.
G _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Apr 17, 2018 11:01 am |
|
|
You can use malloc to declare an area of RAM at runtime.
You declare a pointer to the structure, and then load this with the RAM address from malloc.
This can then be used in your functions (remember though you will access using -> rather than .).
Do a web search on C dynamic memory allocation. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Tue Apr 17, 2018 7:26 pm |
|
|
Thanks for the pointers!
... yes that was a pun. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Wed Apr 18, 2018 12:17 am |
|
|
This might be off topic, but why would you want to create dynamic memory?
Had the same decision to make and decided to rather 'allocate' the memory at compile time for max number of slaves and then poll to check how many are available.
To me it is not about saving RAM as you would need to ensure that the amount of RAM will be available for allocation at run-time anyway.
I also had to send the values to serial and it helped being in a 'continuous' block and just start at the beginning of the element.
Regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Wed Apr 18, 2018 12:32 am |
|
|
Alan makes a very good point. If the system has to be able to handle 'N' channels, there has to be RAM to hold the data for all N channels. The only time dynamic allocation is useful, is when you have perhaps two different sections of the code, both of which have varying allocations, and the RAM can be re-used between these sections using dynamic allocation. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Apr 18, 2018 6:19 am |
|
|
Hi Alan & Mr.T...
Your suggestion to simply declare the max number of slaves seems to be the most practical approach. It seemed at the time of the OP to be a good idea.
I wasn't trying to save Ram... i wanted to have an elegant automatic way of controlling all the loops which need to run x amount of times depending on the amount of slaves. And also have the size of all arrays, etc, get the correct size automatically.
But in retrospect there is no need as you've pointed out.
I will take this route.
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
|