The C++ Forum FAQ

Discussion in 'C++' started by Sanskruti, Mar 21, 2007.

  1. Sanskruti

    Sanskruti New Member

    Jan 7, 2007
    Likes Received:
    Trophy Points:
    Software Consultant
    Mumbai, India

    Q: What are traditional C++ (pre-standard) headers?

    A: The traditional C++ (pre-standard) headers define classes, values, macros, and functions in headers that have *.h extension. This includes non-standard STL headers with *.h extension (iostream.h, fstream.h, vector.h, etc.) and C headers with *.h extension (stdlib.h, stdio.h, etc.). Pre-standard headers have all the code in the global namespace.

    Q: What are C++ standard headers?

    A: C++ standard headers are those headers that are specified by the C++ standard.

    Q: How do I recognize the C++ standard headers?

    A: The naming scheme for these files were specified based on the naming scheme of the traditional header files, but without the ending '.h'. For example, 'fstream.h' becomes 'fstream', 'memory.h' becomes 'memory' and so on.

    Q: What about 'iostream.h'?

    A: The header iostream.h has never been part of the official C++ standard, which means any compiler that provides iostream.h, can do what ever it wants with it, and the compiler would still be considered compliant with the C++ standard.

    Q: What about the traditional ANSI C standard header files?

    A: The traditional ANSI C standard header files are prefixed with the letter 'c'. Thus 'stdio.h' has become 'cstdio', 'stdlib.h' is called 'cstdlib', 'math.h' is called 'cmath', 'time.h' is called 'ctime' and so on.

    Q: How to assign or compare strings?

    A: Assignment and comparison are not necessarily related, but we decided to handle them together because the mistakes made by programmers beginning with C++ have a common root, that is, the tendence to use a 'natural' syntax.

    This actually is an issue only when working with C-style strings, i.e. 'char*'. The most common mistake is this:

    char *s1, *s2;
    s2 = s1;  //might not do what one would think
    This statement assigns to the char pointer 's2' the same vale as 's1'. With other words, 's1' and 's2' will point to one and the same address in memory. By no means is the string 's1' copied into 's2'.

    Another mistake that is frequently made by beginners is something like this:

    char* s = "Hello";
    s[1] = 'a'; // attempting to change the 'e' to an 'a'
    Chances are to get an access violation when running this code. The reason is that, again, the assignment applies to the char pointer 's', i.e. it will point to the address of the constant string literal "Hello'. Since the compiler is allowed to place such const literals in read-only memory, attempting to modify it has undefined results.

    Q.How to avoid problems with include files?

    A: If you declare a class in your code the compiler normally needs to know some information about the used class like e.g. size. But if you just uses a pointer or a reference, the compiler doesn't need to know those information at that point since pointers or references are always the same size (4 bytes on a windows system). So you just need to tell the compiler that there will be a class - in this case called 'CSomeClass' - which will be used later by that declared pointer or reference.

    In this case you can use the so-called 'forward declaration'...

    class foo
      some_class some_class_instance_;
    In this example you would need to provide the complete definition of 'some_class' since 'foo' will create one instance of it. Therefore the compiler needs to know the exact size of the class and how it looks like...

    class some_class;
    class foo
      some_class* some_class_pointer_;
    In this case you only have a pointer to 'some_class'. Since the size of a pointer is independent from the size of the object it is pointing to the compiler just needs to know that there is somewhere a class named 'some_class' is defined...

    Q: What is the difference between 'delete' and 'delete[]'?

    A: Whenever you allocate memory with 'new[]', you have to free the memory using 'delete[]'. When you allocate memory with 'new', then use 'delete' without the brackets. You use 'new[]' to allocate an array of values (always starting at the index 0).

    int *pi = new int; // allocates a single integer
    int *pi_array = new int[10]; // allocates an array of 10 integers
    delete pi;
    pi = 0;
    delete [] pi_array;
    pi_array = 0;

    Q: Why does declaring an array cause my program to crash?
    double x[500000];

    A: Because such a large piece of memory exceeds the stack size (a stack overflow). You need to allocate the memory on the heap instead:
    double* x = new double[500000];

    Q: What is a Design Pattern?

    A: Design Patterns represent solutions to problems what arise when developing software within a particular context.

    Q: How many types of design patterns exist?

    A: Basically, there are three categories:
    • Creational Patterns: deal with initializing and configuring classes and objects
    • Structural Patterns: deal with decoupling the interface and implementation of classes and objects
    • Behavioral Patterns: deal with dynamic interactions among societies of classes and objects

    Q.When should I use float and when double?

    A: Answer is that if precision is less of a concern than storage, consider using type float for floating-point variables. Conversely, if precision is the most important criterion, use type double.

    Q: Why does my random number generator always return the same set?

    A: A random number generator needs to be seeded before use, or it will always generate the same list of random numbers. For example the C 'time()' function can be used for this:

    One should generally avoid using 'clock()' to initialize random numbers, since 'clock()' returns the amount of time the current thread has been running, generally a number close to zero.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice