有关数组的问题 [英] questions about arrays

查看:68
本文介绍了有关数组的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Q1:

如果一个数组在文件A中被声明为静态,那么从文件B访问它是否仍然有效



我的意思是如果函数表单文件A返回指向数组内部的

位置的指针从文件B中调用可以函数

in文件B使用这个指针读/写数组?

我知道这个有用,并且我用过的每个编译器都没有产生警告这种行为是否正确?

这个问题可以用不同的方式说明:静态是什么?确实

为阵列分配的内存区域为静态?

Q2:

我可以保存/恢复阵列到/从像这样的磁盘:


int my_array [100];


fwrite(my_array,sizeof my_array,1,out_file);


fread(my_array,sizeof my_array,1,in_file);


再一次,它对我来说非常好,但是它是

有效/便携吗?

copx


Q1:
If an array is declared static in file A is it still valid to access
it from file B?
I mean if a function form file A which returns a pointer to a
position inside of the array is called from file B can functions
in file B read/write to the array using this pointer?
I know that this works and doesn''t produce a warning with
every compiler I''ve used but is this behaviour correct?
The question could be stated differently: does "static" really
make the memory area allocated for the array "static"?
Q2:
Can I save/restore an array to/from disk like this:

int my_array[100];

fwrite(my_array, sizeof my_array, 1, out_file);

fread(my_array, sizeof my_array, 1, in_file);

Again, it works mighty fine for me but is it
valid/portable?
copx

推荐答案

copx写道:
Q1:我的意思是如果一个函数形式文件A返回一个指向
位置的指针从文件B中调用数组可以使用这个指针在文件B中读取/写入数组吗?
我知道这有效并且不会产生警告
每个我用过的编译器,但这个行为是否正确?
问题可能是s不同的是:静态的是什么?真的
为数组静态分配内存区域?


第一种可能性:你在谈论使用静态给合格的对象提供静态的

存储持续时间,并指的是


/ * file_A.c * /

.. .....

T * ProvidePtr(无效)

{

静态T阵列[MANY];

返回数组;

}


/ * file_B.c * /

...... 。

T * ptr;

ptr = ProvidePtr();


其中T是合适的类型而MANY是#define ''d和> 0。


这确实是标准的,便携式的,你想要的。


第二种可能性:你在说话关于在文件范围内使用的静态;

在这种情况下,阵列已经具有静态存储持续时间和

将具有外部链接(整个可见性的全局可见性) b $ b所有模块)但由于静态链接是内部的(即它只有文件A中可见
)。


/ * file_A.c * /

.......

静态T数组[MANY];

T * ProvidePtr (无效)

{

返回数组;

}


也有效,是合法的一切 - 由于静态存储

持续时间,数组存在整个程序的整个生命周期

并且可以使用来自任何地方的指针访问(这个

在两种情况下都有。)

顺便说一句:你也可以使用它将函数指针传递给函数

,内部链接。


Q2:
我可以像这样在磁盘上保存/恢复数组:

int my_array [100];

fwrite( my_array,sizeof my_array,1,out_file);

fread(my_array,sizeof my_array,1,in_file);

再一次,它对我来说非常好但是它<有效/便携?
Q1:
If an array is declared static in file A is it still valid to access
it from file B?
I mean if a function form file A which returns a pointer to a
position inside of the array is called from file B can functions
in file B read/write to the array using this pointer?
I know that this works and doesn''t produce a warning with
every compiler I''ve used but is this behaviour correct?
The question could be stated differently: does "static" really
make the memory area allocated for the array "static"?
First possibility: You are talking about using "static" to give static
storage duration to the qualified object and are referring to

/* file_A.c */
.......
T *ProvidePtr (void)
{
static T array[MANY];

return array;
}

/* file_B.c */
.......
T *ptr;
ptr = ProvidePtr();

where T is an appropriate type and MANY is #define''d and >0.

This is indeed standard conforming, portable and what you wish.

Second possibility: You are talking about static used on file scope;
in this case, the array already has static storage duration and
would have external linkage (sort of global visibility throughout
all modules) but due to static the linkage is internal (i.e. it
is only visible within file A).

/* file_A.c */
.......
static T array[MANY];
T *ProvidePtr (void)
{
return array;
}

also works, is legal and everything -- due to the static storage
duration the array exists throughout the life time of the program
and can be accessed using a pointer from about everywhere (this
holds in both cases).
BTW: You can also use this to pass function pointers to functions
with internal linkage.

