Standard header files

This is a discussion on Standard header files within the C++ Programming forums, part of the General Programming Boards category; Why do most of the current standard header files have .h omitted. Do these headers take up less memory or ...

  1. #1
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942

    Standard header files

    Why do most of the current standard header files have .h omitted. Do these headers take up less memory or something? Why are they more efficient?

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    It has nothing to do with efficiency, and everything about standards.

    Pre-1998, the C++ langauge was very different. In 1998, a new ANSI C++ standard was published and the language changed. The standard library changed, too, so the headers changed.

    To prevent ambiguity, the names were also changed. The .h extentions were dropped and C standard libraries gained a "c" as a prefix. E.g. the new version (post-1998) of <iostream.h> is <iostream> and the new version of <stdlib.h> is <cstdlib>.

    The most obvious change is namespaces -- the new headers put their variables, classes, and functions in the std namespace; the old put them in the global namespace.
    Last edited by Cat; 06-12-2003 at 05:07 PM.

  3. #3
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    I see.

    But why the change in standard? In 1998, why were new standard libraries created?

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    For a number of reasons.

    1) Namespaces make libraries much easier. For example, say I make a function called less_than, and include it in a library. It would be unrealistic of me to expect that nobody else will name something the same. Namespaces let us both have our way -- as long as we use different namespaces, we can each have a function called less_than.

    This helps a LOT when (like me), you have many libraries installed. For example, I might use std::auto_ptr for one task (part of the C++ standard library), boost::shared_ptr for another task (from the Boost library), and I never have to worry about name conflicts. Boost's stuff is in one namespace, the standard library's is in another, Blitz++'s stuff is in another, etc.

    2) They added many new features. The STL had been very widely used because of its many useful features; these are now officially part of the C++ language. This means I can use code with vectors, maps, etc. and still have portability; any ANSI-C++ compiler can handle them. E.g., say I want a dynamically sized array (happens in LOTS of code). Unfortunately, it's also prone to memory leaks. Now, I can use vector to accomplish the same thing:

    vector<MyObject> objArray(size);
    replaces
    MyObject * objArray = new MyObject[size];

    and is much safer to use (it requires no work at all on my behalf to ensure it won't leak memory or try to free the same memory twice).

    3) They redid many of the classes to use templates. For example, basic_string<> is a template, used in string and wstring, and it makes it easy to develop applications that use other character sets, like Unicode (which I use exclusively).

    4) They made new classes to supercede some non-robust older classes. E.g. the strstream classes are superceded by stringstream classes.
    Last edited by Cat; 06-12-2003 at 05:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help using Header Files
    By d34n in forum C Programming
    Replies: 8
    Last Post: 04-22-2008, 12:06 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  4. Header Files
    By Volair in forum C Programming
    Replies: 2
    Last Post: 12-09-2005, 10:51 AM
  5. more header files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2001, 01:56 PM

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