文件范围和静态浮点 [英] file scope and static floats

查看:106
本文介绍了文件范围和静态浮点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的AI项目中,我遇到了一个有趣的问题.我正在尝试格式化一些调试文本,并且发生了一些奇怪的事情.这是一段代码:

I've run into an interesting problem in an AI project of mine. I'm trying to format some debug text and something strange is happening. Here's a block of code:

    float ratio = 1.0f / TIME_MOD;

TIME_MOD是静态浮点,在单独的文件中声明.该值是根据另一个类中的用户输入修改的(我已经验证了该值在调试的同时仍在输入"功能的范围内进行了更改),但是每当在外循环中尝试除以该值时,都会得到相同的数字. (1除以TIME_MOD的初始值).

TIME_MOD is a static float, declared in a separate file. This value is modified based off of user input in another class (I have verified that the value is changed while still debugging within the scope of the "input" function), but whenever I try to divide by it in my outer loop I get the same number. (1 divided by the initial value of TIME_MOD).

我是否缺少有关静态变量和文件范围的内容?

Am I missing something regarding static variables and file scope?

推荐答案

我认为静态"一词有些混乱.我们有一个关键字static,可以在不同的上下文中执行不同的操作,并且我们使用静态"一词来命名存储持续时间"这三类之一.在某些情况下,static不能控制对象的存储时间,而只能控制链接",这可能是造成混乱的主要原因.

I think there is some confusion with the word "static". We have a keyword static that does different things in different contexts and we use the word "static" to name one of three classes of "storage durations". In some contexts static does not control the storage duration of objects but only "linkage" which is probably the main reason for the confusion.

存储期限是对象的属性.

A storage duration is a property of an object.

  • 具有静态存储持续时间的对象的内存仅分配一次.初始化取决于对象的类型及其定义的位置.一旦初始化,它通常会保持活动状态,直到main执行结束.您在全局/命名空间范围内始终声明和定义的对象具有静态存储持续时间.

  • The memory of an object with static storage duration is allocated once and once only. Initialization depends on the kind of object and where it is defined. Once it is initialized, it generally stays alive until the execution of main ends. Objects you declare and define at global/namespace scope always have a static storage duration.

具有自动存储持续时间的对象只能在函数的块内定义.当执行达到定义时,将创建此类对象.这可以发生多次(递归),从而创建多个对象.当执行离开块时,对象将自动销毁.

Objects with automatic storage duration can only be defined inside a block in functions. Such an object is created when execution reaches the definition. This can happen multiple times (recursion) which creates multiple objects. When execution leaves the block the objects are automatically destroyed.

动态分配的对象具有动态存储持续时间.在这种情况下,用户可以通过new,new [],delete,delete []等控制对象的生命周期.

Dynamically allocated objects have a dynamic storage duration. In this case the user controls the life-time of the objects via new, new[], delete, delete[] etc.

内部与外部的联系是关于跨翻译单位的名称可见性.如果使用外部链接声明某些内容,则引入一个名称,该名称可以在其他翻译单元中使用,也可以引用同一实体,只要这些其他TU包含正确的声明(通常包含在头文件中)即可.如果您定义带有内部链接的内容,则不能从其他翻译单元按名称访问它.甚至可以定义多个具有相同名称的实体(每个TU一个),只要您拥有不超过一个的具有外部链接的实体即可.

Internal vs external linkage is about visibility of names across translation units. If you declare something with external linkage you introduce a name that can be used in other translation units as well to refer to the same entity as long as those other TUs contain the proper declaration (usually contained in a header file). If you define something with internal linkage you can't access it from another translation unit by name. You can even define multiple entities with the same name (one per TU) as long as you have no more than one with external linkage.

static的效果取决于上下文:

The effect of static depends on the context:

  • 如果在全局/命名空间范围内声明或定义对象,则它始终是具有静态存储持续时间"的对象.在全局/命名空间范围内使用关键字static完全不会影响存储时间.相反,它影响链接.它声明实体(也可能是函数)具有内部链接.因此,存储类说明符被误用于"做完全不同的事情:强制内部链接.在这种情况下,它与extern相反.在C ++中,您可以使用匿名名称空间来达到相同的效果.与"static"相比,我们建议您使用匿名名称空间来最大程度地减少混乱".

  • If you declare or define an object at global/namespace scope it is always an object with "static storage duration". The use of the keyword static at global/namespace scope doesn't affect the storage duration at all. Instead, it affects linkage. It declares the entity -- which might be a function as well -- to have internal linkage. So, the storage class specifier has been "misused" to do something completely different: enforce internal linkage. It's sort of the opposite of extern in this context. In C++ you can achieve the same effect with an anonymous namespace. You are encouraged to prefer anonymous namespaces over static to "minimize confusion".

static可用于在类范围内声明具有静态存储持续时间的对象.这样的变量只有一个,每个对象都没有.

static at class scope can be used to declare objects with static storage duration in the scope of the class. There's only one such variable and not one for each object.

static可用于声明具有延迟初始化的静态存储持续时间的对象

static at function scope can be used to declare objects with static storage duration that is lazily initialized

如果您说静态变量",则不清楚您的确切意思.您是指静态存储期限"还是内部链接"?

If you say "static variable" it's not clear what you mean exactly. Do you refer to the "static storage duration" or "internal linkage"?

如果要在翻译单元之间共享全局"变量,则必须将其作为带有 external 链接和 define的实体在一个头文件中声明声明 只需一个翻译单元.请注意,未使用关键字static:

If you want to share a "global" variable across translation units you have to declare it in a header file as an entity with external linkage and define it in exactly one translation unit. Note that the keyword static is not used:

// myheader.hpp
extern int k; // declaring an int variable with external linkage

// foo.cpp
#include "myheader.hpp"
int k;        // defining an int variable with external linkage

// bar.cpp
#include "myheader.hpp"
int main() {
    return k;
}

这篇关于文件范围和静态浮点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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