View unanswered posts | View active topics It is currently Sun Sep 20, 2020 11:55 pm

Reply to topic  [ 5 posts ] 
problem to declare array, even of small size 
Author Message

Joined: Sun Sep 28, 2014 7:18 am
Posts: 5
Post problem to declare array, even of small size

I'm trying to declare array - the size is very modest indeed ;)

task main()
   const int N = 255;
   int myArray[N];
   int i1, i2, i3;
        ... <skipped>

In case of N = 255 I got
Code generation not performed. Errors during compilation.
**Severe*:Out of memory for dynamic variable allocation.

For N = 254 the message is
**Severe*:Out of memory space for nested { .. } blocks or procedures (1028/1024):Nested:8/8Pad:0Var/Temp:1020/0
**Severe*:Out of memory space for nested { .. } blocks or procedures

Also, for array of ints even with N = 253 I can't declare any variables after that
(got Out of memory for dynamic variable allocation. for i1,i2,i3 in text above).

Using byte as type for array I can set N = 300, but not 400 :(
Using bool as type for array I can declare, say, [100][2], but not [60][60], as expected.

Could you help?

Windows XP SP3, the latest RobotC for NXT 4.26, NXT 2 brick with latest firmware
(but the situation is the same for compiling after clean installation and without any brick connected).

Regards, SUldanov

Mon Sep 29, 2014 5:00 am

Joined: Sat Apr 12, 2008 11:09 am
Posts: 60
Location: holland
Post Re: problem to declare array, even of small size

I get the same. The problem only appears when you declare the array locally (within a task or function) declaring it global works just fine

const int L=255
int myArray[L];
int i1,i2,i3;

void something()
   const int M = 255;
   int myArray1[M];
   int i4,i5,i6;

task somethingElse()
   const int N = 255;
   int myArray2[N];
   int i7,i8,i9;

task main()
   const int O = 255;
   int myArray3[O];
   int i10,i11,i12;


Tue Sep 30, 2014 10:11 am

Joined: Sun Sep 28, 2014 7:18 am
Posts: 5
Post Re: problem to declare array, even of small size
Thanks, it's understandable now.

Variables that are defined inside a function—not declared static—are named auto variables by the C standard.
A small number of these variables are placed in processor registers; the rest are placed on the stack.
From a semantic point of view, this is equivalent. The main differences are that accessing registers is faster, and that less
memory is required compared to when variables are located on the stack.
Auto variables live as long as the function executes; when the function returns, the memory allocated on the stack is released.

from ARM® IAR C/C++ Compiler Reference Guide.

As in RobotC for NXT the stack is relatively small (you can see in posts in this forum notifications about count of 10 or sth like that of recursive calls and requests to increase it) - there is just not enough memory.
And it's not a problem with arrays only:
task main()
   int i001, i002, i003, i004, i005, i006, i007, i008, i009, i010;
   int i011, i012, i013, i014, i015, i016, i017, i018, i019, i020;
   int i021, i022, i023, i024, i025, i026, i027, i028, i029, i030;
   int i031, i032, i033, i034, i035, i036, i037, i038, i039, i040;
   int i041, i042, i043, i044, i045, i046, i047, i048, i049, i050;
   int i051, i052, i053, i054, i055, i056, i057, i058, i059, i060;
   int i061, i062, i063, i064, i065, i066, i067, i068, i069, i070;
   int i071, i072, i073, i074, i075, i076, i077, i078, i079, i080;
   int i081, i082, i083, i084, i085, i086, i087, i088, i089, i090;
   int i091, i092, i093, i094, i095, i096, i097, i098, i099, i100;
   int i101, i102, i103, i104, i105, i106, i107, i108, i109, i110;
   int i111, i112, i113, i114, i115, i116, i117, i118, i119, i120;
   int i121, i122, i123, i124, i125, i126, i127, i128, i129, i130;
   int i131, i132, i133, i134, i135, i136, i137, i138, i139, i140;
   int i141, i142, i143, i144, i145, i146, i147, i148, i149, i150;
   int i151, i152, i153, i154, i155, i156, i157, i158, i159, i160;
   int i161, i162, i163, i164, i165, i166, i167, i168, i169, i170;
   int i171, i172, i173, i174, i175, i176, i177, i178, i179, i180;
   int i181, i182, i183, i184, i185, i186, i187, i188, i189, i190;
   int i191, i192, i193, i194, i195, i196, i197, i198, i199, i200;
   int i201, i202, i203, i204, i205, i206, i207, i208, i209, i210;
   int i211, i212, i213, i214, i215, i216, i217, i218, i219, i220;
   int i221, i222, i223, i224, i225, i226, i227, i228, i229, i230;
   int i231, i232, i233, i234, i235, i236, i237, i238, i239, i240;
   int i241, i242, i243, i244, i245, i246, i247, i248, i249, i250;
   int i251, i252, i253, i254, i255, i256, i257, i258, i259, i260;
   int i261, i262, i263, i264, i265, i266, i267, i268, i269, i270;


For the code above we'll get "Out of memory for dynamic variable allocation" for the last 2 rows.

Am I right?

Tue Sep 30, 2014 10:37 am
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 620
Post Re: problem to declare array, even of small size
The NXT has about 20KB in space for variables. This space is spread across the "stack" or local variables, and also "global" or global variables.

By default, the NXT's memory is split up as follows:
5120 Bytes to "Stacks"
15000 Bytes to "Global Variables"

The amount of space available the Stack is for ALL tasks, with a maximum of each stack having 1024 bytes and a minimum of 256 bytes.

Since in ROBOTC 4.X, "ints" are 32-bit variables, you will only be able to declare approximately 256 ints inside of each "task", hence your issue of your last two rows of your program running out of space. This is approximate as there may be a few variables tied up for the system.

You can try to use the following to use less memory:
long - 4 bytes
int - 4 bytes
float - 4 bytes
short - 2 bytes
byte - 1 byte
char - 1 byte
bool - 1/2 byte (A nibble?)

You can check your memory usage by enabling the compiler statistics:

Timothy Friez
ROBOTC Developer - SW Engineer

Thu Oct 02, 2014 11:57 am

Joined: Sun Sep 28, 2014 7:18 am
Posts: 5
Post Re: problem to declare array, even of small size
Timothy, thank you.

It's interesting that changing in Preferences from "Standard - Global and Stack Variables" to "Force 'Classic' - All Memory Allocation in Global" doesn't eliminate the error.

Thu Oct 02, 2014 2:48 pm
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests

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 post attachments in this forum

Search for:
Jump to:  

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.