|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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/ |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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.
![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > C++ Programming Question (Compiling help - Abnormal program termination) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|