0

I am new to computer programming. I was studying about variables and came across a definition on the internet:

Variables are the names you give to computer memory locations which are used to store values in a computer program.

What are these memory locations? Do these locations refer to the actual computer memory or this is just a dump in the program itself from where it calls those variables later when we need them? Also there are other terms that I encountered here on stack overflow like heap and stack. I could not get my head around these. Please help.

Jay
  • 9
  • 2
  • 1
    It depends on the storage class of the variable. – Kerrek SB Jun 29 '16 at 09:45
  • It also depends on where you define the variables. And possibly on if they are initialized at the definition or not. – Some programmer dude Jun 29 '16 at 09:46
  • 1
    read about heap & stack here: http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap – CIsForCookies Jun 29 '16 at 09:47
  • [This](http://electronics.stackexchange.com/questions/237740/what-resides-in-the-different-memory-types-of-a-microcontroller/237759#237759) might help. It is about microcontrollers, but the same applies pretty much universally, with the difference that PC computers have no flash. – Lundin Jun 29 '16 at 10:48
  • @noob: Neither stack nor heap are mandated by the C standard. And there are platforms which have none at all for variables. – too honest for this site Jun 29 '16 at 11:29

1 Answers1

0

The way you've asked the question suggests you expect a single answer. That is simply not the case.

In a rough sense, all variables will exist in memory while your program is being executed. The memory your variables exist in depends both on several things.

Modern computer hardware often has quite a complex physical memory architecture - with multiple levels of cache (in both the CPU, and various peripheral devices), a number of CPU registers, shared memory, different types of RAM, storage devices, EEPROMs, etc. Different systems have these types of memory - and more types - in different proportions.

Operating systems may make memory available to your program in different ways. For example, it may provide virtual memory, using a combination of RAM and reserved hard drive space (and managing mappings, so your program can't tell the difference). This can allow your program to use more memory than is physically available as RAM, but also affects performance, since the operating system must swap memory usage of your program between RAM and the hard drive (which is typically orders of magnitude slower).

A lot of compilers and libraries are implemented to maximise your programs performance (by various measures) - compiler optimisation of your code (which can cause some variables in your code to not even exist when your program is run), library functions crafted for performance, etc. One consequence of this is that the compiler, or library, may use memory in different ways (e.g. some implementations may embed code in your executable to detect memory resources available when the program is run, others may simply assume a fixed amount of RAM), and the usage may even vary over time.

Peter
  • 35,646
  • 4
  • 32
  • 74
  • Most modern computers don't have a cache, nor exessive RAM. Most comouters have a MCU with very limited RAM/Flash. They outnumber the computers with full-growm OS by decades. – too honest for this site Jun 29 '16 at 11:31