Dynamic array size RCM6700

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;
ntp_ok=1;
ntp_fail=0;
}
}
if(try_time==10)
{
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);
tcp_tick(NULL);
ntp_fail=1;
}
for(i=0;i<16;i++)
{
temp[i]=0;
temp_on[i]=0;
}

tcp_tick(NULL);
first_time=0;

“variable definition” int numbers[num_times];

//…
while (1)
{
.
.
.

Thank you,

Konstantin.

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];

1 Like

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.