|
|
View previous topic :: View next topic |
Author |
Message |
mlkushan
Joined: 16 Nov 2008 Posts: 22 Location: Sri Lanka
|
Dynamic Size Array |
Posted: Mon Jan 05, 2009 10:45 am |
|
|
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
|
|
Posted: Mon Jan 05, 2009 3:03 pm |
|
|
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 |
|
|
|
|
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
|