-1

I am learning c++ and in one lesson was a code which is about exception handling. The code is not mine, it is just the example for "try and catch". So this question is not about the code quality

My question to this code is actually: is the output and calculation of the memory size correct? When I allocate a block of memory with new double(1000), isn't the size then 8000 bytes ?

The cerr output only counts as 1kB instead of 8kB. Am I wrong?

I got the size of 1 double with sizeof(double) to confirm it is 8 bytes.

#include <iostream>
#include <cstdlib>
#include <new>
using namespace ::std;
int main()
{
    int i = 0;
    double *q;

try
{
    while (1)
    {
        q = new double[1000];
        i++;
    }
}
catch (bad_alloc &ex)
{
    cerr << "The memory is used up.  " << i
         << " Kilobyte were available." << endl;
    exit(1);
}
}
Mario S
  • 43
  • 6
  • 5
    `new double(1000)` creates a single `double` and initialises it to `1000`. `new double[1000]` dynamically allocates 1000 doubles. A `double` is typically of size `8` but may have a different size. Either way, your code increments `i` (increases it by one) after each successful allocation, so the output value of `i` will be `1` for every `1000` doubles successfully allocated. – Peter Mar 30 '19 at 21:26

2 Answers2

3

To summarize what @Peter said in his comment: Your i variable is counting the number of allocations, not the total amount of memory allocated.

Note, however, that even if you "fix" this, what you'll get is not the amount of "available memory", nor even the amount of available memory rounded down to a multiple of 8000. This is because "available memory" is not a very well-defined concept. The operating system may be willing to let you allocate a zillion bytes; but it might not actually do anything visible to other processes until you start writing into that memory. And even if you do write to it - it could swap unused memory pages to the hard disk / SSD, to make room for the pages you're working on.

If you wanted to check what the maximum amount of memory you can allocate using new, you might consider using a binary-search-like procedure to obtain the size; I won't spell it out in case that's your homework. (And of course, this too will not be accurate since other processes' memory use fluctuates.)

Also consider reading: How to get available memory C++/g++?


Finally, some nitpicks:

  • You're using inconsistent indentation. That's confusing.
  • i is not such a good name for a variable. num_allocations would fit better. When you use a more meaningful name you also commit to its semantics, which makes it more difficult to get them mixed up.
  • Try to avoid "magic numbers" like 1000. Define constants using enum or constexpr. For example: enum { Kilo = 1000 };.
  • There doesn't seem to be a good reason to use double in such a program - which has nothing to do with floating-point arithmetic.
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • @einpolkum Thank you for the explanation. It is not my code, it is the solution of one question at the end of a chapter. The chapter was actually about exception (try / catch), this might be the reason the code is not so good. – Mario S Mar 31 '19 at 07:24
  • @MarioS: Fair enough. Remember, though, that other people reading this question and answer don't know / don't care whose code it is - they just see the code... so it's important to comment about these issues. – einpoklum Mar 31 '19 at 07:52
1

You are absolutely correct. It should be:

cerr << "The memory is used up.  " << sizeof(double) * i
     << " Kilobyte were available." << endl;
TonyK
  • 16,761
  • 4
  • 37
  • 72