ROBOTC.net forums
http://www.robotc.net/forums/

Memory handling in RobotC
http://www.robotc.net/forums/viewtopic.php?f=1&t=4883
Page 1 of 1

Author:  brianchen11 [ Mon Sep 24, 2012 3:59 am ]
Post subject:  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.

Author:  MikeJMcFarlane [ Mon Sep 24, 2012 6:33 am ]
Post subject:  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

Author:  MHTS [ Mon Sep 24, 2012 12:41 pm ]
Post subject:  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.

Author:  tfriez [ Mon Sep 24, 2012 1:22 pm ]
Post subject:  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.

Author:  MHTS [ Mon Sep 24, 2012 1:41 pm ]
Post subject:  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 :)

Author:  brianchen11 [ Tue Sep 25, 2012 3:02 am ]
Post subject:  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.

Author:  mightor [ Tue Sep 25, 2012 3:23 am ]
Post subject:  Re: Memory handling in RobotC

120x120 bytes is very close to the 15k limit :)

- Xander

Author:  brianchen11 [ Tue Sep 25, 2012 6:38 pm ]
Post subject:  Re: Memory handling in RobotC

So 15k is the limit.
I never knew that but I did know it was small.

Author:  skatefriday [ Mon Feb 17, 2014 4:04 pm ]
Post subject:  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.

Author:  mightor [ Tue Feb 18, 2014 2:57 am ]
Post subject:  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

Author:  MHTS [ Tue Feb 18, 2014 4:19 am ]
Post subject:  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.

Author:  skatefriday [ Tue Feb 18, 2014 3:27 pm ]
Post subject:  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.

Author:  mightor [ Tue Feb 18, 2014 3:32 pm ]
Post subject:  Re: Memory handling in RobotC

Haha, sounds like a fair trade :)

= Xander

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/