Home/Support/Support Forum/Dynamic array size RCM6700
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

Dynamic array size RCM6700

0 votes
Greatings all,

Is it possible to define a global variable size after the "main" function was started.

Its out of any loop, so it would be executed only one time on bootup.
try_time = 11;
snmp_trap(lan.IP_SNMP, SNMP_TRAPDEST , 64, 0, trapindices);
if(lan.Habilitar_SNMP2 !=0)
snmp_trap(lan.IP_SNMP2, SNMP_TRAPDEST, 64, 0, trapindices);


"variable definition" int numbers[num_times];

while (1)

Thank you,

asked Feb 26, 2018 in Rabbit Software by Demolick New to the Community (28 points)

Please log in or register to answer this question.

1 Answer

+1 vote
Best answer
Dynamic C includes a malloc() function that allocates "far" memory, so you could do something like:
int far *numbers; numbers = malloc(num_times * sizeof(int));

And then later in your code:
foo = numbers[27];
answered Feb 28, 2018 by TomCollins Veteran of the Digi Community (2,089 points)
selected Mar 1, 2018 by Demolick
The problem I'm having is with the ammount of memory left. As I understand, "malloc" reservers the memory beforehand, an I right?
malloc() acquires memory at run-time.  But you could just have a compile-time option for maximum number of array entries.  An "int" is only 2 bytes -- how many were you thinking of storing?  Just have "far int numbers[MAX_NUMBERS];" as a global and then decide what to do in your program if num_times somehow ends up larger than MAX_NUMBERS.  With embedded, it's usually easier to allocate all of your memory at compile-time so you know that you have enough room for everything.
Its not too big.
The problem is with total amount of space, however, it's a separete issue xD
I'd use the size of the array for some function limitations.
You've solved the doubt i had, thank you.