datastructure_c_localmemory

/*

一直有个疑问变量如何和值相联系的,在机器的层次上,之前看看Berkeley 61a有说namespace 是按照index-value 模式,但还是不能从内存或者编译上面去理解,stackflow 看了下

How are variable names stored in memory in C?  的答案,果然需要很多汇编和编译的基础知识,先不要再挖坑了吧,要不然感觉一路走不知道到哪去.............

 */

/*
2017-02-21 更新

变量在运行前,就被解释为地址,有一张符号表,记录每个变量对应关系,而值,即value,在机器层次上,以00100010这种形式存在,在相应内存中的地址为变量名对应地址,暂时是这么理解的,好像还需要组成和系统两门课后才能理解更好点,暂时就这样吧。*/

 What and why exist?

  • Variables represent storage space in the computer's memory. Each variable presents a convenient names like length or sum in the source code. //represent and abstraction for any language
  • Behind the scenes at runtime, each variable uses an area of the computer's memory to store its value. // at runtime, no names ,just address
  • It is not the case that every variable in a program has a permanently assigned area of memory. Instead, modern languages are smart about giving memory to a variable only when necessary. The terminology is that a variable is allocated when it is given an area of memory to store its value.// efficient
  • A variable is deallocated when the system reclaims the memory from the variable, so it no longer has an area to store its value.
  • For a variable, the period of time from its allocation until its deallocation is called its lifetime.

 

How?

  • The most common variables you use are "local" variables within functions.
  • The variables are called "local" to capture the idea that their lifetime is tied to the function where they are declared.
  • When a function is called, memory is allocated for all of its locals. In other words, when the flow of control hits the starting '{' for the function, all of its locals are allocated memory.
  • Parameters count as locals. The only difference between parameters and local variables is that parameters start out with a value copied from the caller while local variables start with random initial values.
  • The memory for the locals continues to be allocated so long as the thread of control is within the owning function. Locals continue to exist even if the function temporarily passes off the thread of control by calling another function.
  • a website to understand the process  online C tutor

Advantages and Disadvantages?

  • Convenient. Local variables conveniently provide this sort of temporary, independent memory. //及时即用
  • Efficient. they are space efficient in the way they use and recycle memory. // 利用率高
  • Local Copies. This is also known as "pass by value." Parameters are local variables which are initialized with an assignment (=) operation from the caller. The caller is not "sharing" the parameter value with the callee in the pointer sense— the callee is getting its own copy.
  • Short Lifetime. Sometimes a program needs memory which continues to be allocated even after the function which originally allocated it has exited. This problem will be solved later with "heap" memory.
  • Restricted Communication.

 

posted @ 2017-02-17 15:42  lixinnjupt  阅读(96)  评论(0编辑  收藏  举报