Dynamic memory tutorial does not work expected

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 // ConsoleApplication9.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <new>
#include <cstdlib>
using namespace 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,
Hi,

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.

http://en.cppreference.com/w/cpp/header/climits
Last edited on
Hello jecalderon,

You should check into what the size is of the variable you are using. I.E., what can an "int" hold. When I changed "p" to a "long long" it worked.

FYI the first part is something I had and the last four line I just added. You will need to include the header file to run this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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(long long) << " 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(long double) << " 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<long long>::min() << '\n';
	std::cout << " Maximum value for long long: " << std::numeric_limits<long long>::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

Hope that helps,

Andy
Topic archived. No new replies allowed.