constant in function, what for?

This is a discussion on constant in function, what for? within the C Programming forums, part of the General Programming Boards category; I recently met following statement: Code: void func(void) { const unsigned char *const tab[]= { "Str1", "Str2", "Str3" }; /* ...

  1. #1
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42

    constant in function, what for?

    I recently met following statement:
    Code:
    void func(void)
    {
        const unsigned char *const tab[]=
            {
                 "Str1",
                 "Str2",
                 "Str3"
            };
     /* some code that uses strings form array */
    }
    Is there any paticular reason of not making that array external to the function? And how is const created inside function, is it put on stack as any normal variable but has 'const' attributes?

    I do code in embedded world and this is essential for me as in 'my' world const means FLASH and variable is in SRAM. Therefore, I do not understand how all of sudden when a function is called it creates const .... in FLASH - how??

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    by making the value const, it means that the compiler knows [and checks -> gives error if you don't follow your own rules] that you are not going to change the values in the array in any form.

    I'm not sure if the compiler produces this as tables in read-only (flash, etc) memory, or if it's simply a separate (read-only tagged) section of RAM - I think the compiler would be perfectly OK generating this as a table in flash when compiling the code [since it can know we have no intention to change the values].

    The compiler certainly doesn't HAVE to put this in RAM or ROM - it can use whichever it likes [and it's actually the linker/loader that decides where things go, the compiler just says "this goes in the constant section", and the linker will say "Ah, constant section, let's put it here" - whether that is flash or RAM depends on the setup of the linker.]

    Of course, any initialized data that is in RAM must also be stored in ROM, and gets copied during startup.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    Ok I agree with what you said.

    Quote Originally Posted by matsp View Post
    Of course, any initialized data that is in RAM must also be stored in ROM, and gets copied during startup.
    I'm not sure about this one. In embedded you usually create cstartup file which is written in assembly and it has section that describe which part of SRAM data should be initialized and how. Generally you initialize all static variables as a zeros. And then specific values if any. Rest is random (what ever charge stays in SRAM cell).

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, I should have said "initialized data that is not zero". Obviously for the (large majority of) [global/static] variables that are initialized to zero, we can use something akin to memset(datastart, 0, datasize);. But for values that are not zero, we need to know what the values are and where they should be stored, and generally, this is done by simply storing the initalized data in ROM and copying to the relevant area in RAM as part of the startup code.

    And yes, local variables will hold whatever happens to be in that memory at the time.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21