-1

Variables declared in a code(static or global) are copied to the On-Chip Flash(ROM) first along with the entire application code. Then they are inturn copied to the SRAM. Static and Global variables are assigned an address in SRAM(not Stack) whereas local variables in a function are assigned on to the stack(part of SRAM). My doubt: By declaring a variable as "const" type, does it place the variable in On-Chip Flash(ROM), so that i can save SRAM or are const data also copied to the SRAM though their value doesnt change? (LPC17xx Memory Architecture,Keil IDE to code)?

Update: const in ROM- In this link,the answer given by Mike Kleshov confirms that const data is placed in the Onchip Flash(using Keil compiler).

AlphaGoku
  • 968
  • 1
  • 9
  • 24
  • Are you using the term ROM to indicate memory that isn't writable because of some memory protection, because it is hardware read only memory or because the compiler will not permit a change to be made to that value? – David Hoelzer Jan 13 '16 at 04:04
  • That's up to the boot loader. Are you writing your own boot loader, or using a pre-existing boot loader? – user3386109 Jan 13 '16 at 04:09
  • ROM as in the region where the pre-existing bootloader places the code when dumped – AlphaGoku Jan 13 '16 at 04:18
  • @user3386109 This question is about an embedded platform, not PC. –  Jan 13 '16 at 05:40
  • yes...Hence i tagged the question to "C" and "Embedded" only :) – AlphaGoku Jan 13 '16 at 06:12
  • @duskwuff The boot loader is simply the first code to run after a reset. And somebody has to write it. Either the OP writes it (in which case OP decides what gets copied), or somebody else wrote it (and they may or may not have provided options to control what gets copied). – user3386109 Jan 13 '16 at 07:55
  • @user3386109 The stuff described by OP is not performed (usually) by a bootloader, but by start code: the code that call `main` function. – LPs Jan 13 '16 at 08:11
  • This depends on your toolchain and project configuration. For gnu-tools, this is mostly a matter of the linker command file. And startup behaviour is done by - apparently - the startup code. – too honest for this site Jan 13 '16 at 15:40
  • I use Keil uVision for the LPC17xx project – AlphaGoku Jan 14 '16 at 04:17
  • Keil in one of their posts say that const keyword places the code in ROM [Keil-const in ROM](http://www.keil.com/forum/16967/) . But there is confusion between just using const and const with the __attribute_( (at(ADDRESS_TO_PLACE) ) ). The keil forum hasnt answered this. Any idea guys? Why are there 2 different code sizes? – AlphaGoku Jan 27 '16 at 10:47

2 Answers2

1

Yes -- on most microcontrollers, declaring a variable as const will cause it to be stored in read-only memory.

  • [MemoryArchitectureARM](http://stackoverflow.com/questions/5430284/rom-and-ram-in-arm) So as per this link,only global and static variables along with stack will use RAM. const variables will not consume RAM and remain in ROM only – AlphaGoku Jan 13 '16 at 06:25
  • 2
    Not true at all. It depends on the linker script. You can simply move `.rodata` into `RAM` instead of `.text` region. – LPs Jan 13 '16 at 08:09
  • @LPs Well, yes. It's *possible* to do all sorts of strange things if you try hard enough. But I'm talking about what happens with a sane default configuration -- no point in overcomplicating things. :) –  Jan 13 '16 at 09:02
  • Did u guys refer the link i shared? the answer given by @Igor – AlphaGoku Jan 13 '16 at 09:17
  • Keil in one of their posts say that const keyword places the code in ROM [Keil-const in ROM](http://www.keil.com/forum/16967/) . But there is confusion between just using const and const with the __attribute_( (at(ADDRESS_TO_PLACE) ) ). The keil forum hasnt answered this. Any idea guys? Why are there 2 different code sizes? – AlphaGoku Jan 27 '16 at 10:47
1

You should consult the documentation for your specific compiler and/or linker. There is no requirement in the language definition to locate const in ROM since there is no requirement for ROM.

If in doubt you should use your toolchains specific linker directives to locate data as necessary.

Note that in C++ the semantics of const are somewhat different and it may not be possible to place an non POD object in ROM. On at least one compiler I have used it is necessary in C++ to declare a POD variable as static const to ensure that it is placed in ROM. In some cases a simple const will be placed directly in the code as if it were a literal constant.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • u mean the compiler provided by Keil uVision? – AlphaGoku Jan 14 '16 at 04:29
  • @AkshayImmanuelD : Yes, but it may not be unique to that, but neither is it required behaviour. As well as verifying the documentation, I suggest that you check the map file output of your linker to verify appropriate location. – Clifford Jan 14 '16 at 10:00
  • [link](http://stackoverflow.com/questions/5430284/rom-and-ram-in-arm) has explained very well the memory mapping done – AlphaGoku Jan 14 '16 at 10:17
  • Keil in one of their posts say that const keyword places the code in ROM [Keil-const in ROM](http://www.keil.com/forum/16967/) . But there is confusion between just using const and const with the __attribute_( (at(ADDRESS_TO_PLACE) ) ). The keil forum hasnt answered this. Any idea guys? Why are there 2 different code sizes? – AlphaGoku Jan 27 '16 at 10:47