DUE memory

Hi !

Arduino DUE and some strange things… bundaries bug ?

In my code I use

#include <MemoryFree.h>
#include "FreeStack.h"

And actually I get :
free=69647 FreeStack: 69656

but If I add 1 line in the global (before setup()) :

  char* aa;

I get that (more?!) :
free=69651 FreeStack: 69660

and the program crashes when “returning” from a function.

I just know that there is 2 memory banks (32 and 64K) in the ATSAM3X8E and maybe the allocation fails near the limit ?

Thx for your interest

I doubt anybody can say anything about the crash without seeing your code.

I have this code to test for the available SRAM of a DUE (Note: there are 96 K of contiguous SRAM plus 4K not contiguous):

#include <malloc.h>

const char errorMsg[] = {"Invalid code!"};
extern char _end;
extern "C" char *sbrk(int i);
char *ramstart=(char *)0x20070000; 
char *ramend=(char *)0x20088000;

char* Text[] = {
  /* [0]  */  "Start...",
  /* [1]  */  "Sensor1 Count  0",
  /* [2]  */  "Sensor1 Count  1",
  /* [3]  */  "Sensor1 Count  2",
  /* [4]  */  "Sensor1 Count  3",
  /* [5]  */  "Sensor1 Count  4",
  /* [6]  */  "Sensor1 Count  5",
  /* [7]  */  "Sensor1 Count  6",
  /* [8]  */  "Sensor1 Count  7",
  /* [9]  */  "Sensor1 Count  8",
  /* [10] */  "Sensor1 Count  9",
  /* [11] */  "Sensor1 Count 10",
  /* [12] */  "Sensor1 Count 11",
  /* [13] */  "Sensor1 Count 12",
  /* [14] */  "Sensor1 Count 13",
  /* [15] */  "Sensor1 Count 14",
  /* [16] */  "Sensor1 Count 15",
  /* [17] */  "Sensor1 Count 16",
  /* [18] */  "Sensor1 Count 17",
  /* [19] */  "Sensor1 Count 18",
  /* [20] */  "Sensor1 Count 19",
  /* [21] */  "Sensor1 Count 20",
  /* [22] */  "Sensor2 Count  0",
  /* [23] */  "Sensor2 Count  1",
  /* [24] */  "Sensor2 Count  2",
  /* [25] */  "Sensor2 Count  3",
  /* [26] */  "Sensor2 Count  4",
  /* [27] */  "Sensor2 Count  5",
  /* [28] */  "Sensor2 Count  6",
  /* [29] */  "Sensor2 Count  7",
  /* [30] */  "Sensor2 Count  8",
  /* [31] */  "Sensor2 Count  9",
  /* [32] */  "Sensor2 Count 10",
  /* [33] */  "Sensor2 Count 11",
  /* [34] */  "Sensor2 Count 12",
  /* [35] */  "Sensor2 Count 13",
  /* [36] */  "Sensor2 Count 14",
  /* [37] */  "Sensor2 Count 15",
  /* [38] */  "Sensor2 Count 16",
  /* [39] */  "Sensor2 Count 17",
  /* [40] */  "Sensor2 Count 18",
  /* [41] */  "Sensor2 Count 19",
  /* [42] */  "Sensor2 Count 20",
  /* [43] */  "Direction --> to box",
  /* [44] */  "Direction <-- to trainer",
  /* [45] */  "Nose to Nose",
  /* [46] */  "Fault",
  /* [47] */  "Race Completed",
  /* [48] */  "Error 1",
  /* [49] */  "Error 2",
  /* [50] */  "Error Unknown",
};

void setup()
{
   Serial.begin(250000);
   while(!Serial);
   
   char *heapend=sbrk(0);
   register char * stack_ptr asm ("sp");
   struct mallinfo mi=mallinfo();
 
   printf("\nDynamic ram used: %d\n",mi.uordblks);
   printf("Program static ram used %d\n",&_end - ramstart); 
   printf("Stack ram used %d\n",ramend - stack_ptr); 
   printf("Estimated Free RAM: %d\n\n",stack_ptr - heapend + mi.fordblks);
   
   int* ptr_i;
   int i = 0xaa;
   ptr_i = &i;
   
   printf("Size of int: %d, Size of uint: %d\n", sizeof(int), sizeof(unsigned int));
   printf("Size of char: %d, Size of uchar: %d\n", sizeof(char), sizeof(unsigned char));
   printf("Size of short: %d, Size of ushort: %d\n", sizeof(short), sizeof(unsigned short));
   printf("Size of float: %d, Size of double: %d\n", sizeof(float), sizeof(double));
   printf("Size of long: %d, Size of longlong: %d\n", sizeof(long), sizeof(long long));
   printf("Size of pointer (ptr_i): %d\n", sizeof(ptr_i));
   printf("Addr of pointer (ptr_i): 0x%x\n", ptr_i);
   printf("Value of i: 0x%x\n", *ptr_i);


Serial.println(Text[44]);
Serial.println(Text[0]);

}

void loop() {
  // put your main code here, to run repeatedly: 
  
}

sterretje:
I doubt anybody can say anything about the crash without seeing your code.

Sorry, but more than 10K lines will not be very interesting I gess... The only change is 1 line to make it working or not.

I tried to change the added line with that

Code: [Select]

char a[128];

now I get :
free=69647 FreeStack: 69656

And it's running well... many effects for unused fields....

ard_newbie:
I have this code to test for the available SRAM of a DUE (Note: there are 96 K of contiguous SRAM plus 4K not contiguous):

#include <malloc.h>

const char errorMsg = {“Invalid code!”};
extern char _end;
extern “C” char *sbrk(int i);
char *ramstart=(char *)0x20070000;
char *ramend=(char *)0x20088000;

char* Text = {
 /* [0]  /  “Start…”,
 /
[1]  /  “Sensor1 Count  0”,
 /
[2]  /  “Sensor1 Count  1”,
 /
[3]  /  “Sensor1 Count  2”,
 /
[4]  /  “Sensor1 Count  3”,
 /
[5]  /  “Sensor1 Count  4”,
 /
[6]  /  “Sensor1 Count  5”,
 /
[7]  /  “Sensor1 Count  6”,
 /
[8]  /  “Sensor1 Count  7”,
 /
[9]  /  “Sensor1 Count  8”,
 /
[10] /  “Sensor1 Count  9”,
 /
[11] /  “Sensor1 Count 10”,
 /
[12] /  “Sensor1 Count 11”,
 /
[13] /  “Sensor1 Count 12”,
 /
[14] /  “Sensor1 Count 13”,
 /
[15] /  “Sensor1 Count 14”,
 /
[16] /  “Sensor1 Count 15”,
 /
[17] /  “Sensor1 Count 16”,
 /
[18] /  “Sensor1 Count 17”,
 /
[19] /  “Sensor1 Count 18”,
 /
[20] /  “Sensor1 Count 19”,
 /
[21] /  “Sensor1 Count 20”,
 /
[22] /  “Sensor2 Count  0”,
 /
[23] /  “Sensor2 Count  1”,
 /
[24] /  “Sensor2 Count  2”,
 /
[25] /  “Sensor2 Count  3”,
 /
[26] /  “Sensor2 Count  4”,
 /
[27] /  “Sensor2 Count  5”,
 /
[28] /  “Sensor2 Count  6”,
 /
[29] /  “Sensor2 Count  7”,
 /
[30] /  “Sensor2 Count  8”,
 /
[31] /  “Sensor2 Count  9”,
 /
[32] /  “Sensor2 Count 10”,
 /
[33] /  “Sensor2 Count 11”,
 /
[34] /  “Sensor2 Count 12”,
 /
[35] /  “Sensor2 Count 13”,
 /
[36] /  “Sensor2 Count 14”,
 /
[37] /  “Sensor2 Count 15”,
 /
[38] /  “Sensor2 Count 16”,
 /
[39] /  “Sensor2 Count 17”,
 /
[40] /  “Sensor2 Count 18”,
 /
[41] /  “Sensor2 Count 19”,
 /
[42] /  “Sensor2 Count 20”,
 /
[43] /  “Direction → to box”,
 /
[44] /  “Direction ← to trainer”,
 /
[45] /  “Nose to Nose”,
 /
[46] /  “Fault”,
 /
[47] /  “Race Completed”,
 /
[48] /  “Error 1”,
 /
[49] /  “Error 2”,
 /
[50] */  “Error Unknown”,
};

void setup()
{
  Serial.begin(250000);
  while(!Serial);
 
  char *heapend=sbrk(0);
  register char * stack_ptr asm (“sp”);
  struct mallinfo mi=mallinfo();

printf("\nDynamic ram used: %d\n",mi.uordblks);
  printf(“Program static ram used %d\n”,&_end - ramstart);
  printf(“Stack ram used %d\n”,ramend - stack_ptr);
  printf(“Estimated Free RAM: %d\n\n”,stack_ptr - heapend + mi.fordblks);
 
  int* ptr_i;
  int i = 0xaa;
  ptr_i = &i;
 
  printf(“Size of int: %d, Size of uint: %d\n”, sizeof(int), sizeof(unsigned int));
  printf(“Size of char: %d, Size of uchar: %d\n”, sizeof(char), sizeof(unsigned char));
  printf(“Size of short: %d, Size of ushort: %d\n”, sizeof(short), sizeof(unsigned short));
  printf(“Size of float: %d, Size of double: %d\n”, sizeof(float), sizeof(double));
  printf(“Size of long: %d, Size of longlong: %d\n”, sizeof(long), sizeof(long long));
  printf(“Size of pointer (ptr_i): %d\n”, sizeof(ptr_i));
  printf(“Addr of pointer (ptr_i): 0x%x\n”, ptr_i);
  printf(“Value of i: 0x%x\n”, *ptr_i);

Serial.println(Text[44]);
Serial.println(Text[0]);

}

