This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Memory allaocation for variables in EMBEDDED C

Queries regading C coding for EMBEDDED systems.

If we define Variable of type "int constant i=10,

In Which segment it will be stored??( Code/data/stack )

Similarly for any variable declared as "static".

How it will be decided??

Could you please help me in knowing where the different types of variables in C language?

  • "Queries regading C coding for EMBEDDED systems."

    The answer would be specific to the particular compiler that you are using - it is not sufficient to just say "C coding for EMBEDDED systems"

    Keil C51 allocates variables according to the Memory Model that you specify:
    http://www.keil.com/support/man/docs/c51/c51_le_memmodels.htm
    eg, http://www.keil.com/support/man/docs/c51/c51_compact.htm
    (follow the links to the other models).

    You may override the default memory type imposed by the memory model by explicitly declaring a variable with a memory type specifier:
    http://www.keil.com/support/man/docs/c51/c51_le_memtypes.htm

  • Constants have to be stored in non-volatile memory. ANSI C requires all ints to have storage allocated, even if they're constant. So, the typical C system will use space in the ROM (flash, etc) for the constant, and also in the RAM, copying from ROM to RAM when the program starts. (See INIT.A51 in the Keil library.)

    To avoid this redundancy, many 8051 Keil programs will declare the constants to live in code memory.

    The ROM is not necessarily "code" if you have wired your hardware for von Neumann memory.

    "Data" versus "stack" is a more complicated question than usual on the 8051 and with the Keil compiler. Read the manual sections on the build-time call tree analysis, overlay processing, and "softwware" stack.

  • ANSI C requires all ints to have storage allocated, even if they're constant.

    It doesn't. It only requires their value to be available. Only if the address of a constant is actually taken, it has to be allocated somewhere. In all other cases the general "as-if" rule means that as long as the program and its user can't tell the difference, the translator can do whatever it pleases.

  • Are you sure the "if address never used" rule is in C89? I know the as if appeared in C++, and got adopted into C99.

    But I seem to remember this from running into it the hard way (back in the day), mostly because it surprised me, and because of the contrast with C++ (as one of the "C++ as a better C" advantages).

    However, I can't seem to turn up an online C89 spec, so perhaps my memory is faulty.

    Just another reason to conform to the C99 standard, already!

  • I'm reasonably sure the 'as-if' rule has been in the C Standard since its first edition (ANSI C89). Without it, optimization would never have been possible. That a const object whose address is never taken doesn't have to have memory allocated is a direct consequence of 'as-if' --- without taking the address, the program can never find out if the const has an address, so the compiler gets to choose whether to give it one or not.

    The difference about 'const' between C++ and C is elsewhere. C++ consts can serve as compile-time constants, just like literals or enum members. C consts are just immutable, but they can't be used to, e.g., dimension an array or bitfield.