Thread: Couple of small questions. Buffer overflow and passing variables through functions?

  1. #1
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605

    Couple of small questions. Buffer overflow and passing variables through functions?

    Just a couple small ones that I couldn't seem to find solid answers to via either this message board or Google.

    First of all, I've got a simple numbered menu utilizing an integer for user input via cin. When anything other than a number is entered, the program appears to shoot into an infinite loop.
    I've attempted to use both char and string variables to no avail.

    As for my second question, I need to pass a string variable with its value across multiple functions. How would I go about accomplishing something like that? I can't seem to find anything on that one either.

    These are the last two issues that are preventing me from releasing this project, which was basically a self-taught experiment to see whether I could do it or not, spanning over the last couple of months.
    .

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by civix
    First of all, I've got a simple numbered menu utilizing an integer for user input via cin. When anything other than a number is entered, the program appears to shoot into an infinite loop.
    I've attempted to use both char and string variables to no avail.
    There are a few ways to fix this, one of which involves clearing the error state after a failed read, and another which involves reading into a string, then parsing the string for the integer.

    Quote Originally Posted by civix
    As for my second question, I need to pass a string variable with its value across multiple functions. How would I go about accomplishing something like that? I can't seem to find anything on that one either.
    You could pass it as an argument. The parameter could be a value, a (const) reference, or a pointer to a string, depending on your requirements.
    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

  3. #3
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    Sorry for the long bump. I've been deployed.

    As for the buffer overflow situation:

    Code:
    stringstream ss(str);
    int x;
    ss >> x;
    
    if(ss) { // <-- error handling
      // use x
    } else {
      // not a number
    }
    Would use of Stringstream be acceptable?
    .

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is no danger of buffer overflows at all. If trying to read a string into an integer, it simply won't work.
    What you describe is one of the most common methods to do input via the console properly and securely, so yes, it is acceptable.
    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