简体   繁体   中英

C++ comparing a pointer to NULL

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.

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 (eg >> ) 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();
}

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 (ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM