-1

I want to create an array using pointers but I want to set its capacity to the maximum available RAM. I've tested this method:

void init() 
{
  long  maxSize = 0x7fffffff;
  long capacity = maxSize / sizeof(int);
  int* _array = new int[capacity];
}

but this method fails and gives me this error at runtime:

Unhandled exception at 0x7651A6E2: Microsoft C++ exception: std::bad_alloc at memory location 0x012FF780.

So how can I allocate my array at the maximum available size?

S.A.Parkhid
  • 2,772
  • 6
  • 28
  • 58
  • 4
    _I want to set its capacity to the maximum available RAM._ There are other processes as well as the code of your own process which require memory as well. – Scheff's Cat Jun 03 '21 at 07:26
  • 3
    What is the problem you're supposed to solve with this enormous allocation? Why do you need "thw maximum available RAM"? And why do you think that `0x7fffffff` is a good size on your specific system? – Some programmer dude Jun 03 '21 at 07:27
  • 1
    _"I want to create an array using pointers but I want to set its capacity to the maximum available RAM."_ — Why? It does not make any sense per se. – Daniel Langr Jun 03 '21 at 07:27
  • @Someprogrammerdude, the visual studio compiler tells me that the array length must not exceed "0x7fffffff". I want to define an array with the must capacity. – S.A.Parkhid Jun 03 '21 at 07:36
  • @Scheff'sCat I know that the visual studio compiler tells me that the array length must not exceed "0x7fffffff", why? I can't define an array with a size of 0x7fffffff-1? – S.A.Parkhid Jun 03 '21 at 07:37
  • "_why?_" - Because that's the limit in VS when you're compiling it as a 32 bit program. – Ted Lyngmo Jun 03 '21 at 07:44
  • 4
    Will your system have `2147483647` bytes of ***continuous*** virtual memory available? And again, ***why*** do you need this? What is the *original* problem this is supposed to solve? – Some programmer dude Jun 03 '21 at 07:44
  • @Someprogrammerdude no, my system doesn't have this block size. I want to create an unlimited array with pointers. – S.A.Parkhid Jun 03 '21 at 07:50
  • By the way, what is wrong with `std::vector`? – Some programmer dude Jun 03 '21 at 09:49
  • And still haven't told us the actual problem you're trying to solve. Which makes this question [an XY problem](https://en.wikipedia.org/wiki/XY_problem). Always ask about the original and underlying problem directly, telling us about what possible solutions you have considered. – Some programmer dude Jun 03 '21 at 09:50
  • @Someprogrammerdude I have accepted the answer by another person, ask him for the problem or look to the answer if you don't understand my question. also, I would not use an std library like a vector. I want to use pure pointers in this case. – S.A.Parkhid Jun 03 '21 at 10:24

1 Answers1

3

You should understand that if you have 10 GB of free RAM, it does not mean that you can allocate an array of 10 GB. Imagine that the letter e denotes an empty gigabyte and the letter b denotes a busy one. And in real life, your RAM will look something like this eebeebbeeebeebe We can notice that here 10 GB are free, but we cannot allocate an array on 10 GB (let's not forget, at arrays elements go consistently in storage). So the only way I can advise is to go through the dimensions until you find the largest free.

So your code might looks like this(but NOTE, you can improve size by using std::realloc instead of new + delete, and I'm using binary search to imrove perfomance)

size_t min = 0;
size_t max = std::numeric_limits<size_t>::max();

int *ptr = new int[min];

while (max - min > 1)
{
    size_t middle = (max - min) / 2 + min;
    try
    {
        int *temp = new int[middle];
        min = middle;
        delete[] ptr;
        ptr = temp;
    }
    catch (const std::exception &e)
    {
        max = middle;
        continue;
    }
}
std::cout << "max_size is " << min << '\n';
Deumaudit
  • 978
  • 7
  • 17
  • You can't realloc memory you got from new – Aykhan Hagverdili Jun 03 '21 at 08:13
  • You can use `new (nothrow)` so it returns null instead of throwing. – Aykhan Hagverdili Jun 03 '21 at 08:13
  • 1
    Doesn't this ignore the availability of virtual addresses? You may have a virtual contiguous array which physically consists of pages spread over the physical RAM. There might be a limitation in Visual Studio (actually, in MSVC) but I doubt that memory fragmentation is the killer issue. FYI: [Virtual address space](https://en.wikipedia.org/wiki/Virtual_address_space) – Scheff's Cat Jun 03 '21 at 08:14
  • Yeah, but You can realloc memory that was allocated by malloc @AyxanHaqverdili – Deumaudit Jun 03 '21 at 08:14
  • @Scheff'sCat Yeah, I forgot about virtual memory, but I think my code still should be valid – Deumaudit Jun 03 '21 at 08:18
  • @Scheff'sCat but hey, If I understand the principle of allocation correctly, if the pages were not fully occupied by other processes, then our program will not be able to occupy the free part of all partially occupied pages – Deumaudit Jun 03 '21 at 08:30
  • @Deumaudit I doubt that pages can be shared but, actually, I don't know. However, the usual page size (AFAIK) is 4 KB and I'm not sure whether this would be worth the extra trouble of sharing pages. FYI: [SO: What is the page size for 32 and 64 bit versions of windows Os?](https://stackoverflow.com/a/44520278/7478597) – Scheff's Cat Jun 03 '21 at 08:33