echorashmi
echorashmi

Reputation: 29

What exactly is "memory" in C Programming?

I'm curious to know what "Memory" really stands for.

When I compile and execute this code:

#include <stdio.h>
  
int main(void)
{
   int n = 50;
   printf("%p\n", &n);

}

As we know, we get a Hex output like:

0x7ffeee63dabc

What does that Hex address physically stand for? Is it a part of my computer's L1 Cache? RAM? SSD?

Where can I read more about this, any references would be helpful. Thank you.

Some Background: I've recently picked up learning Computer Science again after a break of a few years (I was working in the Industry as a low-code / no-code Web Developer) and realised there's a few gaps in my knowledge I want to colour in.

In learning C (via CS50x) I'm on the week of Memory. And I realise I don't actually know what Memory this is referring to. The course either assumes that the students already know this, or that it isn't pertinent to the context of this course (it's an Intro course so abstractions make sense to avoid going down rabbit holes), but I'm curious and I'd like to chase it down to find out the answers.

Upvotes: 0

Views: 1248

Answers (6)

John Bode
John Bode

Reputation: 123468

If your code is running under a modern operating system1, pointer values almost certainly correspond to virtual memory addresses, not physical addresses. There's a virtual memory system that maps the virtual address your code sees to a physical address in main memory (RAM), but as pages get swapped in and out that physical address may change.


  1. For desktops, anything newer than the mid-'90s. For mainframes and minis, almost anything newer than the mid-'60s.

Upvotes: 1

0___________
0___________

Reputation: 67546

Memory from the C standard point of view is the objects storage. How does it work and how it is organized is left to the implementation.

Even printing the pointers from the C point of view is pointless (it can be informative and interesting from the implementation point of view) and meaningless.

Upvotes: 0

th33lf
th33lf

Reputation: 2275

Is it a part of my computer's L1 Cache? RAM? SSD?

The short answer is RAM. This address is usually associated with a unique location inside your RAM. The long answer is, well - it depends!

Most machines today have a Memory Management Unit (MMU) which sits in-between the CPU and the peripherals attached to it, translating 'virtual' addresses seen by a program to real ones that actually refer to something physically attached to the bus. Setting up the MMU and allotting memory regions to your program is generally the job of the Operating System. This allows for cool stuff like sharing code/data with other running programs and more.

So the address that you see here may not be the actual physical address of a RAM location at all. However, with the help of the MMU, the OS can accurately and quickly map this number to an actual physical memory location somewhere in the RAM and allow you to store data in RAM.

Now, any accesses to the RAM may be cached in one or more of the available caches. Alternatively, it could happen that your program memory temporarily gets moved to disk (swapfile) to make space for another program. But all of this is completely automatic and transparent to the programmer. As far as your program is concerned, you are directly reading from or writing to the available RAM and the address is your handle to the unique location in RAM that you are accessing.

Upvotes: -1

computer architecture 101

  • In your computer there is a CPU chip and there are RAM chips.
  • The CPU's job is to calculate things. The RAM's job is to remember things.
  • The CPU is in charge. When it wants to remember something, or look up something it's remembering, it asks the RAM.
  • The RAM has a bunch of slots where it can store things. Each slot holds 1 byte. The slot number (not the number in the slot, but the number of the slot) is called an address. Each slot has a different address. They start from 0 and go up: 0, 1, 2, 3, 4, ... Like letterboxes on a street, but starting from 0.
  • The way the CPU tells the RAM which thing to remember is by using a number called an address.
  • The CPU can say: "Put the number 126 into slot 73224." And it can say, "Which number is in slot 97221?"
  • We normally write slot numbers (addresses) in hexadecimal, with 0x in front to remind us that they're hexadecimal. It's tradition.
  • How does the CPU know which address it wants to access? Simple: the program tells it.

operating systems 101

  • An operating system's job is to keep the system running smoothly
  • That doesn't happen when faulty programs are allowed to access memory that doesn't belong to them.
  • So the operating system decides which memory the program is allowed to access, and which memory it isn't. It tells the CPU this information.
  • The "Am I allowed to access this memory?" information applies in 4 kilobyte chunks called "pages". Either you can access the entire page, or none of it. That's because if every byte had separate access information, you'd need to waste half your RAM just storing the access information!
  • If you try to access an address in a page that the OS said you can't access, the CPU narcs to the OS, which then stops running your program.

operating systems 102

  • Remember this shiny new "virtual memory" feature from the Windows 95 days?
  • "Virtual memory" means the addresses your program uses aren't the real RAM addresses.
  • Whenever you access an address, the CPU looks up the real address. This also uses pages. So the OS can make any "address page" go to any "real page".
    These are not official terms - OS designers actually say that any "virtual page" can "map" to any "physical page".
  • If the OS wants a physical page but there aren't any left, it can pick one that's already used, save its data onto the disk, make a little note that it's on disk, and then it can reuse the page.
  • What if the program tries to access a page that's on disk? The OS lies to the CPU: it says "The program is not allowed to access this page." even though it is allowed.
  • When the CPU narcs to the OS, the OS doesn't stop the program. It pauses the program, finds something else to store on disk to make room, reads in the data for the page the program wants, then it unpauses the program and tells the CPU "actually, he's allowed to access this page now." Neat trick!
  • So that's virtual memory. The CPU doesn't know the difference between a page that's on disk, and one that's not allocated. Your program doesn't know the difference between a page that's on disk, and one that isn't. Your program just suffers a little hiccup when it has to get something from disk.
  • The only way to know whether a virtual page is actually stored in RAM (in a physical page), or whether it's on disk, is to ask the OS.
  • Virtual page numbers don't have to start from 0; the OS can choose any virtual page number it wants.

computer architecture 102

  • A cache is a little bit of memory in the CPU so it doesn't have to keep asking the RAM chip for things.
  • The first time the CPU wants to read from a certain address, it asks the RAM chip. Then, it chooses something to delete from its cache, deletes it, and puts the value it just read into the cache instead.
  • Whenever the CPU wants to read from a certain address, it checks if it's in the cache first.
  • Things that are in the cache are also in RAM. It's not one or the other.
  • The cache typically stores chunks of 64 bytes, called cache lines. Not pages!
  • There isn't a good way to know whether a cache line is stored in the cache or not. Even the OS doesn't know.

programming language design 101

C doesn't want you to know about all this stuff.

C is a set of rules about how you can and can't write programs. The people who design C don't want to have to explain all this stuff, so they make rules about what you can and can't do with pointers, and that's the end of it.

For example, the language doesn't know about virtual memory, because not all types of computers have virtual memory. Dishwashers or microwaves have no use for it and it would be a waste of money.

What does that Hex address physically stand for? Is it a part of my computer's L1 Cache? RAM? SSD?

The address 0x7ffeee63dabc means address 0xabc within virtual page 0x7ffeee63d. It might be on your SSD at the moment or in RAM; if you access it then it has to come into RAM. It might also be in cache at the moment but there's no good way to tell. The address doesn't change no matter where it goes to.

Upvotes: 8

Ivan
Ivan

Reputation: 41

In general memory is anything that is stored either temporarily or non-volatile. Temporary memory is lost when the machine is turned off and usually referred as RAM or simply "memory". Non volatile is kept in a hard disk, flash drive, EEPROM, etc. and usually referred as ROM or storage.

Caches are also a type of temporary memory, but they are referred just as cache and is not considered part of the RAM. The RAM on your PC is also referred as "physical memory" or "main memory".

When programming, all the variables are usually in main memory (more on this later) and brought to the caches (L1, L2, etc) when they are being used. But the caches are for the most part transparent for the app developers.

Now there is another thing to mention before I answer your question. The addresses of a program are not necessarily the addresses of the physical memory. The addresses are translated from "virtual addresses" to "physical addresses" by an MMU (memory protection unit) or similar CPU feature. The OS handles the MMU. The MMU is used for many reasons, two reasons are to hide and secure the OS memory and other apps memory from wrong memory accesses by a program. This way a program cannot access nor alter the OS or other program's memory.

Further, when there is not enough RAM to store all the memory that apps are requesting, the OS can store some of that memory in non volatile storage. Using virtual addresses, a program cannot easily know if the memory is actually in RAM or storage. This way programs can allocate a lot more memory than there is RAM. This is also why programs become very slow when they are consuming a lot of memory: it takes a long time to bring the data from storage back into main memory.

So, the address that you are printing is most likely the virtual address.

You can read something about those topics here:

https://en.wikipedia.org/wiki/Memory_management_(operating_systems)

https://en.wikipedia.org/wiki/Virtual_memory

Upvotes: 1

Timbo
Timbo

Reputation: 28050

You should think of memory as an abstract mapping from addresses to values, nothing more.

Whether your actual hardware implements it as a single chunk of memory, or a complicated hierarchy of caches is not relevant, until you try to optimize for a very specific hardware, which you will not want to do 99% of the time.

Upvotes: 1

Related Questions