Page 1 of 2

NXC command FreeMemory()

Posted: 05 Oct 2010, 00:08
by mattallen37
Does the FreeMemory() command return the number of bytes in RAM that are available? If so, is there a command that returns the amount of available flash? Thanks.

Re: NXC command FreeMemory()

Posted: 05 Oct 2010, 00:59
by nxtreme
Now, I'm not a NXC programmer but even I know how to use the search function over at the NXC help page (http://bricxcc.sourceforge.net/nbc/nxcd ... index.html). Type in 'freememory' and you'll get a great explanation and a sample program. Try it! When I tried to search for 'flash' it didn't come up with anything. Someone with more NXC experience will be able to explain better.

Re: NXC command FreeMemory()

Posted: 05 Oct 2010, 01:13
by mattallen37
As I said, I know you aren't, but (as I also already said), the search doesn't work for me.

Edit: I figured it out, but it looks like it is for flash, not RAM.

I would like to know how to read both, so if you know how, please tell me.

Re: NXC command FreeMemory()

Posted: 05 Oct 2010, 01:48
by afanofosc
There isn't any way provided by the standard NXT or enhanced NBC/NXC firmwares to find out how much RAM is available.

I would encourage you to show what you have figured out on your own by using the help and guide when you post a question. It will help other newcomers to NXC (and other programming languages) learn "how to fish" as it were. :-)

John Hansen

Re: NXC command FreeMemory()

Posted: 05 Oct 2010, 02:00
by mattallen37
So basically, a program can crash because the NXT was overloaded, but there is no way to detect that ahead of time?

So, just to confirm, the command is for reading the remaining flash, and it is the number of bytes remaining?

I'll try to remember to post what I already learned and tried when I ask questions in the future. Thanks for answering.

Re: NXC command FreeMemory()

Posted: 05 Oct 2010, 07:59
by mightor
First search result in Google:

Image

- Xander

Re: NXC command FreeMemory()

Posted: 05 Oct 2010, 16:54
by afanofosc
mattallen37 wrote:So basically, a program can crash because the NXT was overloaded, but there is no way to detect that ahead of time?

So, just to confirm, the command is for reading the remaining flash, and it is the number of bytes remaining?
I will look into adding the ability to check how much RAM is available in a running program to the enhanced NBC/NXC firmware. I'll also look into exposing the "compact memory" option that the firmware has so that it can be executed manually by a running program. It is supposed to do that automatically whenever you try to allocate more memory for an array but it might be worth having the ability to do it on demand.

As the help files state, FreeMemory returns the number of bytes of flash memory that are free.

John Hansen

Re: NXC command FreeMemory()

Posted: 05 Oct 2010, 17:51
by mattallen37
So, why does it return numbers no more than 106.644 kB? Does the firmware REALLY take up over half the flash? The AT91SAM7S256 has 256 kB of flash, so if I am only seeing less than half that, what is using up all the rest? I know that I have the program to read that number on it, but that only exists of the following.

Code: Select all

task main()
{
  NumOut(0, LCD_LINE1,FreeMemory()); //106644 bytes is the MAX with this program
  while(true);
}
This small program can't take up much space.

Edit: the compiled program is 340 bytes, so REALLY small.

Re: NXC command FreeMemory()

Posted: 05 Oct 2010, 18:55
by mightor
The firmware is indeed just under 128K, the max limit of the IAR workbench for ARM (free, size limited version).

- Xander

Re: NXC command FreeMemory()

Posted: 05 Oct 2010, 19:02
by mattallen37
Then where did the rest go?

256000 (total)
- 128000 (FW)
- 000340 (program)
- 106644 (remaining)
=022016 bytes that are... lost? 22kB is a significant amount when talking about the NXT, and the programs.