在内存静态和全局变量 [英] Static and global variable in memory

查看:110
本文介绍了在内存静态和全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 是否存储类似于全局栈本身的静态变量?如果是这样,他们如何保护,只允许本地级接入?


  2. 在一个多线程上下文是,该存储器可以被其他线程/内核直接访问的恐惧?或为什么不能使用静态/全球多进程/线程enviornment?



解决方案

存储在堆栈上的变量在本质上的时间。他们属于一个功能等,当函数返回和相应的堆栈帧被弹出,堆栈变量与它消失。由于全局被设计成访问无处不在,它们必须不出去上下文,并因此被存储在堆(或在二进制的一个特殊的数据段),而不是在栈上。这同样适用于静态变量;因为他们必须持有的函数调用之间的价值,他们不能当函数返回因此它们不能在栈上分配消失。

至于静态变量保护得好,IIRC这主要是由编译器完成。即使变量是在堆中,你的编译器知道有限的情况下,其中的变量是有效的,任何试图访问静态从上下文之外将导致未知标记或类似的错误。唯一的其他方式来访问堆变量是不正确的,如果你知道静态的地址,你一味地去参考指针。这应导致一个运行时间存贮器存取错误

在多线程环境中,仍然好于使用全局和静态变量。但是,你必须有很多更加小心。必须保证只有一个线程可以在一个时间访问变量(通常是通过某种锁定机构如互斥)。在的情况下,静态函数内部的局部变量,你必须确保你的函数仍然会按预期如果从连续多个线程(即从线程调用称为1,然后从线程2,那么线程1,则线程2等等)。这通常是很难做的,依赖于静态成员变量不是线程安全的,因为这( strtok的是一个明显的例子)。

  1. Are static variables stored on the stack itself similar to globals? If so, how are they protected to allow for only local class access?

  2. In a multi threaded context, is the fear that this memory can be directly accessed by other threads/ kernel? or why cant we use static/global in multi process/ thread enviornment?

解决方案

Variables stored on the stack are temporal in nature. They belong to a function, etc and when the function returns and the corresponding stack frame is popped off, the stack variables disappear with it. Since globals are designed to be accessible everywhere, they must not go out of context and thus are stored on the heap (or in a special data section of the binary) instead of on the stack. The same goes for static variables; since they must hold their value between invocations of a function, they cannot disappear when the function returns thus they cannot be allocated on the stack.

As far as protection of static variables goes, IIRC this is mainly done by the compiler. Even though the variable is on the heap, your compiler knows the limited context in which that variable is valid and any attempt to access the static from outside that context will result in an "unknown identifier" or similar error. The only other way to access the heap variable incorrectly is if you know the address of the static and you blindly de-reference a pointer to it. This should result in a run-time memory access error.

In a multi-threaded environment, it is still okay to use globals and static variables. However, you have to be a lot more careful. You must guarantee that only one thread can access the variable at a time (typically through some kind of locking mechanism such as a mutex). In the case of static local variables inside a function, you must ensure that your function will still function as expected if it is called from multiple threads sequentially (that is, called from thread 1, then from thread 2, then thread 1, then thread 2, etc etc). This is generally harder to do and many functions that rely on static member variables are not thread-safe because of this (strtok is a notable example).

这篇关于在内存静态和全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