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

Dynamic Size Array

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



Joined: 16 Nov 2008
Posts: 22
Location: Sri Lanka

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

Dynamic Size Array
PostPosted: Mon Jan 05, 2009 10:45 am     Reply with quote

Hi all,

Due to the limited memory on micro controllers, it is better if i can used dynamic arrays which can grow at run time, rather than having fixed size arrays.

I need to create a dynamic array with 3 columns and n(n should be less than predefined maximum size) rows. In my project i need to create a routing table with fixed maximum size. But the array should grows dynamically at run time.

So it is very helpful me, if someone can guide help me by posting a sample code segment. Or else please be kind enough to post some links related to my task.
Finally as a solution can i use link list techniques here to create a dynamic array.

Thanks in advance.

Sharma
_________________
Kushan Sharma
[email protected]
Ttelmah
Guest







PostPosted: Mon Jan 05, 2009 3:03 pm     Reply with quote

CCS, in one sense uses memory 'dynamically' automatically. It re-uses memory areas for one subroutine, in others that are not called inside the other.
Now, there is no real fully dynamic process you can use, instead you will have to allocate an area equivalent to your maximum requirement, and then use blocks inside this 'dynamically'. This is done using the standard C functions malloc and free. Basically you don't allocate arrays, but just allocate memory blocks large enough for what you want, and then use pointer operations to access the data. However there are severe caveats to this. Cleaning up, becomes _your_ responsibility, and it can become very easy to point a pointer somewhere you shouldn't. It is also very easy for memory to become fragmented, so you will need to design strategies to cope with this. By allocating the area yourself, you _will_ reduce the compiler's own ability to apply it's own memory management.
So, whether it is worthwhile or not will depend massively on your own particular needs.
The CCS example files show malloc being used.

Best Wishes
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