Skip to main content
28 votes

How are variables stored in and retrieved from the program stack?

Storing local variables on a stack is an implementation detail – basically an optimization. You can think of it this way. When entering a function, space for all local variables is allocated somewhere....
Yuval Filmus's user avatar
26 votes

How are variables stored in and retrieved from the program stack?

Having y on the stack doesn't physically prevent x from being accessed, which, as you pointed out, makes computer stacks ...
aebabis's user avatar
  • 361
16 votes

What course in CS deals with the study of RAM, CPU, Storage?

The subject you're describing often goes under the names computer architecture, computer systems, computer organization and design, and the like. One example is Elements of computing systems, based on ...
Yuval Filmus's user avatar
15 votes

How are variables stored in and retrieved from the program stack?

To provide a concrete example of how a compiler manages the stack and how values on the stack are accessed, we can look at visual depictions, plus code generated by ...
julian's user avatar
  • 250
9 votes

How are variables stored in and retrieved from the program stack?

There are two special registers: ESP (stack pointer) and EBP (base pointer). When a procedure is invoked the first two operations are usually ...
fade2black's user avatar
  • 9,837
9 votes

How are variables stored in and retrieved from the program stack?

You are confused because the local variables stored in stack are not accessed with the access rule of stack: First In Last Out, or just FILO. The thing is that the FILO rule applies to function call ...
nglee's user avatar
  • 191
7 votes

What is the time complexity of memory allocation assumed to be?

what would the worst-case complexity be if it didn't have to copy over $n$ items? If it only needed to allocate a buffer with size $O(n)$ when resizing, would that be considered to run $O(1)$ or $O(n)$...
Tom van der Zanden's user avatar
5 votes

Random access memory and data

RAM is cleared after each power cycle. The power-up value of cells depends on the technology. Static RAM content is usually random (there is no preference between 0 and 1, but the same chip may ...
Grabul's user avatar
  • 1,870
5 votes
Accepted

How is the formula for calculation in row/column major obtained?

Row-major order stores the rows of the array one after another in memory. That is, the array a d g j b e h k c f i l is stored as ...
David Richerby's user avatar
5 votes
Accepted

Do modern operating systems use memory control blocks?

This is really a question about memory allocators and doesn't have much to do with operating systems. "Memory control block" does seem to be a DOS specific term for a node in a free list. I'm not ...
Derek Elkins left SE's user avatar
4 votes

How are variables stored in and retrieved from the program stack?

There are many ways to implement local variables by a language runtime system. Using a stack is a common efficient solution, used in many practical cases. Intuitively, a stack pointer ...
chi's user avatar
  • 14.6k
4 votes
Accepted

Row Major Vs Column Major Order: 2D arrays access in programming languages

The way you access the array affects performance. It depends on how the matrix is represented and stored in memory. Often a matrix is stored in row-major order, so that consecutive elements of a row ...
D.W.'s user avatar
  • 162k
4 votes

Why is data fragmentation not possible on main memory (RAM)?

According to https://lwn.net/Articles/211505/, what you imagine should be done by the OS, is already done: Since Linux is a virtual memory system, fragmentation normally is not a problem; ...
Toni's user avatar
  • 41
4 votes
Accepted

Does word addressable memory have more bytes than byte addressable memory?

If you had a byte addressable architecture where pointers have a fixed size, and every or almost every bit pattern is a valid pointer to a distinct byte address, and a word addressable architecture ...
gnasher729's user avatar
  • 30.5k
4 votes
Accepted

Is it theoretically possible to dynamically grow array size in stack memory?

Yes, it is certainly possible. It really depends if you want it enough to pay the price in terms of complication. First of all, I would strongly recommend that your array should have negative indices:...
Martin Kochanski's user avatar
4 votes
Accepted

Buddy system allocator and slab allocator in Linux kernel

A modern operating system manages physical memory as page frames. A page frame can be allocated to a user process or for the kernel to use for its own purposes, such as to allocate its own data ...
Pseudonym's user avatar
  • 22.3k
3 votes

Does word addressable memory have more bytes than byte addressable memory?

How is it possible that tha same ram, i.e given the ram size is 4 GB, would contain different number of bytes depending on byte/ word addressable memory? It isn't. a 4GB amount of ram would either ...
Tom van der Zanden's user avatar
3 votes
Accepted

How does malloc(sizeof(char)) work

Quick note, sizeof(char) is defined to be 1. So you can just simplify to malloc(1). The real answer however is quite boring. ...
orlp's user avatar
  • 13.6k
3 votes

How are variables stored in and retrieved from the program stack?

The easiest idea is to think of variables as fix names for addresses in memory. Indeed, some assemblers display machine code that way ("store value 5 in address i", ...
Peter - Reinstate Monica's user avatar
3 votes

Understanding memory leaks in C/C++

In your second example, you allocate space for a PrimeSet object and initialise it, then you call the countPrimeNumbers function. The PrimeSet object still exists, it still occupies memory, it ...
gnasher729's user avatar
  • 30.5k
3 votes
Accepted

Words in computer's memory

No, it refers to a word in a computing sense. Here, a word is just a unit of data, whatever is natural for a particular processor. For instance, an x86-64 processor, which I'm currently using, has a ...
Juho's user avatar
  • 22.6k
3 votes

Why does a 32 bit address only contain 1 byte, when 32 bits = 4 bytes?

The question is confusingly worded, but if the question is "why a pointer of 32 bits (4 bytes) points to only one byte of storage", that is just how a particular computer architecture (like ...
Steve's user avatar
  • 562
3 votes

How does a CPU jump to a instruction thats no longer in ram?

The program counter holds an address. Period. The CPU fetches an instruction from that address. Period. Most CPUs today have a hardware unit called an MMU (memory management unit) which sits between ...
Quitting Due To Antisemitism's user avatar
3 votes
Accepted

RAM architecture vs. CPU architecture

Computers have had a number of different ways of managing mismatches between bit count and memory size over the years. The general theme is that "pure" n-bit CPUs are rare. Instead, CPUs ...
Mark's user avatar
  • 995
2 votes

How are variables stored in and retrieved from the program stack?

Data items that can go on the stack are put on the stack - Yes! It is a premium space. Also, Once we pushed x into stack and then pushed ...
inquisitive's user avatar
2 votes
Accepted

Why not space out memory allocations?

Why is memory different from a hard disk? Because hard disks have different performance characteristics than RAM. With hard disks, seeking is extremely slow. Sequential reads are much faster than ...
D.W.'s user avatar
  • 162k
2 votes

Row Major Vs Column Major Order: 2D arrays access in programming languages

It's a completely arbitrary decision. In Fortran, for example, "people" prefer column-major order, whereas in C "they" prefer row-major order. In C the row-major order is forced due to reasons of ...
Yuval Filmus's user avatar
2 votes
Accepted

Less used data (1 byte) in memory address holding 4 bytes

So your computer has 32 bits as the smallest addressable unit. Your compiler has two choices: It can store var1 and var2 in two different sets of 8 bits in the same 32 bit unit, or it can store var1 ...
gnasher729's user avatar
  • 30.5k
2 votes

If you increase the address space for an OS, does the Phys Mem used by the program increase?

No. Due to the way virtual memory works, you can have more virtual pages than physical pages (frames). Some virtual pages might not be mapped to any physical page. It doesn't really make sense to ...
D.W.'s user avatar
  • 162k

Only top scored, non community-wiki answers of a minimum length are eligible