Francesco Di Lauro
Francesco Di Lauro

Reputation: 630

Data segment vs stack

A global variable is allocated in the data segment, while a local one stays in the stack. I know that accessing a variable stored in the heap is slower than accessing a local variable, but I don't know if accessing a local variable is faster than accessing a global one. Does it depend on the compiler? Are the differences significant or not?

Upvotes: 3

Views: 5743

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 148900

Stack and head are only implementation details, meaning that they could depend on the compiling environment. The C standard only defines the linkage and storage duration of identifiers. But you are right, stack, heap and data segment are the common implementation.

But you are wrong when you say accessing a variable stored in the heap is slower than accessing a local variable. Allocating and deallocating dynamic memory is indeed more complex and takes more time than using automatic variables, but during their lifetime, accessing (be it for reading or writing) costs exactly the same - at least on common infrastructure. What will make the difference is:

  • is the data in processor cache or in level 2 cache (speeds up access)
  • is the data in a currently swapped off page that needs to be reloaded from disk (slows down access)

But both can happen the same for dynamic, automatic or static data...

Upvotes: 5

Haoran.Luo
Haoran.Luo

Reputation: 113

See also this article before reading on (I am not talking about accessing stack versus heap):

Is accessing data in the heap faster than from the stack?

The architectures and memory management policies vary so vast that it is a hard article to discuss on. I will take Intel x86 as example.

Accessing data is just through a single instruction, no matter where we are accessing.

<INST> <SEG> : <ADDR>

The INST stands for instruction we are executing. The SEG stands for which segment we are accessing. And VADDR stands for the virtual address.

Under real mode, the SEG will be an base address to segment, and ADDR will be the address internal to segment. The efficiency accessing data under real mode seems to be the same amongst every segments. (No matter stack, heap, or global)

Under protected mode, the SEG will be a selector, and ADDR will be the virtual address. And the most tricky thing is that the MMU (Memory Management Unit) comes to work and blames who access data 'not expected'.

When the data you are accessing is not inside memory, the MMU generates a interrupt of page fault and requests OS to switch pages. And the MMU blames you by spending more time swapping pages with hard disk.

So it is just in vain merely talking about whether it is faster accessing global data or local without considering how data is accessed.

From my perspective, you are more likely to access stack 'local data' than global data, so the page fault probability may be higher when accessing global one.

Upvotes: 5

Related Questions