Help with some c++ runtime errors
I wrote some code and so far all it should do is allocate memory and then free it up however if I try to allocate A LOT of memory it doesn't throw an error instead it just locks up....
Now the program will accept 999999999 and throw the error I coded in but if you try adding 1 more 9 so there are 10 digits it freezes up and does not throw an error. The last message on the screen when it freezes is
"Allocating memory for operation, please wait..."
So I would think it literally freezes outside of my code in one of the defines?
Code:
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
#include <new>
using namespace std;
int* makelist (int a)
{
cout << "Initializing memory for operation, please wait... \n";
int * lenghth;
lenghth = new (nothrow) int [a];
if (lenghth == 0)
{
cout << "ERROR ALLOCATING MEMORY!\n";
return 0;
}
cout << "Done!\n";
return(lenghth);
}
void clearmem (int * addy, int lenghth)
{
cout << "Freeing memory please wait...\n";
delete[lenghth] addy;
cout << "Done!";
}
void pause()
{
cin.ignore();
cin.get();
}
int main()
{
int a;
int a_const;
int * list_location;
cout << "Enter the number of numbers you want to find in the Fibonaci sequence.\n";
cin >> a;
a_const = a;
if (a == 0)
{
cout << "Ummmm so you started this program to not use it.... im not going\nto calculate the 0th number -.-\npress enter to close...";
pause();
return 0;
}
list_location = makelist (a);
clearmem(list_location, a_const);
pause();
return 0;
}