难道持久化变量没有被广泛使用?我无法在网上或我的 C 教科书索引中找到太多关于它们的信息 - C 的艺术与科学。
任何您可以分享的有关它们的信息,尤其是它们的范围和示例声明 都会有所帮助。我猜想声明它们您使用“持久”作为关键字?
static void foo( void ) {
persistent unsigned int width = 5;
}
这是我能找到的唯一有用的引用资料: “当电路板关闭和打开、运行主程序以及发生系统重置时,持久变量会保持其状态。当由于加载或卸载文件而下载代码时,持久变量将丢失其状态。” http://www.newtonlabs.com/ic/ic_5.html#SEC9
谢谢!
最佳答案
交互式 C(您提供的链接)提供了 persistent
关键字,但不是标准 C。特别是因为像“在电路板关闭和打开、运行主程序以及发生系统重置时保持它们的状态”这样的保证。
persistent
随交互式 C 编译器一起提供,并与专用硬件(在本例中为摩托罗拉芯片)一起工作,将变量值存储在非 volatile 内存中以实现重启后的持久性。
Interactive C is a C compilation environment for many Motorola 6811 based robots and embedded systems. Originally developed for the MIT LEGO Robot Design Contest (6.270), Interactive C has enjoyed widespread distribution and use. Interactive C's claim to fame is its interactivity: users can type in expressions and have them compiled on the fly and run immediately, rather than waiting for lengthy compile and download cycles. IC currently supports the 6.270, the HandyBoard and the RugWarrior and RugWarrior Pro. source.
要在局部作用域(例如函数)中实现变量持久化,请使用 static
关键字。
关于c - c中的持久局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4357282/