Search code examples
pointersansi-c

What type of memory is pointer pointing to in c?


I am trying to understand pointers more deeply and I got to a situation where I don't know to what type of memory do the pointers point to. Do I understand it correctly that if the pointers point to dynamically allocated variables through malloc() or calloc() then the pointer points to RAM and if there are static arrays or some variables then the pointer points to the memory (SSD/HDD)?


Solution

  • No. Conceptually, all memory is RAM, borrowed from the OS which manages it (if there is an OS). The difference between statics/globals and dynamic memory is that statics/globals are designed to never be returned to the OS until the program exits/dies whereas dynamically allocated memory (malloc/calloc/mmap) is conceptually returnable, which is what free/munmap is for.

    (Note that when you free malloc'd/calloc'd memory, you only return in to your C standard library, which returns it to the OS at its own discretion (if it does at all).)