Usage of 'const'

Discussion in 'C Programming' started by Peteris Krumins, Jun 27, 2003.

  1. Hello again,

    It's not clear enough for me when to use const and when not use const.

    As I understand const should be used everywhere i have a variable which
    should be put in read-only data (.rodata (is this segment standard for
    read-only data (i mean title of it 'rodata'))?) segment. So by putting it
    in .rodata segment i ensure the data can never be altered?

    Should const be used everywhere it should be used, what improvments does
    it make? If i precisely declare variables const where needed does the
    compiler do it's job better?

    For example,
    int function(const char *data, int section) {

    What's the difference if i had simply 'char *data' and not 'const char

    Anything about 'const' welcomed.

    Peteris Krumins, Jun 27, 2003
    1. Advertisements

  2. No, C won't guarantee this, your compiler might, or it might not. Don't
    depend on this. Const is only there to help you catch writes to const
    vars. at compile time. Don't expect more from it.
    Mark A. Odell, Jun 27, 2003
    1. Advertisements

  3. Peteris Krumins

    Malcolm Guest

    Don't worry about helping the compiler, unless you know enough not to have
    to ask about const.
    const tells you the programmer that data is read-only. Once you make a
    high-level object const, everything that references it has to treat it as
    const also.
    function() is not allowed to modify the contents of the string "data" if it
    is declared const. This can be useful in documenting the function - eg
    copy(const char *s1, char *s2) tells you which way the copy goes.
    Also, you can pass a const char * to function() without the compiler
    complaining. function("My name is Fred", 1) would be OK, since the string
    literal won't be modified.

    Generally, if you have a low-level routine that takes a pointer and doesn't
    modify the contents, then declare it as const. However be more careful about
    declarign high-level objects as const, especially if control over the source
    isn't too tight, as the compiler will complain if const is not used
    consistently throughout.
    Malcolm, Jun 27, 2003
  4. The trick is that, in C, the semantic of 'const' is not 'constant', but
    'read-only'. May this revelation shade a new light on your thoughts.
    These considerations are highly implementation dependent. It's however true
    that on some implementations, the read-only stuffs (mainly the code and the
    initialized constant arrays and strings) can, be laid out in read-only
    segments that can physically be a parte of a PROM (or equivallent, flash or
    whatever) on an embedded system or a read-only data segment on a hosted
    system. More details on your linker documentation. It's not a C issue.
    'const' is certainely useful as a design checker. For example, you know that
    strings literals are not guaranteed to be modifiable. (On the embedded
    systems I work on, strings are laid out on the flash. Trust me, don't attempt
    to write to a flash!)

    If you write a function accepting string literals, it is highly recommended
    that the parameter receiving the address of such a string is a pointer to
    const char.

    void print (char const *s)
    char const *p = s;

    while (*p)
    putchar (*p);

    It prevents you to write ugly things like:

    *p = 0;

    *p = getchar ();

    (The temperature is high in Paris at the moment, and I know that some brain
    cells can melt...)
    Emmanuel Delahaye, Jun 28, 2003
  5. Oops!
    Emmanuel Delahaye, Jun 28, 2003
  6. Peteris Krumins

    Dan Pop Guest

    When writing a complete application, never.
    When writing a library that may be used by other people, in every function
    parameter declaration where it makes sense.

    These guidelines will minimise the headaches caused by const.
    Here's a simple program showing the difference:

    fangorn:~/tmp 147> cat test.c
    #include <string.h>

    void mmemcpy(void *dest, void *src, size_t size)
    memcpy(dest, src, size);

    int main()
    const float f = 1.75;
    float g;

    memcpy(&g, &f, sizeof f);
    mmemcpy(&g, &f, sizeof f);
    return 0;
    fangorn:~/tmp 148> gcc test.c
    test.c: In function `main':
    test.c:14: warning: passing arg 2 of `mmemcpy' discards qualifiers from pointer target type

    Note that memcpy declares its second parameter as having the type const
    void *. This way, the function can be called with arguments that are
    pointers to both const and non-const objects. By omitting const from
    the definition of mmemcpy, I restrict it to taking only pointers to
    non-const objects as arguments (for no redeeming benefits).

    Of course, if you never use const in your program, this is a non issue.
    Hence the guidelines at the beginning of my post.

    Dan Pop, Jun 30, 2003
    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.