const function parameters

This is a discussion on const function parameters within the C Programming forums, part of the General Programming Boards category; I have a few questions about const function paramaters ie. Code: void constfunc(const int *i) {} //as opposed to void ...

  1. #1
    Registered User Tox|k's Avatar
    Join Date
    May 2008
    Posts
    6

    const function parameters

    I have a few questions about const function paramaters ie.
    Code:
    void constfunc(const int *i) {}
    //as opposed to
    void func(int *i) {}
    Is there any benefit to using them, either than maybe keeping things well formed and informative? Is the code that's generated for the function any different than not using const parameters, or is this just a compile time check?

    I just have doubts about them seeing as all one has to do to modify the variable is declare another pointer, copy over the address of the const, and then use the new pointer to modify the data (not that I'd ever do such a thing ).

    Actually, is there much point to using const at all, either than having some useful compiler warnings when you try to do something you shouldn't?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Const correctness happens to be more of a C++ concept than a C concept at this point of time, but you could still read this FAQ section: Const correctness.

    C compilers are more lax about code that attempts to circumvent const.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A good compiler will most likely optimize external functions that use const better than non-const functions - in the sense that if you have something in a register, that is also pointed to by a parameter to a function, it can't continue to hold the value in a register.

    Of course, if the function is in the same file (or in a header that is included by the source file), and it's inlined, then compiler should be able to figure out that the value is not being changed anyways.

    --
    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.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    53
    Quote Originally Posted by laserlight View Post
    C compilers are more lax about code that attempts to circumvent const.
    Unfortunately, it is "implementation-defined" what happens when you modify a const pointer anyway. You may get a compiler warning, but it will compile.

    ---

    Computer Programming: An Introduction for the Scientifically Inclined

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,456
    That's why C is so incredibly unsafe.
    It's far better to write C++/C (compiling C inside C++). No worries about modifying const then.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. function overloading. const T and const T* parameters
    By Mario F. in forum C++ Programming
    Replies: 7
    Last Post: 06-07-2006, 11:04 AM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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