#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    6
    Rep Power
    0

    C++ Programming Question (Compiling help - Abnormal program termination)


    Source code:

    Code:
    #include <iostream>
    #include <cstring>
    #include <cstddef>
    #include <stdexcept>
    using namespace std;
    
    const int maxnames = 5;
    class Names    {
        char name[25];
        static char Names::pool[];
        static short int Names::inuse[maxnames];
    public:
        Names(char* s = 0)
            { if (s) strncpy(name, s, sizeof(name)); }
        void* operator new[](size_t) throw(bad_alloc);
        void operator delete[](void*) throw();
        void display() const
            { cout << name << endl; }
    };
    
    // simple memory pool to handle fixed number of Names
    char Names::pool[maxnames * sizeof(Names)];
    short int Names::inuse[maxnames];
    
    // overloaded new[] operator for the Names class
    void* Names::operator new[](size_t size) throw(bad_alloc)
    {
        int elements = size / sizeof(Names);
        for (int p = 0; p < maxnames; p++) {
            if (!inuse[p])    {
                int i;
                for (i = 0; i < elements && p+i < maxnames; i++)
                    if (inuse[p+i])
                        break;
                if (i == elements && p+i < maxnames)  {
                    for (i = 0; i < elements; i++)
                        inuse[p+i] = elements;
                    return pool+p*sizeof(Names);
                }
            }
        }
        throw bad_alloc();
    }
    
    // overloaded delete[] operator for the Names class
    void Names::operator delete[](void* b) throw()
    {
        if (b != 0)  {
            int p = ((char*)b - pool) / sizeof(Names);
            int elements = inuse[p];
            for (int i = 0; i < elements; i++)
                inuse[p + i] = 0;
        }
    }
    
    int main()
    {
        Names* np = new Names[maxnames+1];
        int i;
        for (i = 0; i < maxnames; i++)    {
            cout << endl << "Enter name # " << i+1 << ": ";
            char name[25];
            cin >> name;
            *(np + i) = name;
        }
        for (i = 0; i < maxnames; i++)
            (np + i)->display();
        delete [] np;
        return 0;
    }
    I'm using BCB 5.0 to compile the source code, but I get "Abnormal program termination" when I run the executable. Can anyone please help me figure out what's wrong?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    13
    Frankly I can't imagine what you're trying to accomplish with your names class. Your program itself appears to be doing nothing more than recording a list of names then spewing them back out. If that's your goal, there are many easier ways to accomplish it.

    Code:
    #include <vector>
    #include <string>
    #include <iostream>
    
    int main() {
    
            vector<string> name;
            char line[80] = "";
    
            while (cin) {
    
                    cin.getline(line, 80);
                    if (cin)
                            name.push_back(line);
    
            }
    
            for(vector<string>::iterator p = name.begin(); p != name.end(); p++)
                    cout << *p << endl;
    
            return 0;
    
    }
    Nothing trick or nasty there, just some nice use of the STL. Type names, and as soon as you end the input stream (^D on UNIX, ^Z on Windows/DOS) you'll get your list back.

    Clay
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    6
    Rep Power
    0
    Sorry for not being more specific. :)

    The code in question is a from a book Teach yourself C++ 5th Ed by Al Stevens that demonstrates the use of overloaded class new[] and delete[] operator functions for array allocations.

    There are no error messages when compiling the code, but when I execute the program, it displays "Abnormal program termination".

    Since there are no error messages, and I don't know what causes "Abnormal program termination", I hope that someone could:

    - explain or provide a reference source so that I could try to fix the source code.

    - if possible post a simple code that demonstrates the use of overloaded class new[] and delete[] operator functions as a guide.

    TIA
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    13
    The easiest technique that I have seen is to create a class that inherits from vector<string>, which will give you the desired constructor.

    Bjarne Stroustrup's book can provide you with more details. It's heavily technical, but the examples that he provides make it pretty easy to follow along and get the important parts.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    6
    Rep Power
    0
    Thanks. Just got back from a trip from last week. Since I drive an 18-wheeler for a living, finding time to learn programing is a bit difficult, but i'll try to work it out next Sunday. :)

IMN logo majestic logo threadwatch logo seochat tools logo