void loop() {
 // put your main code here, to run repeatedly:
 
}

Thx : I have this
Dynamic ram used: 0
Program static ram used 28800
Stack ram used 104
Estimated Free RAM: 69400
Size of int: 4, Size of uint: 4
Size of char: 1, Size of uchar: 1
Size of short: 2, Size of ushort: 2
Size of float: 4, Size of double: 8
Size of long: 4, Size of longlong: 8
Size of pointer (ptr_i): 4
Addr of pointer (ptr_i): 0x20087fa4
Value of i: 0xaa
Direction ← to trainer
Start…

and that
free=65423 FreeStack: 65432

And due crashes…

Weird…
I have this with the above code provided in reply #2:

Dynamic ram used: 0
Program static ram used 5076
Stack ram used 80
Estimated Free RAM: 93148

Size of int: 4, Size of uint: 4
Size of char: 1, Size of uchar: 1
Size of short: 2, Size of ushort: 2
Size of float: 4, Size of double: 8
Size of long: 4, Size of longlong: 8
Size of pointer (ptr_i): 4
Addr of pointer (ptr_i): 0x20087fb4
Value of i: 0xaa
Direction <-- to trainer
Start...

ard_newbie:
Weird…
I have this with the above code provided in reply #2:

Dynamic ram used: 0

Program static ram used 5076
Stack ram used 80
Estimated Free RAM: 93148

Size of int: 4, Size of uint: 4
Size of char: 1, Size of uchar: 1
Size of short: 2, Size of ushort: 2
Size of float: 4, Size of double: 8
Size of long: 4, Size of longlong: 8
Size of pointer (ptr_i): 4
Addr of pointer (ptr_i): 0x20087fb4
Value of i: 0xaa
Direction ← to trainer
Start…




I just have 10K lines after yours... ;)

Not an hardware issue... after changing with a new unpacked board, same pb...

IMO stack size can't be more than a few KB, it's not usual to have freestack = 65 KB . Somewhere in your code, the stacksize is bugged.

The heap grows upwards while the stack grows backwards resulting in a crash.

Try this out with a minimal code:

void* stackPtr = alloca(4); // This returns a pointer to the current bottom of the stack
  printf("StackPtr %d\n", stackPtr);

ard_newbie:
IMO stack size can't be more than a few KB, it's not usual to have freestack = 65 KB . Somewhere in your code, the stacksize is bugged.

Nonsense. The stack exists at the top of the heap, the heap at the bottom. EITHER can expand to occupy a large portion of the heap with no problem UNTIL either expands enough that the two boundarieshit or cross each other. You can easily prove this by creating a very large (more than "a new KB") local variable, like a large array, within a function. As long as there is enough free RAM available in a contiguous block, it will work just fine, even if that array is tens of KB.

Just after Serial.begin I get that :
+StackPtr 537427888

and just before the crash
37427632
note that "+StackPtr " is not printed so some left digits possibly missing

I remove the "char* " line and I get :
+StackPtr 537427888

and at the same place before the crash
537427632
"+StackPtr " not here system works.

So absolutely no change (but working)

I replaced printf by

Serial.print("StackPtr ");Serial.println(stackPtr);

and printing is ok.

The value of the SP has no real meaning for me... I don't know the address where the ram begins/ends and where the fields are stored.

Absolutely EVERYTHING you need to know about memory is contained in the memory descriptor block that MemoryFree uses to display its values. It contains the start and end addresses of RAM, the stack and heap locations, etc. You can manually "walk" the memory allocation list to see the addresses and sizes of every single allocated and free block of memory.

Adding or removing a single line of code and the result is a crash in my opinion indicates a problem in the code. It can be e.g. writing outside the boundaries of an array or heavy String (capital S) manipulation/concatenation

Are you doing anything with the variable declared in that one line of code? Does not seem like the compiler would do anything different if it is unused.

sterretje:
Adding or removing a single line of code and the result is a crash in my opinion indicates a problem in the code. It can be e.g. writing outside the boundaries of an array or heavy String (capital S) manipulation/concatenation

No String, unused field, and absolutely stable place to crash (that's a server with 5 to 20 (in and out) random connexions tcp, udp, ntp every minute) managing many files on a sd card. This software is running for years with improvments from time to time. I made some cleaning in the variables (removing unused ones) and I had to remove at least the number I wanted to add, to run the program. Just adding one anywhere inside the global crash !
Out of desperation, I ordered STM NUCLEO boards with uno pinout.

Subject closed

As usual a field overflow ...

Thx for your support

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.