CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Run time Global Struct declaration?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

Run time Global Struct declaration?
PostPosted: Mon Apr 16, 2018 4:34 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Apr 17, 2018 11:01 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Apr 17, 2018 7:26 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 18, 2018 12:17 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 18, 2018 12:32 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 18, 2018 6:19 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group