question on consts.

This is a discussion on question on consts. within the Linux Programming forums, part of the Platform Specific Boards category; Hi guys, I was wondering if there is a function or tool in an editor.. or program that can add ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    52

    question on consts.

    Hi guys,

    I was wondering if there is a function or tool in an editor.. or program that can add const modifiers to parameters that aren't changed withing a function.. etc..

    Thanks,

    Ed.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    It's more part of the design process. You should mark variables that should not change as const partly to prevent mistakes of your own.
    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.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,244
    Quote Originally Posted by Elysia View Post
    It's more part of the design process. You should mark variables that should not change as const partly to prevent mistakes of your own.
    I think the question was more about whether there is a tool that can automatically analyze the code and determine where "const" can be added.

    There are such tools but I don't know of any free ones, and the commercial ones do not come cheap. Coverity is one popular tool for static source analysis. It starts at $30,000.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    That was my intended point, though. Kind of. Yes.
    A simple tool might detect variables that aren't modified and change them to const.
    But are there tools that can detect which variables really should be const? That's the question, and probably is more of a design issue rather than a tool issue.
    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.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    52
    There are such tools but I don't know of any free ones, and the commercial ones do not come cheap. Coverity is one popular tool for static source analysis. It starts at $30,000.
    Yesterday
    hmm very interesting, I didn't know it would be so expensive :\.
    What about stuff for:

    -Detecting unnecessary includes
    -Detecting functions that are not implemented. Since when modyfing others code I end up moving only class method implementations I need, and I have a bunch declared that are not implemented.
    -Detecting unused variables?

    I work develop right now mostly in linux, so a linux tool would be preferable.

    Ed.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Detecting functions that are not implemented.
    The linker will do that. Only when they're used, of course.

    Detecting unused variables?
    Try enabling all warnings (-Wall -Wextra) and optimization (-O1 or higher) in GCC.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Detecting unused includes is pretty easy, unless the project is enormous, by experimenting - just remove the include and compile - if compiles without warnings & errors, then it's not needed. It gets a bit harder if you have many different configuration options, but it's usually doable. Start with files that have a large proportion of include vs. actual code (where you have the biggest gain!).

    I don't know of any tool that actually does this for you, however.

    A cross-referencing tool can also help you with all manner of things - however, classes and pointers to classes can lead to interesting effects, e.g.
    Code:
    class A
    {
       public
          virtual void foo();
    };
    
    cass B: public A
    {
       public:
          virtual void foo();
    };
    
    void func(A *ptr)
    {
       ptr->foo();
    }
    The linker will probably complain if you don't implement B::foo(), but if you never actually call func() with a pointer to B - because of the way that func is being used - then it's very difficult to discover - I don't think coverity can do that either (I have used coverity a little bit - but not a whole lot).


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

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    Quote Originally Posted by matsp View Post
    Detecting unused includes is pretty easy, unless the project is enormous, by experimenting - just remove the include and compile - if compiles without warnings & errors, then it's not needed. It gets a bit harder if you have many different configuration options, but it's usually doable. Start with files that have a large proportion of include vs. actual code (where you have the biggest gain!).
    There's only one problem with that, though.
    Since header X may include header Y, you will find that the code may compile without header Y and you may deem it unnecessary, remove it, and thus, the code will fail to compile on other compilers.
    Beware.
    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.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    There's only one problem with that, though.
    Since header X may include header Y, you will find that the code may compile without header Y and you may deem it unnecessary, remove it, and thus, the code will fail to compile on other compilers.
    Beware.
    That's what I meant by "but it gets harder if you have many configurations" (as the different configurations will mean many compiles with either different compilers or different settings).

    Beware of differences between debug and release builds too - release builds may not use the logging code, which is produced in a debug build. So when you remove say, stdio.h, then the code will build in release form, but not in debug form.

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

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    52
    A cross-referencing tool can also help you with all manner of things - however, classes and pointers to classes can lead to interesting effects, e.g.
    By cross-referencing tool, do you mean something like eclipse's indexing.. though its indexing isn't very good, sometimes I'm frustrated on how it is so INCONSISTENT.

    What cross-referencing tool would you recommend me, matsp?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    We use a version of "linux cross reference", which is used on this site:
    http://lxr.linux.no/

    It's not perfect, but it's pretty good. Source code for that site is available via link on the above page.

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

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > or program that can add const modifiers to parameters that aren't changed withing a function..
    Lint will do a lot of that, and at around $400 (oh man, it's gone up!) for a single-user licence, it pays for itself with the first bug it finds that you no longer have to spend a day looking for the old fashioned way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Jul 2008
    Posts
    52
    Quote Originally Posted by Salem View Post
    > or program that can add const modifiers to parameters that aren't changed withing a function..
    Lint will do a lot of that, and at around $400 (oh man, it's gone up!) for a single-user licence, it pays for itself with the first bug it finds that you no longer have to spend a day looking for the old fashioned way.
    That's looks pretty cool, FlexeLint is the one you are talking about right?

    There's also Visual Lint, is that one pretty good too, and could it work for Linux?

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    They're the same product (with different names) for different markets.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    There are open lint implementations. For example FreeBSD includes one and I'm sure it isn't any closed source implementation.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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