Q2:
Can I save/restore an array to/from disk like this:

int my_array[100];

fwrite(my_array, sizeof my_array, 1, out_file);

fread(my_array, sizeof my_array, 1, in_file);

Again, it works mighty fine for me but is it
valid/portable?




它有效但不便携。即使是不同的实现,你的平台也可能对表示形式有不同的想法,并导致不兼容的二进制文件。

如果你想要使用可移植的二进制文件,你必须定义一个文件格式并将整数转换为该格式。

示例:磁盘上的每个字节应存储8位(如反对某个机器上的
CHAR_BIT位),整数存储在2s补码中,

小端,用完16位。然后你编写你的转换

例程,并希望在所有平台上都能幸福地生活。


文本文件通常更强大,可以操作和

轻松生成 - 轻松检查。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



It is valid but not portable. Even different implementations on
your platform might have different ideas about the representation
of ints and lead to incompatible binary files.
If you want to use binary files which are portable, you have to
define a file format and convert the ints into that format.
Example: Every Byte on disk shall store 8 bits (as opposed to
CHAR_BIT bits on a certain machine), ints are stored in 2s complement,
little endian and use up 16 bits. Then you write your conversion
routines and hope to live happily ever after on all platforms.

Text files are usually more robust, open to manipulations and
easy generation -- and easy checking.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


2005年3月26日星期六09:07:36 +0100,copx

< in ***** @ invalid.com>写道:
On Sat, 26 Mar 2005 09:07:36 +0100, copx
<in*****@invalid.com> wrote:
Q1:
如果一个数组在文件A中被声明为静态,它仍然有效从文件B访问它吗?
我的意思是如果一个函数表单文件A返回一个指向数组内部
位置的指针从文件B中调用,那么文件B中的函数可以使用这个指针读/写到数组吗?


是的。您还可以在

函数中使用delared静态函数:


#include< stdio.h>

# include< limits.h>


const char * to_binary(无符号值)

{

static char str [sizeof(值)* CHAR_BIT + 1];

int i;

/ *用二进制值填充str,nul终止* /

return str;

}


int main(无效)

