[SOLVED]Silly question, minimum free Ram on DUE? / Max size of char array

Hello everyone,

I appreciate this is one of those 'how long is a piece of string' type questions, but there is a genuine requirement behind asking it.

I am trying to write an auto-adjustable buffer depending on the size of item needing to be buffered, my algorithm works fine, maybe not the prettiest way of doing it but I am not asking for style points.

	int ram = FreeRam(), tempa=0, tempb=((pow(2, tempa))*sx);
	while((ram-tempb)>=100)
	{
	tempa++;
	tempb=((pow(2, tempa))*sx);	
	}
	tempa--;
	tempb=((pow(2, tempa))*sx);
	char buffer[tempb];

So my theory is....... if a theoretical buffer would leave less than 100 bytes free, drop back a power of 2, but the aim is to have the biggest buffer possible while leaving more than 100 bytes free.

The idea works, the allocation works, it appears to work, BUT clearly I am missing something because the sketch crashes.

Now, the value of sx on my first call of the routine is 240, so we iterate around the loop and drop out of it with a value of tempa=8, tempb= 61440;

SRAM on an Arduino DUE = 96KB, so why can I not have a buffer of 61440 without causing problems?

A buffer of 30720 is fine however.

Could somebody point me in a direction where I can find these limits in print rather than trial and error please?

Regards,

Graham

What is the freeRam() function you are calling?

pepe:
Hi

The MCU of the Due has got 96 Kbytes of memory, but it is organized in two distinct banks of 64 Kbytes and 32 Kbytes.

I don't think that the fact that the size of the SRAM banks is the issue here, as the docs state:

The available SRAM is 96 KB in two contiguous bank of 64 KB and 32 KB. All the available memory (Flash, RAM and ROM) can be accessed directly as a flat addressing space.

I don't have access to one of my Dues right now, but it might simply be that the libraries you are including in your sketch are already taking up a significant portion of the available SRAM...

Will test this when I get back home...

Ralf

pepe:
This could be due to (for instance) :

  • a call to a function that allocates memory, but not to another one to free it
  • a corruption of the memory management system
  • a fragmentation of the free memory
    ...

Option 1 and 3 are IMHO the most likely source of the problem here...

Ralf

Maybe try to allocate a static buffer first and see if it works and what is FreeRam going to tell you.

char buffer[61440];

If that works, then probably your dynamic buffer allocation causes a memory leak or the memory is fragmented by the time you try to allocate it (as others have mentioned).