memset()和可移植性 [英] memset() and portability

查看:66
本文介绍了memset()和可移植性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用零填充char / int数组。

像这样便携/保存:


char myArray [30] [80] ;


memset(myArray,0,(30 * 80)* sizeof(char));


copx

I want to fill char/int arrays with zeros.
Is something like this portable/save:

char myArray[30][80];

memset(myArray,0,(30*80) * sizeof(char));

copx

推荐答案

copx写道:
copx wrote:
我想用零填充char / int数组。
是像这样的便携/保存:

char myArray [30] [80];

memset(myArray,0,(30 * 80)* sizeof(char));
I want to fill char/int arrays with zeros.
Is something like this portable/save:

char myArray[30][80];

memset(myArray,0,(30*80) * sizeof(char));




memset(myArray,0,sizeof(myArray));


似乎对我来说至少更多可靠,如果myArray确实是一个

数组而不是指针。


对于int数组,有原则上的可移植性陷阱,除非

某人'从标准中找到了一个证据,即将一个数组作为(未签名)字符处理
并用(char)零填充它将

保证它被(int)零填充。


-

Chris" electric hedgehog" Dollin

C常见问题解答: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html

C欢迎: http://www.angelfire.com/ms3/bchambl...me_to_clc.html


在< bf ********** @ murdoch.hpl.hp.com> Chris Dollin< ke ** @ hpl.hp.com>写道:
In <bf**********@murdoch.hpl.hp.com> Chris Dollin <ke**@hpl.hp.com> writes:
copx写道:
copx wrote:
我想用零填充char / int数组。
就像这样便携式/保存:

char myArray [30] [80];

memset(myArray,0,(30 * 80)* sizeof(char));
memset (myArray,0,sizeof(myArray));至少对我来说,如果myArray确实是一个
数组而不是指针,那么对我来说似乎更可靠。
I want to fill char/int arrays with zeros.
Is something like this portable/save:

char myArray[30][80];

memset(myArray,0,(30*80) * sizeof(char));
memset( myArray, 0, sizeof (myArray) );

seems, to me at least, to be more reliable, if myArray is indeed an
array and not a pointer.




它也更具可读性,特别是如果你删除不需要的括号,

来暴露sizeof是一个操作符而不是一个函数的事实:


memset(myArray,0,sizeof myArray);

对于int数组,有原则上的可移植性陷阱,除非
某人''从标准中找到了一个证据,即将一个int的数组作为(无符号)字符处理并用(char)零填充它将保证它被(int)零填充。



It''s also more readable, especially if you drop the unneeded parentheses,
to expose the fact that sizeof is an operator and not a function:

memset(myArray, 0, sizeof myArray);
For int arrays there are in-principle portability traps, unless
someone''s found a proof from the Standard that treating an array
of ints as (unsigned) chars and filling it with (char) zeroes will
guarantee that it gets filled with (int) zeroes.




官方答复缺陷报告保证。希望TC2

将包含对当前标准的相关更改。


但是,因为C89首先没有问题和C99是实际上没有实现的b $ b,没有值得担心的问题;-)


Dan

-

Dan Pop

DESY Zeuthen,RZ集团

电子邮件: Da ** ***@ifh.de




" copx" <在***** @ invalid.com>在消息中写道

"copx" <in*****@invalid.com> wrote in message

我想用零填充char / int数组。
就像这样便携/保存:

char myArray [30 ] [80];

memset(myArray,0,(30 * 80)* sizeof(char));

I want to fill char/int arrays with zeros.
Is something like this portable/save:

char myArray[30][80];

memset(myArray,0,(30*80) * sizeof(char));



sizeof(char)总是等于1 。


memset()有一些恼人的可移植性陷阱。例如,NULL

指针几乎总是所有位为零,但这不能保证。 (这是
保证总是比较等于0)。当所有位都为零时,浮点数几乎为
始终为0,但这不能保证。


问题是你的代码会很愉快地工作,当有人将其转换为其中一种奇怪的格式时突然断开



sizeof(char) always equals one.

memset() has a few irritating portability traps. For instance, a NULL
pointer is almost always all bits zero, but this is not guaranteed. (It is
guaranteed to always compare equal to 0). A floating point number is almost
always 0 when all bits are zero, but again this is not guaranteed.

The problem is that your code will work happily, and then suddenly break
when someone transfers it to one of these weird formats.


这篇关于memset()和可移植性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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