这种类型的char数组启动是否合法? [英] Is this type of char array initization legal?

查看:81
本文介绍了这种类型的char数组启动是否合法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码将在visual c ++ 2003中编译,但我不确定它的

是否有效。


unsigned char myString [200] ="" ; $


执行此行后,myString中的所有字节确实设置为

为''0''''但这是非常有效的c ++还是c?我在哪里可以找到

这是如何实现的?


因为我有一个第三方库包装器而我很担心

崩溃,我能够通过从上面改变

初始化方法来缓解崩溃......


unsigned char myString [200];


memset(myString,0,sizeof(myString));


非常感谢任何指导!

- Velik

the code below will compile in visual c++ 2003, but im not sure its
valid.

unsigned char myString[200] = "";

after this line executes, all the bytes within myString are indeed set
to ''0''s'' but is this really valid c++ or c? where can I find out how
this is implemented?

Im concerned because I had a 3rd party library wrapper which was
crashing, and I was able to alleviate the crash by changing the
initialization method from the above to ...

unsigned char myString[200];

memset( myString, 0, sizeof( myString ) );

any guidance is greatly appreciated!
-Velik

推荐答案

做****** @ gmail.com 写道:

下面的代码将在visual c ++ 2003中编译,但我不确定它的

是否有效。


unsigned char myString [200] ="" ;;


执行此行后,myString中的所有字节确实已设置

到''0'''但是这真的是有效的c ++还是c?我在哪里可以找到

这是如何实现的?


因为我有一个第三方库包装器而我很担心

崩溃,我能够通过从上面改变

初始化方法来缓解崩溃......


unsigned char myString [200];


memset(myString,0,sizeof(myString));


非常感谢任何指导!

- Velik
the code below will compile in visual c++ 2003, but im not sure its
valid.

unsigned char myString[200] = "";

after this line executes, all the bytes within myString are indeed set
to ''0''s'' but is this really valid c++ or c? where can I find out how
this is implemented?

Im concerned because I had a 3rd party library wrapper which was
crashing, and I was able to alleviate the crash by changing the
initialization method from the above to ...

unsigned char myString[200];

memset( myString, 0, sizeof( myString ) );

any guidance is greatly appreciated!
-Velik



这是有效的。标准,子条款8.5.2明确允许。


它可能崩溃的唯一原因是编译器没有提供正确的初始化数组。你可以做的是

将它恢复到原来的状态,并设置一个断言,看看它是否确实是问题:


unsigned char myString [200] ="" ;;

#ifndef NDEBUG

for(int iii = 0; iii< 200; ++ iii )

ASSERT(myString [iii] == 0);

#endif


现在,如果你是说你验证阵列的所有元素

在初始化后确实设置为0,然后就可以了,

崩溃是由于你遇到的其他一些问题只是*隐藏*通过介绍

你的''memset''修复,最有可能。


V

- -

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

It is valid. Allowed explicitly by the Standard, subclause 8.5.2.

The only reason it could be crashing is if the compiler wastn''t
providing proper initialisation for the array. What you could do is
revert this to what it was and put an assertion to see if it''s indeed
the problem:

unsigned char myString[200] = "";
#ifndef NDEBUG
for (int iii = 0; iii < 200; ++iii)
ASSERT(myString[iii] == 0);
#endif

Now, if you are saying that you verified that all elements of the array
are indeed set to 0 after the initialisation, then it''s OK, and the
crash is due to some other problem which you just *hid* by introducing
your ''memset'' "fix", most likely.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


2008-10-24 12:22:09 -0400,做** ****@gmail.com 说:
On 2008-10-24 12:22:09 -0400, Do******@gmail.com said:

下面的代码将在visual c ++ 2003中编译,但我不确定它的

是否有效。


unsigned char myString [200] ="" ;; <这行执行后
,myString中的所有字节确实设置为'b'到''0'''但是这真的是有效的c ++还是c?我在哪里可以找到

这是如何实现的?
the code below will compile in visual c++ 2003, but im not sure its
valid.

unsigned char myString[200] = "";

after this line executes, all the bytes within myString are indeed set
to ''0''s'' but is this really valid c++ or c? where can I find out how
this is implemented?



如果myString具有静态存储持续时间,则它们被设置为零,并且如果没有初始化器,那么'

也是如此。如果它有自动存储持续时间,

myString [0]设置为0.

They''re set to zero if myString has static storage duration, and that''s
also true with no initializer. If it has automatic storage duration,
myString[0] is set to 0.


>

我担心,因为我有一个第三方库包装器,它是b / b
崩溃,我能够通过从上面更改

初始化方法来缓解崩溃...


unsigned char myString [200];


memset(myString,0,sizeof(myString));

任何指导都非常感谢!
>
Im concerned because I had a 3rd party library wrapper which was
crashing, and I was able to alleviate the crash by changing the
initialization method from the above to ...

unsigned char myString[200];

memset( myString, 0, sizeof( myString ) );

any guidance is greatly appreciated!



显然,第三方库需要一个阵列,其中所有元素

都设置为零。该要求应该在

库的文档中。


-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com )The brbsp的作者>
标准C ++库扩展:教程和参考

www.petebecker.com/tr1book

Apparently the third-party library requires an array with all elements
set to zero. That requirement should be in the documentation for the
library.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


****** @ gmail.com 写道:

下面的代码将在visual c ++ 2003中编译,但我不确定它

有效。


unsigned char myString [200] ="" ;;

此行执行后
,myString中的所有字节确实设置为'b'到''0'''但是这真的是有效的c ++或c吗?我在哪里可以找到

这是如何实现的?


因为我有一个第三方库包装器而我很担心

崩溃,我能够通过从上面改变

初始化方法来缓解崩溃......


unsigned char myString [200];


memset(myString,0,sizeof(myString));


非常感谢任何指导!
the code below will compile in visual c++ 2003, but im not sure its
valid.

unsigned char myString[200] = "";

after this line executes, all the bytes within myString are indeed set
to ''0''s'' but is this really valid c++ or c? where can I find out how
this is implemented?

Im concerned because I had a 3rd party library wrapper which was
crashing, and I was able to alleviate the crash by changing the
initialization method from the above to ...

unsigned char myString[200];

memset( myString, 0, sizeof( myString ) );

any guidance is greatly appreciated!



实用建议:将其更改为


unsigned char myString [200] = {};


你是什么意思减轻崩溃了吗?


PS:第一次初始化背后的语言律师比我有时间解释要复杂得多。考虑到

数组元素的类型是unsigned char(不是char),而且

这个区域有一个C标准澄清C ++

没有拿起(虽然你的C ++编译器可能会事实上实现它)。


< http://groups.google。 com / group / comp.lang.c ++ / msg / d5ac14fbe55de25b>


-

Gennaro Prota | name.surname yahoo.com

Breeze C ++(预览版):< https://sourceforge.net/projects/breeze/>

你需要C ++方面的专业知识吗? ?我有空。

Practical advice: change it to

unsigned char myString[ 200 ] = {} ;

What to you mean by "alleviate" the crash?

PS: the language lawyering behind the first initialization is a
bit more complicated than I have time to explain; consider that
the array elements are of type unsigned char (not char) and that
there was a C standard clarification in this area which C++
hasn''t picked up (though your C++ compiler might de-facto
implement it).

<http://groups.google.com/group/comp.lang.c++/msg/d5ac14fbe55de25b>

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I''m available.


这篇关于这种类型的char数组启动是否合法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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