0

I'm trying to solve the following problem:

"Write a C program that multiplies an n x n matrix by an n-element vector. Choose n as large as your memory allows."

The matrix multiplication is very easy to implement and you can find enough information on the internet, but I have no idea how to figure out the maximum allocable size of my memory.

The first thing thTat crossed my mind was to do a dynamic memory allocation with the functions malloc() and realloc() and see how much it is possible.

int main() {  
    int n = 0;
    float** first = (float**) malloc(sizeof(float*));

    while(1) {
        next** = (float**) realloc(first, n * sizeof(float*));
        n++;

        if (next == NULL) {
            break;
        }
    }   
}

The problem is, that Windows 10 is using virtual RAM and stuff, so this method results in an endless loop.

Any Ideas ?

Rabobsel
  • 3
  • 2
  • Compile your code as 32 bit program, then you should run out of memory pretty quickly. A 32 bit program cannot address more than 4Gb of memory virtual memory or not. – Jabberwocky Nov 08 '19 at 17:02
  • .. or limit the maximum memory size allocated by your program artificially to the size of your RAM (say 8 Gb or whatever). – Jabberwocky Nov 08 '19 at 17:04
  • 1
    *Choose n as large as your memory allows* That's a horrible, poorly-thought-out condition. Try that on a default Linux installation and your `malloc()`/`realloc()` will succeed, but when you try to actually use the memory the OOM killer will start killing processes on the system. – Andrew Henle Nov 08 '19 at 17:08
  • Related question [link](https://stackoverflow.com/a/2360186/12114801) – Sathvik Nov 08 '19 at 17:20
  • @Rabobsel check out what is the biggest integer available on your platform is and use that. I believe windows must have INT_MAX or something defined in one of it's header files.Check that out and use that and see how it goes. AFAIK, this will create a lot of memory pressure but I am not sure how the windows kernel will handle that. I believe it won't allow a user space program like this to hog the CPU completely and render the machine unresponsive (like prev versions of windows). It should have something similar to the Linux OOM killer. But you still have to check that yourself. – AjB Nov 08 '19 at 18:13
  • See [Why is malloc not “using up” the memory on my computer?](https://stackoverflow.com/q/19991623/2410359). – chux - Reinstate Monica Nov 08 '19 at 19:14
  • 1
    @Jabberwocky 32-bit code can allocated more than 4Gb and more than 8, 16, Gb, etc. – chux - Reinstate Monica Nov 08 '19 at 19:15
  • My vanilla Windows 10 installation at work has a "nice" feature. When some programs allocate and use so much memory that virtual memory will be used, the systems freezes totally. Nothing helped until we (the development and the IT departments) decided to set the size of virtual memory to zero. I'd not use the approach "allocate as long as it goes" with such an instable operating system. – the busybee Nov 08 '19 at 19:22
  • Are you sure _"Choose n as large as your memory allows"_ should be interpreted in that way? It seems likely it simply means choose a large value of n that will use most available space. For performance reasons, you might reasonably assume it means physical RAM. The critical word here is "choose" not "determine". Say you have 4Gb physical RAM and Task Manager says that 2Gb is available then n might reasonably "choose" `n = sqrt(2 * 1024 * 1024) / sizeof(float))` - as an estimate of a value that might utilise most available physical RAM. – Clifford Nov 08 '19 at 20:15
  • @chux-ReinstateMonica how can that be? – Jabberwocky Nov 11 '19 at 07:45
  • @Jabberwocky "32-bit code" refers to the processor's native integer processing size, not its memory range. E.g. [Is there way to enable more than 4 GB RAM in 32-bit Windows OS?](https://superuser.com/questions/67444/is-there-way-to-enable-more-than-4-gb-ram-in-32-bit-windows-os). 16-bit processors for decades have worked with more than 64k memory as well as 8-bit ones using more than 256 bytes. – chux - Reinstate Monica Nov 11 '19 at 08:02
  • @chux-ReinstateMonica but a single 32 bit process cannot address more then 4Gb of memory. – Jabberwocky Nov 11 '19 at 08:17
  • @Jabberwocky Are you of the mind that a pointer bit width cannot exceed the bit width of the native integer size and that C requires that? – chux - Reinstate Monica Nov 11 '19 at 08:28
  • @chux-ReinstateMonica it's not a matter of the C language, bujt it's about 32 bit programs that cannot address more than 4Gb because they only have 32 bit registers. – Jabberwocky Nov 11 '19 at 09:14
  • @Jabberwocky Sorry I cannot convince you of the possibilities of page registers, address registers wider than the 32-bit width and other architectures. Although it is common to limit processors address to the native integer bit width, it is by no means an industry requirement. – chux - Reinstate Monica Nov 11 '19 at 14:13
  • @chux-ReinstateMonica I didn't say it's an industry requirement, but the fact is that at least in Windows (maybe in Linux too, not totally sure), a 32 bit a user process cannot address more than 4Gb of memory. – Jabberwocky Nov 11 '19 at 14:16

0 Answers0