3

I'm fairly new to C++ so bear with me.

I have the following program to learn about dynamic memory allocation.

#include<iostream> #include<new> using namespace std; int main () { int i,n; int * p; cout << "How many numbers would you like to enter? "; cin >> i; p = new (nothrow) int [i]; if (NULL == p){ cout << "Not enough memory!"; }else{ for (n=0; n<i; n++){ cout << "Enter a number: "; cin >> p[n]; } cout << "You have entered: "; for(n=0; n<i; n++){ cout << p[n] << ", "; } delete[] p; } return 0; } 

So long as a sensible amount is entered initially the program runs as expected. But when a huge number (1000000000000) is entered I expected the output "Not enough memory" when in fact it starts printing "Enter a Number: " presumably 1000000000000 times, obviously I haven't waited for output. Since this is in the "else" part of the check, why is this happening? I guessed that the comparison isn't working. Any help is appreciated. Thanks.

13
  • 1
    You might want to review the code you posted. It seems that there has been some issue with the copy-n-paste Commented Jun 24, 2013 at 20:42
  • 1
    If you're compiling a 64-bit program from that code, it should be able to happily allocate the 4GB of memory required to hold a billion integers. Commented Jun 24, 2013 at 20:47
  • 1
    @TaylorBrandstetter: Of course, if you typed if (!p) that would not be an issue at all and it would be readable (without resorting to Yoda programming) Commented Jun 24, 2013 at 20:49
  • 3
    @Taylor It's considered good practice by many who don't turn up their compiler's warning settings high enough to put .... There, FTFY :) Commented Jun 24, 2013 at 20:49
  • 1
    @MattPellegrini, are you sure your number doesn't have an additional zero? Is it really 1bln what you enter and not something even bigger? Commented Jun 24, 2013 at 20:51

3 Answers 3

2

If the first number you enter is more 2^31 one of the possible reasons is the following:

After you give invalid data the first time cin becomes in invalid state and each next data input operation (e.g. >> ) does nothing (so it doesn't wait for your input) unless your explicitly return cin to normal state.

One of the possible solutions for you are: add after

cin >> p[n]; 

this piece of code:

if (cin.fail()) { cout << "Bad data" << endl; cin.clear(); } 
Sign up to request clarification or add additional context in comments.

1 Comment

This is pretty much it, thanks. I was totally unaware, of the concept of cin entering this error state
1

First, as somebody already noticed, it could be that 1 billion was actually allocated. It fits in an integer (limit is ~2 billion), and it requires you to have 4gb of memory.

Anyway, before starting printing, I suggest you print the number you have received in input (and then put a pause, one second, or waiting for an input from the user): that value might be different from what you have expected, because it might be too big to be read correctly from cin.

You might therefore want to define i as an unsigned long long.

What is the difference between unsigned long and unsigned long long?

Also, check you are not putting cin in an error state giving a string it cannot parse

Edit from Mooing Duck suggestion:

Use

 if (std::cin >> variable) { } 

or

 while(std::cin >> variable) { } 

to avoid this problem. Avoid checking .bad(), .fail(), or .eof(), they're often misused, leading to bugs.

16 Comments

Thanks, this solved it, so the number was too big for an int, so the cin wasn't working. Using a long solved this and now outputs "not enough mem" as expected, thanks all
I also suggest that you don't name the variable i, as it is commonly what you use inside a cycle for, try to use meaningful names.
Does anyone no why this behaviour happens? So for example even with the fix, if I enter 's' at the first input, the same thing happens? Why is no error thrown?
@MattPellegrini: Usually, we use if (std::cin >> variable) { } or while(std::cin >> variable) { to avoid this problem. Avoid checking .bad(), .fail(), or .eof(), they're often misused, leading to bugs.
@Adrian No wait, the code Antonio linked. New rule, each person's name in a conversation must start with a unique letter.
|
1

Depending on the OS, requests for large blocks of memory may be granted even if there is not enough memory, in the hope that by the time it is needed there will be enough (some process might have released memory it held, or more swap memory might become available). In those systems the call to the allocator will succeed but memory will be handed to the process only on demand (i.e. as you use the memory pages) eventually triggering a fault when the next page cannot be allocated.

This is not really an issue with C++, but with the behavior of the operating system.

2 Comments

Interesting to know, but this doesn't really answer the problem. The program doesn't wait for user input as it would with a granted amount of memory requested, it just starts printing the "Enter a number, over and over without waiting for input. I'm going to look into the answers below and post some results
@MattPellegrini It is of crucial importance knowing how to ask questions. The issue seems to be that of an error while parsing the input, but the original question had a number that will parse correctly into an integer in most if not all current architectures. Providing the wrong code or input does not help you get an answer and makes others waste time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.