char数组初始化 [英] char array initialization
问题描述
在审查我的一个同行的代码时,我遇到了一段代码
,这对我来说有点令人惊讶。它与初始化
一个可变长度数组有关。我运行了一个小应用程序来说服自己
它是否有效,它确实有效 - 但是我无法解释为什么,所以我为什么,所以我以为我会问这里。
示例代码如下:
char test [] =" hello world \ ñ英寸; //测试字符串,但我很容易
从文件中检索到这个字符串或者在哪里
int len = strlen(test)+1;
//以下行是感兴趣的代码
char out [len];
strncpy(out,test,len);
printf(" out:%s",out);
注意''out''在堆栈上声明,但'len''是变量
,其值在编译时未知。为什么会这样?我认为实际使用变量
长度的char数组的正确方法是使用new,比如char * out = new char [len],但是不知何故
以上代码有效。有人能解释为什么会这样吗?
Avalon1178写道:
在审查我的一个同行代码时,我遇到了一段代码
,这对我来说有点令人惊讶。它与初始化
一个可变长度数组有关。我运行了一个小应用程序来说服自己
它是否有效,它确实有效 - 但是我无法解释为什么,所以我为什么,所以我以为我会问这里。
示例代码如下:
char test [] =" hello world \ ñ英寸; //测试字符串,但我很容易
从文件中检索到这个字符串或者在哪里
你是什么意思,检索从文件?通过在
中包含一些文本,将字符串定义为宏?它不会使这个字符串变量 -
长度。
int len = strlen(test)+1;
//以下行是感兴趣的代码
char out [len];
这在C ++中是非法的。
strncpy(out,test,len);
printf(" out:%s",out);
注意''out''在堆栈上声明,但是'len' '是一个变量
,其值在编译时是未知的。为什么会这样?
因为编译它的编译器提供了这种语法
作为扩展名,很有可能。
我
会认为实例化具有变量
长度的char数组的正确方法是使用new,例如char * out = new char [len]",但不知何故
以上代码有效。有人可以解释为什么这有效吗?
这不是合法的C ++。它的工作原理是因为编译器没有编译
C ++,它正在编译C ++ with extensions。
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要问
11月9日下午1:32,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
Avalon1178写道:
回顾一下我的同行代码,我遇到了一段代码
,这对我来说有点令人惊讶。它与初始化
一个可变长度数组有关。我运行了一个小应用程序来说服自己
它是否有效,它确实有效 - 但是我无法解释为什么,所以我为什么,所以我以为我会在这里问。
示例代码如下:
char test [] =hello world \ n; //测试字符串,但我可以很容易地从一个文件或者每个
检索这个字符串
你是什么意思,检索从文件?通过在
中包含一些文本,将字符串定义为宏?它不会使这个字符串变量 -
长度。
我只是说测试可以在任何地方设置或初始化
,而不是必须像我在上面定义了(比如
getline(),或snprintf,或其他)。我只是为了简单的
演示而做了。
>
int len = strlen(test)+1;
//以下行是感兴趣的代码
char out [len];
这在C ++中是非法的。
是的,这也是我的想法....
strncpy(out,test,len);
printf(" out :%s,out);
注意''out''在堆栈上声明,但'len''是变量
其值在编译时是未知的。为什么会这样?
因为编译它的编译器提供了这种语法
作为扩展名,很有可能。
我
会认为实例化具有变量
长度的char数组的正确方法是使用new,例如char * out = new char [len]",但不知何故
以上代码有效。有人可以解释为什么这有效吗?
这不是合法的C ++。它的工作原理是因为编译器没有编译
C ++,它正在编译C ++ with extensions。
太棒了!谢谢你的解释。
V
-
请删除大写'A'的时候通过电子邮件回复
我没有回复最热门的回复,请不要问
< blockquote>嗯.....所以我收集它是依赖编译器的。我使用g ++编译代码
,在执行g ++ -v之后,这里是它运行的版本:
从/ usr /读取规范lib / gcc / i386-redhat-linux / 3.4.6 / specs
配置:../ configure --prefix = / usr --mandir = / usr / share / man -
infodir = / usr / share / info --enable-shared --enable-threads = posix -
禁用检查--with-system-zlib --enable- __cxa_atexit --disable-
libunwind-exceptions --enable-java-awt = gtk --host = i386-redhat-linux
线程模型:posix
gcc版本3.4.6 20060404(Red Hat 3.4.6-3)
我仍然很好奇....如果说我正在使用这个C ++
扩展,编译器生成汇编代码究竟是怎样的?'char out [len]''
,因为编译后的二进制文件正在工作?
During a review of one of my peer''s code, I ran into a section of code
that was a bit suprising to me. Its got to do with initialization of
a variable-length array. I ran a small application to convince myself
whether it works or not, and it does work---however I couldn''t explain
it to myself why, so I thought I''d ask here.
The sample code is below:
char test[] = "hello world\n"; // test string, but I could easily
have retrieved this string from a file or wherever
int len = strlen(test)+1;
// line below is the code of interest
char out[len];
strncpy(out, test, len);
printf("out: %s", out);
Note that ''out'' is declared on the stack, yet ''len'' is a variable
whose value is not known at compile time. Why would this work? I
would think the proper way of instantiating a char array with variable
length is using new, like "char *out = new char[len]", yet somehow the
above code works. Can someone explain why this works?
Avalon1178 wrote:During a review of one of my peer''s code, I ran into a section of code
that was a bit suprising to me. Its got to do with initialization of
a variable-length array. I ran a small application to convince myself
whether it works or not, and it does work---however I couldn''t explain
it to myself why, so I thought I''d ask here.
The sample code is below:
char test[] = "hello world\n"; // test string, but I could easily
have retrieved this string from a file or whereverWhat do you mean, "retrieve from a file"? By including some text with
the string defined as a macro? It doesn''t make this string "variable-
length".
int len = strlen(test)+1;
// line below is the code of interest
char out[len];That''s illegal in C++.
strncpy(out, test, len);
printf("out: %s", out);
Note that ''out'' is declared on the stack, yet ''len'' is a variable
whose value is not known at compile time. Why would this work?Because the compiler with which it is compiled offered this syntax
as an extension, most likely.
I
would think the proper way of instantiating a char array with variable
length is using new, like "char *out = new char[len]", yet somehow the
above code works. Can someone explain why this works?It''s not legal C++. It works because the compiler wasn''t compiling
C++, it was compiling "C++ with extensions".
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
On Nov 9, 1:32 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:Avalon1178 wrote:During a review of one of my peer''s code, I ran into a section of code
that was a bit suprising to me. Its got to do with initialization of
a variable-length array. I ran a small application to convince myself
whether it works or not, and it does work---however I couldn''t explain
it to myself why, so I thought I''d ask here.
The sample code is below:
char test[] = "hello world\n"; // test string, but I could easily
have retrieved this string from a file or wherever
What do you mean, "retrieve from a file"? By including some text with
the string defined as a macro? It doesn''t make this string "variable-
length".I''m just saying that ''test'' could have been set or initialized
anywhere, and not necessary like the one I defined above (i.e. like
getline(), or snprintf, or whatever). I just did it for simple
demonstration.
>int len = strlen(test)+1;
// line below is the code of interest
char out[len];
That''s illegal in C++.
Yeah, that''s what I thought too....
strncpy(out, test, len);
printf("out: %s", out);
Note that ''out'' is declared on the stack, yet ''len'' is a variable
whose value is not known at compile time. Why would this work?
Because the compiler with which it is compiled offered this syntax
as an extension, most likely.
I
would think the proper way of instantiating a char array with variable
length is using new, like "char *out = new char[len]", yet somehow the
above code works. Can someone explain why this works?
It''s not legal C++. It works because the compiler wasn''t compiling
C++, it was compiling "C++ with extensions".
Great! Thanks for the explanation.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
Hmm.....so I gather it is compiler dependent. I compiled the code
using g++, and after doing g++ -v, here''s the version it is running:
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.6/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --
disable-checking --with-system-zlib --enable-__cxa_atexit --disable-
libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-3)
I''m still curious however....if say I am using this C++ with
extensions, how exactly is the compiler generating the assembly code
for ''char out[len]'', since the compiled binary is working?
这篇关于char数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!