View unanswered posts | View active topics It is currently Thu Jul 31, 2014 11:37 am






Reply to topic  [ 13 posts ] 
Memory handling in RobotC 
Author Message
Rookie

Joined: Tue Jul 03, 2012 5:16 am
Posts: 43
Post Memory handling in RobotC
Hi,

I'm just wondering whether you could delete variables in RobotC for memory allocation.
I am not very experienced at this and I would like to know because I have a very large array that may or may not cause the NXT to produce and error.


Mon Sep 24, 2012 3:59 am
Profile
Rookie

Joined: Thu Sep 13, 2012 9:43 am
Posts: 47
Post Re: Memory handling in RobotC
brianchen11 wrote:
Hi,

I'm just wondering whether you could delete variables in RobotC for memory allocation.
I am not very experienced at this and I would like to know because I have a very large array that may or may not cause the NXT to produce and error.


This might help viewtopic.php?f=1&t=500


Mon Sep 24, 2012 6:33 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Memory handling in RobotC
Talking about memory allocation, does RobotC 3.51 support dynamic memory allocation (i.e. the new and delete operator and heap)? I don't have RobotC in front of me, so I can't do tests to see if it supports it or not, nor can I look through the help topics to find the answer.


Mon Sep 24, 2012 12:41 pm
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 601
Post Re: Memory handling in RobotC
MHTS wrote:
Talking about memory allocation, does RobotC 3.51 support dynamic memory allocation (i.e. the new and delete operator and heap)? I don't have RobotC in front of me, so I can't do tests to see if it supports it or not, nor can I look through the help topics to find the answer.


No - ROBOTC automatically handles the memory allocation of variables in your program. There are no malloc/free keywords in ROBOTC.

You have a 15K of Global Variables and around 5K of stack "dynamic" space that is shared with all declared tasks. Longs/Floats are 4 bytes, shorts are 2 bytes and chars/bytes are 1 - Strings are 20 bytes.

If you're looking at something incredibly complex and this won't be enough memory, you should look at using the IAR toolset with the NXT. Remember you're not programming on a PC with gigabytes of space and memory, but rather a small embedded processor - you should set your expectations as such.

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Mon Sep 24, 2012 1:22 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Memory handling in RobotC
It is not really about anything complex or allocations that require GB of memory. At times when designing a module, there is a trade off of allocating a fixed size array and hope that you will never exceed the limit. Determining the size of the array at compile time is sometimes tricky. If you make it too big, you are wasting precious memory. If you make it too small, you may run out at run-time. Dynamically allocating memory doesn't always mean huge amount of memory, it is just deferring the allocation until run-time so that the exact amount of memory required is known. So far for our application, we haven't found a scenario where we absolutely require dynamic memory allocation (yet). But since 3.50 starts to support pointers, it is a logical question to ask whether it also supports dynamic memory allocation. So don't worry. I am not asking for that feature, just curious if it has the support :)


Mon Sep 24, 2012 1:41 pm
Profile
Rookie

Joined: Tue Jul 03, 2012 5:16 am
Posts: 43
Post Re: Memory handling in RobotC
I've used the array and it works for me:
byte coordinates[91][61];
This is a byte array as I don't need more than that.

Once I do get up to around [120][120] I get the error "Out of static memory for variable allocation."
but I don't need that much.


Tue Sep 25, 2012 3:02 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3165
Location: Rotterdam, The Netherlands
Post Re: Memory handling in RobotC
120x120 bytes is very close to the 15k limit :)

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Sep 25, 2012 3:23 am
Profile WWW
Rookie

Joined: Tue Jul 03, 2012 5:16 am
Posts: 43
Post Re: Memory handling in RobotC
So 15k is the limit.
I never knew that but I did know it was small.


Tue Sep 25, 2012 6:38 pm
Profile
Rookie

Joined: Tue Dec 11, 2012 9:19 pm
Posts: 21
Post Re: Memory handling in RobotC
MHTS wrote:
It is not really about anything complex or allocations that require GB of memory. At times when designing a module, there is a trade off of allocating a fixed size array and hope that you will never exceed the limit. Determining the size of the array at compile time is sometimes tricky. If you make it too big, you are wasting precious memory. If you make it too small, you may run out at run-time. Dynamically allocating memory doesn't always mean huge amount of memory, it is just deferring the allocation until run-time so that the exact amount of memory required is known. So far for our application, we haven't found a scenario where we absolutely require dynamic memory allocation (yet). But since 3.50 starts to support pointers, it is a logical question to ask whether it also supports dynamic memory allocation. So don't worry. I am not asking for that feature, just curious if it has the support :)


Wondering if 4.0 will add support for dynamically allocated memory.

As MHTS notes, it's not all about allocating large blocks of memory.
I'd like to be able to create lists, short ones, describing segments of a
dead reckoning path, but lists none-the-less.

Arrays will work here but it's cleaner to design an api such as

add_segment(path_t *path, float distance, int turn, int speed);

Call that repeatedly in init robot and then execute the path by
iterating over the list.

This could be done with arrays, but IMHO not as cleanly. Although
if Robomatter fixes the statically allocated array of structs bug that
would help.

e.g. This initializes to all zero.

typedef struct point_ {
int x;
int y;
point_t;

point_t p[] = { {1, 2}, {1, 3}, {1, 4} };

It effectively ignores the static initializer.

Really looking forward to 4.0 for NXT.


Mon Feb 17, 2014 4:04 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3165
Location: Rotterdam, The Netherlands
Post Re: Memory handling in RobotC
I've written a dynamic memory allocator/deallocator based on the AVR libC malloc code and it works quite well. There are some things I need to finish up to make the test program work properly. It's written in ROBOTC, so should work on all platforms. You assign a pool of memory to it and after that you can do whatever you like with it.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Feb 18, 2014 2:57 am
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Memory handling in RobotC
Yes, it would be nice to have RobotC supporting dynamic memory allocation but as Xander said, it is not hugely difficult to write your own memory allocator. Just hog all 15K memory and suballocate it yourself.


Tue Feb 18, 2014 4:19 am
Profile
Rookie

Joined: Tue Dec 11, 2012 9:19 pm
Posts: 21
Post Re: Memory handling in RobotC
I'll trade you the protoboard as an i2c master, which I have planned
to work on after the FTC season, for the memory allocation library.


Tue Feb 18, 2014 3:27 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3165
Location: Rotterdam, The Netherlands
Post Re: Memory handling in RobotC
Haha, sounds like a fair trade :)

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Feb 18, 2014 3:32 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 13 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.