Thread: Questions about variable declarations

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    106

    Questions about variable declarations

    Hi Guys
    I try to do my own research, but some questions I can’t express well into Google.
    Basically, I’d like to know if I’m correct about some things I just assume at the moment.

    Code:
    int n;
    int i = 10;
    If you declared variables n and i as above in the top line,
    and your program never used either of them, the compiler would discard n at compile time, but the value 10 in i would consume RAM at program run time because you put a value in it.

    Code:
    unsigned char dataa[10]={0,1,2,3,4,5,6,7,8,9};
    const unsigned char datab[10]={0,1,2,3,4,5,6,7,8,9};
    If you declared arrays dataa and datab as above, the reason datab array can’t be written to is due to being stored in a program memory area.
    Thanks

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    but the value 10 in i would consume RAM at program run time because you put a value in it.
    Not necessarily, if the variable is never used the compiler can optimize the variable away and not use any memory.

    the reason datab array can’t be written to is due to being stored in a program memory area.
    Again not necessarily. First not all architectures have separate memory areas. I've seen systems that only have volatile memory, if they loos power you need to reload the "program". I've also see programs that only have a few bytes of volatile memory, relying mainly on registers to store "variables". Getting a C compiler to that creates programs to run on these types of systems is quite problematic so you won't usually see them in a C programming environment.

    However, with most systems today, const qualified variables are quite possibly (probably) placed into read only segments to prevent modifications to the variables.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by jimblumberg View Post
    However, with most systems today, const qualified variables are quite possibly (probably) placed into read only segments to prevent modifications to the variables.
    const is checked at compile time.
    It is not necessarily (not usually?) placed in read-only memory. (That would be difficult on the stack.)
    It can be fooled:
    Code:
    #include <stdio.h>
    
    int main(void) {
        const char a[] = "abcde";
        printf("%s\n", a);
        // a[0] = 'x';  // compiler will complain if you uncomment this
        *((char*)a) = 'x'; // but this might work (does for me: intel, gcc, linux)
        printf("%s\n", a);
        return 0;
    }

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I think algorism makes a good point. You can easily cast away const-ness. I think just like the inline decoration, the compiler knows what's actually being written to and will decide on its own const-ness. If anything, const just makes it very easy to maintain, read and reason about your code. The compiler is your instruction generator so it knows what's being written to and what's being read from. const might just be more for the sake of humans.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You risk undefined behaviour if you cast away the const-ness of an object that was initially declared const.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Sadly, in C some library functions return a non-const to what may have been a const object. E.g.,
    Code:
    char *strstr(const char *, const char *);
    This is a hack and necessitates a conversion from const to non-const within the function. It's not technically UB since strstr doesn't write to the object. But the user of the function could make it UB as in the example below. So you should avoid this kind of thing if possible. I wonder why C doesn't have function overloading?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        const char s[] = "abcde";
        printf("%s\n", s);
        char *p = strstr(s, "cd");
        *p = 'x';  // UB
        printf("%s\n", s);
        return 0;
    }
    Note that if it was
    Code:
    const char *s = "abcde";
    then the data is not stored on the stack but (if possible) in a non-writable page, so the write would cause a segfault.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    106
    Thanks for the replies
    I’m programming for dsPic micro at the moment with Microchip C30 compiler.
    Memory is tight (RAM), and there comes a point where if I make an unsigned char array a const unsigned char array instead,
    that was the difference between the program compiling or not. Furthermore, I haven’t yet reached a limitation on constant arrays.
    Also, when I removed some variables that were declared with a value, because they were unused, I was able to declare, and use a few more variables.

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    >Also, when I removed some variables that were declared with a value, because they were unused, I was able to declare, and use a few more variables.

    Like Jim said above in post #2, if the variable is not used it's space in memory that would of
    been taken by that variable is then taken by any new variable. Compilers can be configured
    this way.

    If your memory space is very limited, it could be an idea to use the smallest possible
    data type for each variable. Example: if you are using integers, and the value is very small
    then consider declaring it as short. I am unsure whether such a change would really
    make much of a difference however.

    You could also consider making small functions that are used often inline.
    Be warned however, that the compiler can ignore the suggestion if it deems it
    not required of such a function.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple declarations of a variable
    By krkr in forum C Programming
    Replies: 3
    Last Post: 07-24-2015, 02:50 AM
  2. variable declarations and arithmetic expressions
    By r3zaneo in forum C Programming
    Replies: 3
    Last Post: 10-21-2012, 01:36 PM
  3. Variable Declarations
    By PritinTheGreat in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2008, 02:49 AM
  4. Few questions: Bytes, Arrays, Declarations, and Buff
    By viciousv322 in forum C Programming
    Replies: 7
    Last Post: 12-15-2005, 07:54 PM
  5. variable declarations
    By K&J in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2001, 10:28 AM

Tags for this Thread