Discussion Forum

Array length changeable during Run - Time

Next Thread | Thread List | Previous Thread Start a Thread | Settings

DetailsMessage
Read-Only
Author
Andreas Pankasz
Posted
26-May-2004 11:51 GMT
Toolset
C51
New! Array length changeable during Run - Time
Hi everyone

Does someone know how to change the length
of on array during the run time execution?
I mean how can I request memory to use
it for a array? Is there somthing like
malloc()?

Thanks!
Read-Only
Author
Mike Kleshov
Posted
26-May-2004 12:29 GMT
Toolset
C51
New! RE: Array length changeable during Run - Time
You will probably be better off allocating maximum array size at compile time. Dynamic memory allocation in a 8051-based design is generally a bad idea. Just do a search in this forum with keywords like "malloc" or "dynamic" for further discussion.

- mike
Read-Only
Author
Andrew Neil
Posted
26-May-2004 13:32 GMT
Toolset
C51
New! RE: Array length changeable during Run - Time
"You will probably be better off allocating maximum array size at compile time."

Yes.

Think about it: even if you do use malloc (or similar) you will have to define the size of the "pool" from which this allocates its memory - so you will need to know the maximum array size at compile time anyway.

So why waste your time, and the compiler's, and some CPU run time with dynamic allocation?

Just Don't Do It!
Read-Only
Author
Stefan Duncanson
Posted
26-May-2004 13:42 GMT
Toolset
C51
New! RE: Array length changeable during Run - Time
"Is there somthing like malloc()?"

To answer your question directly there is indeed something like malloc(): malloc(), in fact.

However, as others have pointed out using malloc() is a bad idea in most 8051 situations. If you need dynamic allocation because your total memory requirements exceed your available memory then declare arrays as local variables within functions and let the overlay manager perform its magic.
Read-Only
Author
Jon Ward
Posted
26-May-2004 17:01 GMT
Toolset
C51
New! RE: Array length changeable during Run - Time
The small model benchmarks for malloc and free may be found at: http://www.keil.com/benchmks/c51_alloc.asp.

The benchmark results are from the Knuth memory allocation test as described in Fundamental Algorithms, Section 2.5 Dynamic Storage Allocation. In other words, the results in the benchmark are likely to be close to what you see in your implementation.

Note that the free() function in the Keil libraries merges adjacent free blocks. This is NOT true of most malloc/free implementations.

Jon
Read-Only
Author
Dan Henry
Posted
26-May-2004 18:08 GMT
Toolset
C51
New! RE: Array length changeable during Run - Time
"To answer your question directly there is indeed something like malloc(): malloc(), in fact."

And then to answer your question even more directly about runtime changes, after malloc(), you would use realloc() to change size.

http://www.keil.com/support/man/docs/c51/c51_realloc.htm

Next Thread | Thread List | Previous Thread Start a Thread | Settings