SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old January 11th, 2003, 12:36 PM
Ratt Ratt is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 6 Ratt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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?

Reply With Quote
  #2  
Old January 15th, 2003, 03:51 PM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 6
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/

Reply With Quote
  #3  
Old January 15th, 2003, 07:00 PM
Ratt Ratt is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 6 Ratt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #4  
Old January 16th, 2003, 08:33 AM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 6
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.

Reply With Quote
  #5  
Old January 20th, 2003, 09:12 AM
Ratt Ratt is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 6 Ratt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C++ Programming Question (Compiling help - Abnormal program termination)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway