I'm trying to figure out why my program has an infinite loop. I'm pretty sure it has something to do with the catch(bad_alloc) function or maybe the while (true) loop above it but I'm not sure. Can you help me figure out why i have an infinite loop and help me fix it? Thanks
----------------------------------
main.cc
----------------------------
#include
#include
#include
#include "numbers.h"
using namespace std;
int main()
{
Numbers N1, N2;
for(size_t i = 2; i < 16; i +=2)
N1.add(i);
N1.display();
cout << std::endl;
N2 = N1;
for(int i = 0; i < 4; ++i)
N2.remove_last();
for(size_t i = 5; i < 20; i += 5)
N2.add(i);
N2.display();
unsigned long start, stop, running;
start = time(NULL);
size_t item = 0;
try{
while(true){
Numbers N3;
for(int i = 0; i < 100; i++){
N3.add(item);
N3.display();
++item;
}
}
}
catch(bad_alloc){
cout<<"Memory failure after adding " <
}
stop = time(NULL);
running = (stop - start)/60;
cout<
return 0;
}
--------------------------------------
numbers.h
------------------------------------
#include
class Numbers{
public:
void add(unsigned long item);
void resize();
void display();
void remove_last();
Numbers();
Numbers& operator = (const Numbers &other);
private:
unsigned long * data;
std::size_t used;
std::size_t capacity;
};
Numbers::Numbers(){
used = 0;
capacity = 5;
data = new unsigned long[capacity];
}
void Numbers::add(unsigned long item){
if (used == capacity){
resize();
}
data[used] = item;
++used;
}
void Numbers::resize(){
unsigned long *tmpdata;
tmpdata = new unsigned long[capacity+5];//Allocate bigger
array
for (size_t i=0; i tmpdata[i] = data[i];//Copy data
}
long unsigned *temp = data;
delete[] temp;//delete old data
data = tmpdata;//reassign original data
capacity += 5;//update capacity
}
void Numbers::display(){
std::cout << "Displaying array...." << std::endl;
for (size_t i=0; i std::cout << data[i] <<
std::endl;
}
}
void Numbers::remove_last(){
--used;
}
Numbers& Numbers::operator = (const Numbers
&other){
if (this != &other){
long unsigned *temp = data;
delete[] temp;
data = new long unsigned [other.capacity];
capacity = other.capacity;
used = other.used;
for (int i = 0; i < capacity; i++){
data[i] = other.data[i];
}
}
return *this;
}
Hello,
It is due to the while loop, You have to use break to exit
oput of the loop .
I have added the code to break, It will break when i becomes 100
and declared i , so that it can be accessed outsode for. The added
part of the code is highlighted in bold.
#include "numbers.h"
using namespace std;
int main()
{
Numbers N1, N2;
for(size_t i = 2; i < 16; i +=2)
N1.add(i);
N1.display();
cout << std::endl;
N2 = N1;
for(int i = 0; i < 4; ++i)
N2.remove_last();
for(size_t i = 5; i < 20; i += 5)
N2.add(i);
N2.display();
unsigned long start, stop, running;
start = time(NULL);
size_t item = 0;
try{
int i=0;
while(true){
Numbers N3;
for( i = 0; i < 100; i++){
N3.add(item);
N3.display();
++item;
}
if(i==100)
break;
}
}
catch(bad_alloc){
cout<<"Memory failure after adding " <<endl;
}
stop = time(NULL);
running = (stop - start)/60;
cout<<running;
return 0;
}
Thanks, let me know if there is any concern.
Get Answers For Free
Most questions answered within 1 hours.