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

This is a discussion on Couple of small questions. Buffer overflow and passing variables through functions? within the C++ Programming forums, part of the General Programming Boards category; Just a couple small ones that I couldn't seem to find solid answers to via either this message board or ...

  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
    21,636
    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.
    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
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    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

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