一个指针数组,导致分段错误 [英] an array of pointers, causing segmentation fault
问题描述
您好,以下代码因分段错误而失败。可以
有人告诉我为什么失败了吗?谢谢!
#include< iostream>
#include< string>
#include< cstring>
#include< cstddef>
使用命名空间std;
int main(){
char * x [] = {" a,b,c};
for(size_t j = 0; j!= 3; j ++)
cout<< *(x [j])<< endl;
*(x [0])=''A''; //因为字符串a和是一个字符数组,*(x [0])
是内存中存储数组中第一个字符的位置,即
''a'' 。这行代码尝试将其更改为''A'
for(size_t j = 0; j!= 3; j ++)
cout< ;< *(x [j])<<结束;
返回0;
}
输出结果为:
a
b
c
分段错误
Hello, the following code failed with "segmentation fault". Could
someone tell me why it failed please? Thanks!
#include<iostream>
#include<string>
#include<cstring>
#include<cstddef>
using namespace std;
int main(){
char* x[] = {"a","b","c"};
for(size_t j = 0; j != 3; j++)
cout << *(x[j]) << endl;
*(x[0]) = ''A''; //since string "a" is an array of chars, and *(x[0])
is the location in memory that stores the first char in the array, ie
''a''. this line of code tries to change it to ''A''
for(size_t j = 0; j != 3; j++)
cout << *(x[j]) << endl;
return 0;
}
The output is:
a
b
c
Segmentation fault
推荐答案
Jess写道:
Jess wrote:
您好,以下代码因分段错误而失败。可以
有人告诉我为什么失败了吗?谢谢!
#include< iostream>
#include< string>
#include< cstring>
#include< cstddef>
使用命名空间std;
int main(){
char * x [] = {" a"," b"," c"};
Hello, the following code failed with "segmentation fault". Could
someone tell me why it failed please? Thanks!
#include<iostream>
#include<string>
#include<cstring>
#include<cstddef>
using namespace std;
int main(){
char* x[] = {"a","b","c"};
这是一个不幸的语言功能[应该被废除
年前!!!]让你相信你被允许更改''x'元素指向的字符的
值。你有_ b $ b初始化''x'',地址是_constant_字符数组。
然后你试着改变它们,这是'未定义的行为。
This is an unfortunate language feature [that ought to be abolished
years ago!!!] which led you to believe you are allowed to change the
values of the characters to which elements of ''x'' point. You have
initialised ''x'' with addresses of arrays of _constant_ characters.
Then you try to change them, and that''s undefined behaviour.
>
for(size_t j = 0; j!= 3; j ++)
cout<< *(x [j])<< endl;
*(x [0])=''A''; //因为字符串a和是一个字符数组,*(x [0])
是内存中存储数组中第一个字符的位置,即
''a'' 。这行代码尝试将其更改为''A'
for(size_t j = 0; j!= 3; j ++)
cout< ;< *(x [j])<<结束;
返回0;
}
输出结果为:
a
b
c
分段错误
>
for(size_t j = 0; j != 3; j++)
cout << *(x[j]) << endl;
*(x[0]) = ''A''; //since string "a" is an array of chars, and *(x[0])
is the location in memory that stores the first char in the array, ie
''a''. this line of code tries to change it to ''A''
for(size_t j = 0; j != 3; j++)
cout << *(x[j]) << endl;
return 0;
}
The output is:
a
b
c
Segmentation fault
" - 医生,当我尝试更改常量内存的内容,
我的程序段错误!
- 好吧,不要尝试更改常量内存的内容。
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要问
"- Doctor, when I try changing the contents of the constant memory,
my program segfaults!
- Well, don''t try changing the contents of the constant memory."
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
2007年4月26日星期四08:44:29 -0400,Victor Bazarov写道:
On Thu, 26 Apr 2007 08:44:29 -0400, Victor Bazarov wrote:
Jess写道:
Jess wrote:
>您好,以下代码因分段错误而失败。可能有人告诉我为什么失败了吗?谢谢!
#include< iostream>
#include< string>
#include< cstring>
#include< cstddef>
using namespace std;
int main(){
char * x [] = {" a"," b"," c"};
>Hello, the following code failed with "segmentation fault". Could
someone tell me why it failed please? Thanks!
#include<iostream>
#include<string>
#include<cstring>
#include<cstddef>
using namespace std;
int main(){
char* x[] = {"a","b","c"};
这是一个不幸的语言功能[应该废除
年前!!!]让你相信你被允许更改''x'元素指向的字符的
值。你有
初始化''x'',其中包含_constant_字符数组的地址。然后
你试着改变它们,那是'未定义的行为。
This is an unfortunate language feature [that ought to be abolished
years ago!!!] which led you to believe you are allowed to change the
values of the characters to which elements of ''x'' point. You have
initialised ''x'' with addresses of arrays of _constant_ characters. Then
you try to change them, and that''s undefined behaviour.
确实很不幸;然而,看到我们被它坚持下去我想知道
是否期望你的编译器接受它并不是太多(
至少是警告),因为如果你试图将
分配给任何其他const表达式,它很可能会抱怨。或者那会违反
标准吗?
-
Lionel B
Unfortunate indeed; however seeing as we''re stuck with it I''m wondering
whether it''s not too much to expect your compiler to pick up on it (with
at least a warning), since it will most likely complain if you try to
assign to any other const expression. Or would that contravene the
standard somewhere?
--
Lionel B
Lionel B schreef:
Lionel B schreef:
On Thu,2007年4月26日08:44:29 -0400,Victor Bazarov写道:
On Thu, 26 Apr 2007 08:44:29 -0400, Victor Bazarov wrote:
> Jess写道:
>Jess wrote:
>>您好,以下代码因分段错误而失败。可能有人告诉我为什么失败了吗?谢谢!
#include< iostream>
#include< string>
#include< cstring>
#include< cstddef>
using namespace std;
int main(){
char * x [] = {" a"," b"," c"};
>>Hello, the following code failed with "segmentation fault". Could
someone tell me why it failed please? Thanks!
#include<iostream>
#include<string>
#include<cstring>
#include<cstddef>
using namespace std;
int main(){
char* x[] = {"a","b","c"};
这是一个不幸的语言功能[应该在多年前废除!!!]让你相信你可以改变
值x元素指向的字符。您已使用_constant_字符数组的地址初始化''x''。然后你试着改变它们,那是不确定的行为。
This is an unfortunate language feature [that ought to be abolished
years ago!!!] which led you to believe you are allowed to change the
values of the characters to which elements of ''x'' point. You have
initialised ''x'' with addresses of arrays of _constant_ characters. Then
you try to change them, and that''s undefined behaviour.
确实很不幸;然而,看到我们被它坚持下去我想知道
是否期望你的编译器接受它并不是太多(
至少是警告),因为如果你试图将
分配给任何其他const表达式,它很可能会抱怨。或者那会违反
标准吗?
Unfortunate indeed; however seeing as we''re stuck with it I''m wondering
whether it''s not too much to expect your compiler to pick up on it (with
at least a warning), since it will most likely complain if you try to
assign to any other const expression. Or would that contravene the
standard somewhere?
不确定以下内容是否正确:
int main()
{
char * x [] = {" a"," b"," c"};
foo(x [0]);
}
void foo(char * p)
{
* p =''A'';
}
我的想法是它可能需要很多跟踪和跟踪
编译器,以便检测可能发生段错误的所有可能情况。
Jeroen
Not sure if the following is correct:
int main()
{
char* x[] = {"a","b","c"};
foo(x[0]);
}
void foo(char *p)
{
*p = ''A'';
}
My idea is that it may require a lot of tracking and tracing for a
compiler in order to detect all possible situations where the segfault
can occur.
Jeroen
这篇关于一个指针数组,导致分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!