Problem with nothrow

Please bare with me. I'm a beginner and in need of much help.

For some reason when I intentionally type in a value for i too large to be allocated in my computer's memory, I don't get the "Error: memory could not be allocated" message? I'm using bloodshed's dev c++ 4.9.9.2

second. I'm afraid that i'm wasting my computer's memory if I type in a value for i during runtime that is large but can, in fact, be handled by my computer's memory, and don't input any numbers. Let's say I type in 1000, and click out of the program without typing in 1000 numbers. Is this using my memory. If so, how can i free that memory up?


This is the program as featured on an online C++ tutorial (minus the cin.gets)

#include
#include
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 == 0)
cout << "Error: memory could not be allocated";
else
{
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
}
cin.get();
cin.get();
return 0;
}

Thanks in advance.


Comments

  • : Please bare with me. I'm a beginner and in need of much help.
    :
    : For some reason when I intentionally type in a value for i too large
    : to be allocated in my computer's memory, I don't get the "Error:
    : memory could not be allocated" message? I'm using bloodshed's dev
    : c++ 4.9.9.2

    Windows allocates a 4GB virtual address space (vas) to every program that runs. This is managed by Windows via virtual memory and paging. This is 4294967295 decimal.

    On Win32 based systems, ints are generally 32 bits. Hence the largest value it can store happens to be the above same number. Assigning it a number greater then what it can store will wrap around back to 0.

    Hence, it is not possible to allocate a single block larger then the amount of RAM on your system, as the amount of virtual memory will be larger. You can test this by disabling paging (or decreasing the pagefile size that Windows can use.)

    (If you allocate multiple blocks of very large allocations, Windows will increase the pagefile size and give you a warning; or new will just throw std::bad_alloc)

    : second. I'm afraid that i'm wasting my computer's memory if I type
    : in a value for i during runtime that is large but can, in fact, be
    : handled by my computer's memory, and don't input any numbers. Let's
    : say I type in 1000, and click out of the program without typing in
    : 1000 numbers. Is this using my memory. If so, how can i free that
    : memory up?

    Use the STL, or handle memory management more effectively. The basic idea is to create a system that allows dynamic allocations and deallocations during runtime; rather then creating one large allocation.

    Your example can easily be managed better using an std::vector<> or std::list<>; both are vairents of Linked Lists (Look it up; its a useful structure for managing memory.)

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion