-3

i have question about C language (memory). this is my source

#include <unistd.h>

int main() {

    char ___storage___[1073741824];

    sleep(30);

    return 0;
}

RAM Usage : 10 Bytes
when i run this program i expected this program get 1 GB from my pc ram for 30 seconds. but it's get nothing from my PC ram !!! but for example if i copy the characters into this array like this

#include <stdio.h>
#include <unistd.h>

int main() {

    char ___storage___[1073741824];

    for (int i = 0; i < 536870912; i++) // 512 Mb characters !
        ___storage___[i] = 'h';

    sleep(30);

    return 0;
}

RAM Usage : 512 MB
for this program when i run it, this program get 512 MB of my ram ! but i declared a variable with (1GB) size ! why ? if this get our PC ram only when we insert something inside it, why we have dynamic variables !? for example we give a dynamic variable high range and insert inside it with out dynamic allocation or reallocation !

Filip Vondrášek
  • 1,208
  • 1
  • 13
  • 26
itmanwork
  • 81
  • 1
  • 1
  • 7
  • How are you measuring the application RAM usage? – Mark Apr 23 '18 at 14:09
  • windows task manager – itmanwork Apr 23 '18 at 14:10
  • Why should the OS map you memory you don't use? And in the first case, why should the C compiler even include a variable you never use? In fact, a compiler could even optimize away your whole array in the second program ... just writing a value to a (non-volatile) location hasn't any observable behavior if it's never used. –  Apr 23 '18 at 14:11
  • even i use it, it's get 512 Mb (what ever i put character inside it) ... not 1 Gb ... i excpted to get 1Gb (what i set on declare) – itmanwork Apr 23 '18 at 14:12
  • 1
    well same thing, you only "use" half of it (and an aggressive optimizer would conclude you "use" none at all, see comment above). On a side note, identifiers starting with underscore are reserved for the implementation, so this isn't correct C after all. –  Apr 23 '18 at 14:15
  • Uhm @Lundin, this isn't the duplicate OP is looking for (but I see a stackoverflow around the corner anyways)... –  Apr 23 '18 at 14:17
  • 1) Compilers optimize code and 2) There's probably not enough stack memory for you, see the linked dupe. – Lundin Apr 23 '18 at 14:17
  • @FelixPalmen I don't think we have a duplicate for "what is an optimizing compiler and how does it work" since that would probably be too broad a topic. – Lundin Apr 23 '18 at 14:19
  • 1
    @Lundin and I don't mind this Q to be closed, but the duplicate is about a different issue, so this could be a bit confusing ... –  Apr 23 '18 at 14:20

1 Answers1

0

Most compilers allocate local variables in the stack, however most operating system limit stack size to something reasonable, like in the tens to hundreds of megabytes. So let's assume it does some compiler magic is hide this limit and isn't in the stack but on the heap.

Long story short your program doesn't get physical RAM until it actually attempts to read or write for most things. This causes a pages fault which gets handled by a component in the operating system called a virtual memory manger. This then maps RAM to a segment of your address space (program memory) in a unit called a page. Pages vary in size based on processor architecture, however for AMD64 it uses 4KB or 4MB page sizes, depending on what the operating system would like to do.

Mark
  • 1,128
  • 13
  • 21