The following code is pasted from the dynamic memory tutorial into a Win 10 Visual Studio 2017 Community IDE. Not getting the Error memory when entering a large number. PLease comment.
// ConsoleApplication9.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <new>
#include <cstdlib>
usingnamespace std;
int main()
{
int i, n;
int * p;
cout << "How many numbers would you like to type? ";
cin >> i;
p = new (nothrow) int[i];
if (p == nullptr)
cout << "Error: memory could not be allocated";
else
{
for (n = 0; n<i; n++)
{
cout << "Enter number " << n + 1 << " of " << i << " : ";
cin >> p[n];
}
cout << "You have entered: ";
for (n = 0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
}
return 0;
}
Here is what getting
How many numbers would you like to type? 5
Enter number 1 of 5 : 4
Enter number 2 of 5 : 65
Enter number 3 of 5 : -765
Enter number 4 of 5 : 3
Enter number 5 of 5 : 10000000000001
You have entered: 4, 65, -765, 3, -842150451,
Not getting the Error memory when entering a large number. PLease comment.
The error message should only appear if the memory is not allocated, very unlikely for a small number of ints. There is no code to protect against the overflow of a integer number.
But more importantly, this is not the way to do things in modern C++. By far the best way is to use a std::vector or some other STL container. It does it's own memory management. Use of new is bad news, mainly because if an exception is thrown, delete and the destructor are never reached, so memory is leaked.
Use of new is OK if one is writing their own library, say a funky type of tree ADT container. So I think it is better to learn STL and RAII before learning low level dynamic memory.
To protect against too large numbers (+ or -) one could write their own function, making use of the constants found in the <climits.h> header file.
void SizeOfVarsableTypes()
{
std::cout << '\n' << " bool:\t\t" << sizeof(bool) << " bytes" << std::endl;
std::cout << " char:\t\t" << sizeof(char) << " bytes" << std::endl;
std::cout << " wchar_t:\t" << sizeof(wchar_t) << " bytes" << std::endl;
std::cout << " char16_t:\t" << sizeof(char16_t) << " bytes" << std::endl; // C++11, may not be supported by your compiler
std::cout << " char32_t:\t" << sizeof(char32_t) << " bytes" << std::endl; // C++11, may not be supported by your compiler
std::cout << " short:\t\t" << sizeof(short) << " bytes" << std::endl;
std::cout << " int:\t\t" << sizeof(int) << " bytes" << std::endl;
std::cout << " long:\t\t" << sizeof(long) << " bytes" << std::endl;
std::cout << " long long:\t" << sizeof(longlong) << " bytes" << std::endl; // C++11, may not be supported by your compiler
std::cout << " float:\t\t" << sizeof(float) << " bytes" << std::endl;
std::cout << " double:\t" << sizeof(double) << " bytes" << std::endl;
std::cout << " long double:\t" << sizeof(longdouble) << " bytes" << std::endl;
std::cout << "\n Minimum value for int: " << std::numeric_limits<int>::min() << '\n';
std::cout << " Maximum value for int: " << std::numeric_limits<int>::max() << '\n';
std::cout << "\n Minimum value for long long: " << std::numeric_limits<longlong>::min() << '\n';
std::cout << " Maximum value for long long: " << std::numeric_limits<longlong>::max() << '\n';
}
This should explain why the int is not large enough to hold the value "10000000000001".
bool: 1 bytes
char: 1 bytes
wchar_t: 2 bytes
char16_t: 2 bytes
char32_t: 4 bytes
short: 2 bytes
int: 4 bytes
long: 4 bytes
long long: 8 bytes
float: 4 bytes
double: 8 bytes
long double: 8 bytes
Minimum value for int: -2147483648
Maximum value for int: 2147483647
Minimum value for long long: -9223372036854775808
Maximum value for long long: 9223372036854775807