0

We know that pointers in C are used to store memory address of anything but what memory is this? I mean that is it RAM or hard disk? For variable i can conclude that it must be RAM because variables exist only when the program is running. But what about functions? If I make a pointer that points to a function then it must point to the address of the function in the hard disk because the functions exist even when the program is not running. If this is so then how can we know by looking at the memory address that whether it corresponds to RAM or the hard disk?

And also, for example if I have a program that occupies 10MB in hard disk and when I run the program, first of all the program is loaded into RAM. Do the program occupies 10MB in RAM also? If not then what factors decide the memory occupied by the program in RAM?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • does [this answer](http://stackoverflow.com/a/34463661/2173917) help? – Sourav Ghosh Apr 18 '17 at 07:29
  • "*first of all the program is loaded into RAM*" Not on most modern OSes. – David Schwartz Apr 18 '17 at 07:29
  • This has nothing to do with the language tags, removed them. – Sourav Ghosh Apr 18 '17 at 07:30
  • Pointers always point to somewhere in memory (RAM). The only difference is that they could be on the stack (working memory for the function call stack) or on the heap (allocated memory for your entire program). I'd presume that function pointers are actually handled via the heap, since calling a pointer outside your allocated heap memory causes a segmentation fault at the OS level. – Jason Lang Apr 18 '17 at 07:31
  • 1
    @JasonLang Nonsense. Pointers always point to somewhere in virtual memory. They may or may not point to RAM. (I can mmap a 12GB file on a system with 4GB of RAM and then form pointers to every byte of that file. You can't tell me those pointers must point to RAM -- there aren't 12GB of RAM on the machine.) – David Schwartz Apr 18 '17 at 07:34

2 Answers2

2

The pointers point into the process' virtual address space. Addresses may be backed by RAM, disk, neither, or both.

For example:

  1. You allocate some memory with malloc. It's probably backed by nothing at this point.

  2. You write to that memory. Now it's probably backed by RAM.

  3. You memory map a file. It's backed by the file at this point.

  4. You read from that memory. Now it's backed by both the file and RAM.

  5. The OS would like some more RAM for the disk cache and you haven't accessed that memory in a while, so it drops the pages you read and now your memory mapping is backed only by the file again.

  6. The OS steals the pages that were backing the memory you allocated with malloc and wrote to, now that memory is backed by only the paging file.

And also, for example if I have a program that occupies 10MB in hard disk and when I run the program, first of all the program is loaded into RAM.

Not on most modern OSes you are likely to use. Instead, 10MB of virtual address space is allocated and backed by the executable. The pages will be read into RAM (and pages of RAM will be allocated) as they are accessed.

Do the program occupies 10MB in RAM also?

Not yet. But RAM will be needed as the pages are accessed.

If not then what factors decide the memory occupied by the program in RAM?

It's a decision the OS has to make based on what provides the best performance for the system overall. You can think of the RAM as a cache with the OS moving things into and out of RAM as RAM is available and as data is accessed or not accessed.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
1

Pointers point to some memory, true, agreed. The memory it points to, well, is usage / environment dependent.

To elaborate, typically for a user-space program, pointers point to some address in the virtual address space.

On the other hand, for low-level system programming (in kernel, for example) and in case of OSs without MMU, it points to physical memory.

I mean that is it RAM or hard disk?

Either, both, none. It all depends. In general, at the program execution beginning, each process is allocated with a virtual address space. It's the job of the MMU/OS to make sure the required "actual" memory is available "upon request". Also, this includes paging concept.

Copying from wikipedia,

           0                                            4GB
VAS 1      |---vvvv-------vvvvvv---vvvv----vv---v----vvv--|
mapping        ||||       ||||||   ||||    ||   |    |||
file bytes     app1 app2  kernel   user   system_page_file
mapping             ||||  ||||||   ||||       ||   |
VAS 2      |--------vvvv--vvvvvv---vvvv-------vv---v------|

it shows,

  • The virtual address space for process "app1"
  • The virtual address space for process "app2"
  • The virtual address space for kernel processes
  • The virtual address space for userspace libraries
  • The virtual address space mapping for page files, which is used to access the virtual memory.
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261