{

printf("%s \\ \\ n,to_binary(42));

返回0;

}

我知道这有效并且不会产生我用过的每个编译器都警告过这个行为是否正确?
问题可以用不同的方式说明:静态是什么?真的
为数组静态分配内存区域?


''静态'是什么意思? C以两种不同的方式使用它:


(在函数内)内存不会在函数调用之间重复使用

(与''自动'对比函数中的变量。


(在文件范围内)变量名称的可见性仅持续

直到翻译单元结束。 br />

我上面的代码说明了第一个特性,

函数中的'str''将保留其值,而不是在to_binary时重复使用( )退出,

而(自动)变量''i'将在

to_binary()退出后具有未定义的值。


第二个定义意味着翻译单元没有任何东西可以看到变量的/ name /,但没有什么能阻止该单元中的函数返回它/地址/到一个调用函数,

然后可以通过该地址访问它。

Q2:
我可以保存/恢复一个数组到/来自这样的磁盘:

int my_array [100];

fwrite(my_array,sizeof my_array,1,out_file);

fread( my_array,sizeof my_array,1,in_file);


是的,前提是文件是以二进制模式而不是文本模式打开的(在

系统上,这会产生影响,如Windows)。 (并假设

包括适当的标题,如stdio.h。)

再一次,它对我来说非常好,但它是否有效/可移植?
Q1:
If an array is declared static in file A is it still valid to access
it from file B?
I mean if a function form file A which returns a pointer to a
position inside of the array is called from file B can functions
in file B read/write to the array using this pointer?
Yes. You can also doi it with something delared static inside a
function:

#include <stdio.h>
#include <limits.h>

const char *to_binary(unsigned value)
{
static char str[sizeof(value) * CHAR_BIT + 1];
int i;
/* fill str with binary value, nul terminated */
return str;
}

int main(void)
{
printf("%s\n", to_binary(42));
return 0;
}
I know that this works and doesn''t produce a warning with
every compiler I''ve used but is this behaviour correct?
The question could be stated differently: does "static" really
make the memory area allocated for the array "static"?
What do you mean by ''static''? C uses it in two different ways:

(within a function) the memory is not re-used between function calls
(contrasted with ''automatic'' variables within functions).

(at file scope) the visibility of the name of the variable only lasts
until the end of the translation unit.

My code above illustrates the first feature, the arrat ''str'' in the
function will keep its value and not be re-used when to_binary() exits,
whereas the (automatic) variable ''i'' will have an undefined value after
to_binary() exits.

The second definition means that nothing ouside the translation unit can
see the /name/ of the variable, but nothing stops a function in that
translation unit from returning its /address/ to a calling function,
which can then access it through that address.
Q2:
Can I save/restore an array to/from disk like this:

int my_array[100];

fwrite(my_array, sizeof my_array, 1, out_file);

fread(my_array, sizeof my_array, 1, in_file);
Yes, provided the files are opened in binary mode and not text mode (on
systems where that makes a difference, like Windows). (And assuming
appropriate headers, like stdio.h, are included.)
Again, it works mighty fine for me but is it
valid/portable?




代码是可移植的,文件可能不是因为数据写的是
作为一系列字节,并且有一个数字可能导致它在另一个系统(甚至是使用不同编译器或不同编译器选项的相同

系统)上错误读回的参数,



包括:


类型的大小(int可能是16,32或64位,或其他)


字节顺序(''endianness'',int中的字节是否存储为高位

low,low to high或其他一些顺序)


如果是一个结构的数组,而不是一个简单类型的数组,

包装问题(结构成员之间是否留有空间)


Chris C



The code is portable, the file may not be because the data is written
as a series of bytes, and there are a number of parameters which could
cause it to be read back incorrectly on another system (or even the same
system with a different compiler or different compiler options),
including:

size of the type (int could be 16, 32 or 64 bits, or others)

byte order (''endianness'', whether bytes in an int are stored high to
low, low to high or some other order)

if it is an array of a struct, rather than an array of a simple type,
packing issues (whether space is left between members of the struct)

Chris C




" Chris Cro ughton" < CH *** @ keristor.net> schrieb im Newsbeitrag

news:sl ****************** @ ccserver.keris.net ...

"Chris Croughton" <ch***@keristor.net> schrieb im Newsbeitrag
news:sl******************@ccserver.keris.net...
On星期六,2005年3月26日09:07:36 +0100,copx
< in ***** @ invalid.com>写道:
[snip]''静态'是什么意思? C以两种不同的方式使用它:

(在函数内)内存不会在函数调用之间重复使用
(与函数中的''自动''变量形成对比)。 />
(在文件范围内)变量名称的可见性仅持续到翻译单元结束。


我的意思是文件范围静态(对不起,我忘了静态是过载

在C中 - 语言设计错误IMO)。

第二个定义意味着翻译单元没有任何东西能够看到变量的/ name /,但没有什么能阻止该翻译单元中的函数返回其/地址/到一个调用函数,
然后可以通过该地址访问它。
On Sat, 26 Mar 2005 09:07:36 +0100, copx
<in*****@invalid.com> wrote: [snip] What do you mean by ''static''? C uses it in two different ways:

(within a function) the memory is not re-used between function calls
(contrasted with ''automatic'' variables within functions).

(at file scope) the visibility of the name of the variable only lasts
until the end of the translation unit.
I meant file scope static (sorry, I forgot that "static" is "overloaded"
in C - a language design bug IMO).
The second definition means that nothing ouside the translation unit can
see the /name/ of the variable, but nothing stops a function in that
translation unit from returning its /address/ to a calling function,
which can then access it through that address.




但在这种情况下,文件范围静态的重点是什么?

除非明确导入(使用extern),否则无法在翻译单元外看到该名称

,对吗? />
我很谨慎,因为我读了一本静态的C书。 (文件范围)

允许编译器基于

进行额外的优化,因为该变量只能在本地范围内访问。


[snip]



But what''s the whole point of file scope static in this case?
The name can''t be seen outside of the translation unit anyway
unless it''s explicitly imported (using "extern"), right?
I was wary because I read in a C book that "static" (file scope)
allows the compiler to do additional optimizations based on
the assumtion that the variable is only accessed in local scope.

[snip]

再次,它对我来说效果很好但它是否有效/便携?
Again, it works mighty fine for me but is it
valid/portable?



代码是可移植的,文件可能不是因为



The code is portable, the file may not be because



[snip]


我知道这些问题。在我的程序的情况下

不需要便携式文件。


copx


[snip]

I''m aware of these problems. In the case of my program
portable files are not required, though.

copx


这篇关于有关数组的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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