Thread: Simple Buffer Overflow Question

  1. #16
    Registered User
    Join Date
    Jun 2003
    Posts
    10
    Dev will compile with either:

    #include <iostream>
    using namespace std;

    or

    #include <iostream.h>


    BH <--- C++ nOOb

  2. #17
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Alright... Sorry I said "discarded". I wasn't speaking about the input buffer, but rather the assignment to the character. Clearly, you cannot assign a long string to a single character, and so, for the purposes of that assignment, the extra characters were discarded.

    * edit *
    Dev doesn't compile anything, its just the IDE. The underlying compiler is some flavor of GCC (Cygwin or MinGW), which is standard compliant.
    Last edited by Zach L.; 06-13-2003 at 03:24 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #18
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by Black-Hearted
    Dev will compile with either:

    #include <iostream>
    using namespace std;

    or

    #include <iostream.h>


    BH <--- C++ nOOb
    Only the first one is legal C++, so if your compiler allows it, you should certainly use it.

  4. #19
    Registered User
    Join Date
    Jan 2003
    Posts
    311

    gcc and iostream.h

    gcc 3.2 warns by default on iostream.h, or any of the depreciated headers. The Dev-C++ distribution may have modified this as the warning really couldn't be more explicit. With all the Dev-C++ users coding from seven year old books we should see lot's of complaints. The warning is relitively new, though I am not sure how new, I haven't used iostream.h for years. iostream.h has been implemented in terms of iostream for about as long as there has been an iostream. I don't expect gcc to drop the header any time soon. Half the poiint of gcc is being able to re-compile 20 year old code on a 10 year old machine with a brand new compiler and have that old program work better. If this really upsets you, changing the warning to an error would requre changing a single word in one header.

  5. #20
    Registered User
    Join Date
    Apr 2003
    Posts
    50

    Namespace std;

    What is the advantage when 'using namespace std;' ??
    I see that lots of people use it in their code, but when I delete it, the code still works. So why use it?

  6. #21
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    If you use <iostream> et al instead of the old <iostream.h> counterparts, then namespace std will make a difference. Namespaces prevent name collisions. For example, if you made a vector class in your own namespace (say, 'name1'), then the compiler would be able to distinguish between std::vector and name1::vector.

    Additionally, all of the standard C++ libraries define their symbols within the std namespace.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #22
    Registered User
    Join Date
    May 2003
    Posts
    1,619

    Re: Namespace std;

    Originally posted by FromHolland
    What is the advantage when 'using namespace std;' ??
    I see that lots of people use it in their code, but when I delete it, the code still works. So why use it?
    If it has no effect, you're not writing legal C++. What kind of #includes are you doing? For example:

    Code:
    #include <iostream.h> //ILLEGAL, should be <iostream>
    #include <string.h> //ILLEGAL, should be <string>
    #include <stdlib.h> //ILLEGAL, should be <cstdlib>
    if "using namespace std" has no effect, you're writing "old" C++, which is no longer standard, and won't even compile on many compilers.

  8. #23
    Registered User
    Join Date
    Jan 2003
    Posts
    311

    Re: Re: Namespace std;

    Originally posted by Cat
    Code:
    #include <string.h> //ILLEGAL, should be <string>
    #include <stdlib.h> //ILLEGAL, should be <cstdlib>
    <string> clearly urinates all over null terminated strings from a great height, but a replacement for string.h it ain't. <cstring> is what you want here, keeping the global namespace nice and clean. I am somewhat surprised, and would really like more information on, the ILLEGAL nature of perfectly good C headers. It is my understanding that the cleaner c* headers for the C standard library should be preferred in C++, but making their C equivalents ILLEGAL just seems to break compatibility for no clear reason.

  9. #24
    Registered User
    Join Date
    Apr 2003
    Posts
    50
    It doesn't matter as long as the compiler does not see the difference between LEGAL and NON-LEGAL code.

  10. #25
    Registered User
    Join Date
    May 2003
    Posts
    1,619

    Re: Re: Re: Namespace std;

    Originally posted by grib
    <string> clearly urinates all over null terminated strings from a great height, but a replacement for string.h it ain't. <cstring> is what you want here, keeping the global namespace nice and clean. I am somewhat surprised, and would really like more information on, the ILLEGAL nature of perfectly good C headers. It is my understanding that the cleaner c* headers for the C standard library should be preferred in C++, but making their C equivalents ILLEGAL just seems to break compatibility for no clear reason.
    You are correct, I should, of course, have said <cstring> not <string>.

    And, for example, <stdio.h> is illegal, wheras <cstdio> is legal, simply by definition of the C++ standard headers. C++ compilers are not obligated to compile C code; many cannot. The C++ standard does not require a C++ compiler to even provide <stdio.h>. It *does* require that a C++ compiler provide <cstdio>.

    The only headers that a compliant C++ compiler is guaranteed to provide are the following 51 headers:

    <algorithm>, <bitset>, <cassert>, <cctype>, <cerrno>, <cfloat>, <ciso646>, <climits>, <clocale>, <cmath>, <complex>, <csetjmp>, <csignal>, <cstdarg>, <cstddef>, <cstdio>, <cstdlib>, <cstring>, <ctime>, <cwchar>, <cwctype>, <deque>, <exception>, <fstream>, <functional>, <iomanip>, <ios>, <iosfwd>, <iostream>, <istream>, <iterator>, <limits>, <list>, <locale>, <map>, <memory>, <new>, <numeric>, <ostream>, <queue>, <set>, <sstream>, <stack>, <stdexcept>, <streambuf>, <string>, <strstream>, <typeinfo>, <utility>, <valarray>, and <vector>.

    Of course, many will provide the C headers as well, but they don't have to, and a portable solution shouldn't require them.

    Further, the C++ version of the C headers aren't functionally identical to the C versions. C++ makes additional guarantees about the standard headers that C didn't. For example, in C, it was acceptable for implementations to use masking macros; in C++, implementations are not allowed to do this.
    Last edited by Cat; 06-15-2003 at 02:37 PM.

  11. #26
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Originally posted by FromHolland
    It doesn't matter as long as the compiler does not see the difference between LEGAL and NON-LEGAL code.
    But if someone then wants to use a different compiler which DOES see a difference?
    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

  12. #27
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Addendum:

    I perused the ANSI-C++ standards, and found that <stdio.h> and the other C-headers are still legal ANSI-C++, although they are listed as deprecated and are recommended against using. They will probably be phased out in the next standard; the only reason to keep them was for compatibility with ANSI-C, but ANSI-C, since C99, isn't a subset of C++. They're like <strstream>, they still exist, still are technically legal, but they've given advance warning that they will be phased out soon. So it is, until the next standard, legal, but not advisable to use.

    Originally posted by FromHolland
    It doesn't matter as long as the compiler does not see the difference between LEGAL and NON-LEGAL code.
    Actually, it matters very much. Legal code is guaranteed by the standard to behave in a certain way. The functions, classes, namespaces, etc. are guaranteed to exist and behave as the standard demands. On the other hand, compiler-specific, nonstandard headers can be implemented however the implementor chooses. They are under no obligation to provide the functionality that you expect. For example, if you #include <iostream.h>, cin is not necessarily of type basic_istream<char>. Code that would work with <iostream> might fail with <iostream.h> because whomever implemented <iostream.h> didn't necessarily make it an equivalent to <iostream>.
    Last edited by Cat; 06-15-2003 at 02:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on buffer overflows
    By maxhavoc in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2004, 03:48 PM
  2. Quick C Question - checking buffer for input
    By sean in forum C Programming
    Replies: 3
    Last Post: 11-13-2004, 12:23 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Buffer overflow issue.
    By caroundw5h in forum C Programming
    Replies: 3
    Last Post: 12-27-2003, 12:13 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM