1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

Global variable memory allocation

Discussion in 'Embedded' started by ravikumar.n, Jul 5, 2005.

  1. ravikumar.n

    ravikumar.n Guest

    Hi all,
    I am Ravi Kumar.N, I have a query regarding memory allocation
    for global variable.


    1) I know that for global variable memroy will be allocated during
    compilation process and the value assigned for that variable will be
    zero.

    2) If we explicitly allocate some known value to the global variable,
    the assignment of that value to the corresponding variable will take
    place during compilation process or does it take place during execution
    of the program.

    Please let me know.


    With regards,
    Ravi Kumar.N
     
    ravikumar.n, Jul 5, 2005
    #1
    1. Advertisements

  2. Assuming you are compiling for an embedded target and assuming that you
    are storing the firmware in Flash in binary form...

    The linker will be directed by you to place preset variables in the
    binary file. You must then (using startup code) copy this memory to the
    RAM location that you specified it to run at. The global and static
    variables are just a location in RAM, but the location must be zero'd by
    you, also done in the startup code (eg. crt.s).

    So to answer your question, the variable is preassigned by the compiler
    or linker by generating a block of data in the binary file that
    represents the values for each pre assigned variable. It is left up to
    you to copy this chuck of data to RAM before calling main().

    Of course if you are running an operating system, then the OS does all
    this for you and the binary file is actually a structured file format
    that directs the OS in setting itself up for execution, ELF and COFF are
    two such examples of these.

    Notes:
    (1) For GCC, the output is usually an ELF file. This must be translated
    into a binary file by you, using 'objcopy' in the binutils.

    (2) For information on configuring the GCC linker, 'ld' scripting
    information. http://sourceware.org/binutils/docs-2.16/ld/index.html

    (3) The LPC2000 Eclipse HowTo is an invaluable resource that I believe
    can be used for any target. It describes the steps involved in
    generating startup code and the linker script:
    http://www.olimex.com/dev/pdf/ARM Cross Development with Eclipse.pdf

    - Richard
     
    Richard Willis, Jul 5, 2005
    #2
    1. Advertisements

  3. Zero, or whatever else you initialized it to. And this happens not
    only for "global" variables (a concept the C language doesn't actually
    have), but to all variables of what the languages "static storage
    duration". That includes variables defined inside code blocks but
    qualified "static", and all variables defined external to all code
    blocks (so-called "external" objects).
    First, a terminology nitpick: initialization of static objects is not
    actually called an "assignment". The two words mean different things.

    It cannot really be said when exactly initialization happens. The
    final decision that it's going to be done is made by the
    compiler+linker, but the last activity is usually carried out by the
    run-time environment, at some opportune moment (usually directly on
    start-up). It can be the equivalent of a series of automatically
    generated simple assignments, or single block copy of all values from
    the program file's stored version directly to the actual variables
    (the "data segment" approach), or it can be something completely
    different.

    This is not something an ordinary C programmer would ever have to
    worry about. It's the job of whoever implemented the C compiler for
    the given target platform to get this done in whatever way he pleases.
    The programmer can assume that by the time a variable is first
    referred to, the iniatilization has been carried out.

    Only in the embedded world, where people commonly have to at least
    check the start-up code and runtime environment, and possibly adapt
    then to their particular hardware, this promise is not always kept.
    It's still the compiler maker's business to document the necessities,
    though, even in this case. The language definition has nothing to say
    about this.
     
    Hans-Bernhard Broeker, Jul 5, 2005
    #3
  4. ravikumar.n

    CBFalconer Guest

    All you know, and all you need to know, is that the value is
    initialized before your program gets control. It's all in the
    standard, which see.
     
    CBFalconer, Jul 5, 2005
    #4
  5. Zero, or whatever else you initialized it to. And this happens not
    only for "global" variables (a concept the C language doesn't actually
    have), but to all variables of what the language calls "static storage
    duration". That includes variables defined inside code blocks but
    qualified "static", and all variables defined external to all code
    blocks (so-called "external" objects).
    First, a terminology nitpick: initialization of static objects is not
    actually called an "assignment". The two words mean different things.

    It cannot really be said when exactly initialization happens. The
    final decision that it's going to be done is made by the
    compiler+linker, but the last activity is usually carried out by the
    run-time environment, at some opportune moment (usually directly on
    start-up). It can be the equivalent of a series of automatically
    generated simple assignments, or single block copy of all values from
    the program file's stored version directly to the actual variables
    (the "data segment" approach), or it can be something completely
    different.

    This is not something an ordinary C programmer would ever have to
    worry about. It's the job of whoever implemented the C compiler for
    the given target platform to get this done in whatever way he pleases.
    The programmer can assume that by the time a variable is first
    referred to, the iniatilization has been carried out.

    Only in the embedded world, where people commonly have to at least
    check the start-up code and runtime environment, and possibly adapt
    then to their particular hardware, this promise is not always kept.
    It's still the compiler maker's business to document the necessities,
    though, even in this case. The language definition has nothing to say
    about this.
     
    Hans-Bernhard Broeker, Jul 5, 2005
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.