定义一个静态数组到C或C ++源文件 [英] Defining a static array into a C or C++ source file

查看:159
本文介绍了定义一个静态数组到C或C ++源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是每个程序员都应该知道的问题,但我不知道。很久没有C语言编程,我已经忘记了很多事情。

I know this is a question that every programmer should know, but I do not know. Long time no C programming and I've forgotten a lot of things.

我的问题是:

我有一个头文件中定义了三个巨大的静态数组。有人告诉我,这是更好的向他们声明为在头文件的extern ,并在单个C或C ++源文件中定义它们。

I have three huge static arrays defined inside a header file. Someone told me that It's much better to declare them as extern in the header file, and define them in a single C or C++ source file.

我怎么能这样做?

下面是我的头文件:

#ifndef _TEMPLE_OBJECT_H_
#define _TEMPLE_OBJECT_H_


#define NUM_TEMPLE_OBJECT_VERTEX 10818

static const float TEMPLEVertices[NUM_TEMPLE_OBJECT_VERTEX * 3] = {...};
static const float TEMPLENormals[NUM_TEMPLE_OBJECT_VERTEX * 3] = {...};
static const float TEMPLETexCoords[NUM_TEMPLE_OBJECT_VERTEX * 3] = {...};

#endif

如果一个使用C ++源文件,我可能需要定义一个类?

If a use a C++ source file, may I need to define a class?

更新:
我认为这个问题是:

在这些头都包含(即使是间接的)每个源文件都将产生自己的那些静态数组的定义。有没有保证,编译器/连接器将优化成一个单一的定义,即使是在源文件,他们是不用的。事实上,在很多情况下,编译器不能优化他们离开。这可能会导致您的静态数据占用大量的磁盘空间,并且可能运行时内存。

UPDATE:
I think the problem is:
Every source file in which those headers are included (even indirectly) will generate its own definition for those static arrays. There's no guarantee that the compiler/linker will optimize them into a single definition, even in source files where they're unused. In fact, in many cases the compiler cannot optimize them away. This could result in your static data consuming a lot of disk space, and possibly runtime memory as well.

感谢您。

推荐答案

静态的extern 在同一时间没有意义。 静态在文件范围使得阵列被其他文件无法访问,而的extern 告诉你的数组定义别的地方编译

static and extern at the same time makes no sense. static at file scope makes the array inaccessible by other files, while extern tells the compiler that your array is defined somewhere else.

您可以做什么321008表明,除非你不声明你的阵列静态的这是非法的C和C ++。这给了你,你可以使用的地方是包含头文件三个全局变量。

You can do what 321008 suggests, except that you don't declare your arrays static which is illegal C and C++. That gives you three global variables that you can use wherever the header file is included.

例如这样的:

// .h file:

extern const float TEMPLEVertices[];

// .cpp (or .c) file:

const float TEMPLEVertices[] = { 1.0, 2.0, 5.6 /* or whatever*/ };

或者,你可以做什么建议FORTRAN,但是这会给你的文件范围只能访问,而不是全局变量。

Or you can do what fortran suggests, but that will give you file-scope access only, not global variables.

您做的的任何方式,如果您使用C ++源文件来定义一个类。与Java,C ++不会强迫你进入一个面向对象的设计(无论是好的大概可以讨论,但无论如何)。

You do not in any way have to define a class if you use a C++ source file. Unlike Java, C++ does not force you into an object oriented design (whether or not that is good can probably be discussed, but anyway).

编辑:至于你的问题的更新,那是因为你将它们定义为静态。如果你只是想全局变量,你不应该这样做,而是保持一个单一的定义(常量浮动)和引用它的extern ,按我上面的例子。

As for your question update, that is because you define them as static. If you only want global variables, you should not do that, but instead keep one single definition (const float) and reference it with extern, as per my example above.

这篇关于定义一个静态数组到C或C ++源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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