0

I have a function that dumps the contents of the arduino's ram, and it works, but I still dont understand why it says my stack pointer is at 0x08C9 If the ram of the arduino is 2kb, or 2048 bytes, shouldent the max addressable size be 0x0800?

RAM: SP->0x08C9 HP->0x0371 FREE:1368

And the code:

void printAddress(uint16_t val)
{
 Serial.print(F("0x"));
 if(val < 16)
{
 Serial.print(F("0"));
}
if(val < 256)
 {
 Serial.print(F("0"));
}
if(val < 4096)
{
 Serial.print(F("0"));
}
Serial.print(val,HEX);
}
void setup(){
Serial.begin(115200);
//0x0800 2048
uint8_t *heapptr, *stackptr;
stackptr = (uint8_t *)malloc(4); // use stackptr temporarily
heapptr = stackptr; // save value of heap pointer
free(stackptr); // free up the memory again (sets stackptr to 0)
stackptr = (uint8_t *)(SP); // save value of stack pointer
Serial.print(F("RAM: SP->"));
printAddress(stackptr);
Serial.print(F(" HP->"));
printAddress(heapptr);//(int)&__heap_start);
Serial.print(F(" FREE:"));
Serial.print(stackptr - heapptr);
Serial.println();
}

As a note, Comments are not mine.

asked Feb 3, 2017 at 19:33
2
  • 1
    Your code comments contain serious conceptual mistakes - free() should not alter the stack pointer at all, and its effect on what it is freeing is undefined (or perhaps better said, access to what was free()'d is subsequently undefined) Commented Feb 3, 2017 at 20:35
  • 1
    Okay, let me rephrase that in light of the edit; the uncredited author of the code you posted has made serious conceptual errors. You should probably find a different source of inspiration. Commented Feb 7, 2017 at 19:04

2 Answers 2

1

Sure... if the RAM started at address 0, which it doesn't.

According to the datasheet the RAM starts at address 0x0100, below which are all the SFRs:

enter image description here

answered Feb 3, 2017 at 19:46
7
  • Is the internal Rom mapped below that? Commented Feb 3, 2017 at 23:07
  • No. The flash (not ROM) is on a separate bus with its own address range. Commented Feb 3, 2017 at 23:35
  • Sad, cause if it was mapped below the sram that would make it it easy to handle Progmem variables... Commented Feb 5, 2017 at 1:28
  • That's why there are special helper function ctions to assist you. Commented Feb 5, 2017 at 10:14
  • True, but it makes it difficult. PROGMEM Variables are treated as char* so creating functions to determine if I need to load it from ROM (Flash, ROM potato, potahto) or not is a crap-shoot. The F() macro Is all well and good to solve that but cant be used with Static variable so that wouldn't work. Had to Use the FLASH_STRING library to do what I wanted. Would have made alot more sense to keep with the memory hierarchy then just throwing it off somewhere else. Commented Feb 7, 2017 at 16:53
0

free up the memory again (sets stackptr to 0)

Freeing memory alters the heap, not the stack. In any case the heap is not like the stack, it is a piece of memory from which memory is allocated, not necessarily contiguously. If you were to allocate A B C from the heap, and then delete B, the heap pointer would be untouched, because it would now have a gap: A ... C

shouldn't the max addressable size be 0x0800?

As Majenko pointed out, RAM starts past the "internal registers" of the processor, so the maximum RAM would be 0x8FF in this particular case.

answered Feb 4, 2017 at 8:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.