The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
C++ Programming Question (Compiling help - Abnormal program termination)
Discuss C++ Programming Question (Compiling help - Abnormal program termination) in the C Programming forum on Dev Shed. C++ Programming Question (Compiling help - Abnormal program termination) C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 11th, 2003, 12:36 PM
|
|
Junior Member
|
|
Join Date: Mar 2002
Posts: 6
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?
|

January 15th, 2003, 03:51 PM
|
|
Contributing User
|
|
Join Date: Oct 2002
Location: Flint, MI
Posts: 328
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 11
|
|
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/
|

January 15th, 2003, 07:00 PM
|
|
Junior Member
|
|
Join Date: Mar 2002
Posts: 6
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
|

January 16th, 2003, 08:33 AM
|
|
Contributing User
|
|
Join Date: Oct 2002
Location: Flint, MI
Posts: 328
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 11
|
|
|
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.
|

January 20th, 2003, 09:12 AM
|
|
Junior Member
|
|
Join Date: Mar 2002
Posts: 6
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. 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|