Thread: making a bug apparent

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    making a bug apparent

    My instructor said that if the writer of the code below made full use a language feature in C++ from one of the following:

    References
    The const qualifier
    NULL vs. 0
    bool
    The friend specifier
    Copy constructors

    the bug would be apparent to see.... do you guys have any idea what it is?
    I know the problem is clearly at the get method and the itsChars variable.. but what feature would make the bug apparent?

    Code:
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    class CharList {
    public:
    CharList(char* s) {
    itsChars = new char[strlen(s)+1];
    strcpy(itsChars, s);
    }
    //
    // Iterate over the characters, returning a NUL when done
    //
    char get() {
    if (*itsChars)
    return *itsChars++;
    else
    return 0;
    }
    //
    // Return the nth character
    //
    char at(int n) {
    if (n < length())
    return itsChars[n];
    else
    return 0;
    }
    int length() { return strlen(itsChars); }
    private:
    
    char *itsChars;
    };
    //
    // debugging routine -- print the characters in the list
    //
    void print(const char *label, CharList& L)
    {
    printf("%s", label);
    const char *fmt = "%c";
    while (char c = L.get()) {
    printf(fmt, c);
    fmt = ", %c";
    }
    puts("");
    }
    int main(int argc, char **argv)
    {
    if (argc != 2) {
    fprintf(stderr, "Usage: %s STRING\n", argv[0]);
    exit(1);
    }
    CharList L(argv[1]);
    print("argv[1]: ", L);
    printf("Ready to enter loop...\n");
    for (int i = 0; i < L.length(); i++) {
    printf("in loop\n");
    printf("L[%d] = '%c'\n", i, L.at(i));
    }
    printf("Done with loop...\n");
    }
    Last edited by -EquinoX-; 02-27-2010 at 11:23 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Logically, the act of printing out an object should not change that object. Otherwise, if you print it out twice, there will be different results each time.

    Make the second argument of print() a const reference. Then you will see what your instructor is talking about. The compiler will complain bitterly, but that is actually the point. Your code is broken but, if you don't follow your instructor's guidance, the compiler can't provide helpful information to recognise that.
    Last edited by grumpy; 02-27-2010 at 05:28 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Could you give me more details why making the second argument a const reference would make it work?

  4. #4
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by -EquinoX- View Post
    Could you give me more details why making the second argument a const reference would make it work?
    It doesn't make it work. It helps you find the bug in your print function. const (constant) means the function can't change it. Given that, what do you think you're doing? You're changing it. Printing shouldn't change the subject. That's your bug.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Properly indenting the code would certainly make the code more readable and make bugs more apparent.
    I don't waste my time looking through unindented code, but this line definitely looks like a bug. I don't know any compiler that wouldn't give you an error there:
    Code:
    C SC 397a Assignment 5; page 2
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by Dae View Post
    It doesn't make it work. It helps you find the bug in your print function. const (constant) means the function can't change it. Given that, what do you think you're doing? You're changing it. Printing shouldn't change the subject. That's your bug.
    so are you saying that by putting a const in the function parameter gives an indication that the print shouldn't change the subject?

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Declaring a variable (or function parameter) as "const" means that your promising to the compiler not to modify it. You're saying you want it to be "read-only". Then later you break your promise by attempting to modify it, which the compiler catches and calls you a liar! So it doesnt really matter the context (i.e. function X passes const variable Y, function Z modifies it, etc). All that matters is if your trying to do anything but read a const variable.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so putting a const in that function parameter will make the bug apparent because the compiler will fail?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by -EquinoX-
    so putting a const in that function parameter will make the bug apparent because the compiler will fail?
    Yes, there will be a compile error, unless you do some nasty things that make the compiler ignore the bug like using const_cast to cast away the const-ness inappropriately.
    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

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    @-EquinoX-
    500+ posts, and that's the best code formatting you can foist upon us?
    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.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Dae mention the term "subject", what is he actually referring to?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    An analogy, I believe. Basically, saying that your function shouldn't be changing something, yet it does.
    Now, why is your indentation POC?
    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. Disappearing Bug
    By el-sid in forum C++ Programming
    Replies: 4
    Last Post: 08-16-2009, 12:12 PM
  2. compiler bug or brain bug?
    By Lesshardtofind in forum C++ Programming
    Replies: 10
    Last Post: 07-14-2009, 03:16 AM
  3. multithread bug I cannot reproduce
    By CodeMonkey in forum C++ Programming
    Replies: 9
    Last Post: 06-10-2009, 07:34 PM
  4. Kernel bug? (attn matsp)
    By brewbuck in forum Linux Programming
    Replies: 7
    Last Post: 04-13-2009, 10:31 AM
  5. Zooming Algorithm Bug
    By stuffy_boiz in forum C Programming
    Replies: 2
    Last Post: 01-19-2009, 02:34 AM