分段错误... SIGSEGV [英] segmentation fault ... SIGSEGV
问题描述
反向功能有什么问题,给出了分段
违规。我认为它与数据对齐有关。(4个字节......)。
谢谢
Slashdot
void main()
{
char * s =" abcdexgh";
reverse(s);
printf("%s" ;,s);
}
void reverse(char * s)
{
char * q,* p;
int j;
char t;
j = strlen(s);
--j;
q = s + j;
for(p = s; p< q; ++ p, - q)< br $>
{
t = * p;
* p = * q;
* q = t;
}
}
Hi,
What is wrong with the reverse function, gives a segmentatiion
violation. I think it is relates to data alignment.(4 bytes ...).
Thanks
Slashdot
void main()
{
char *s="abcdexgh";
reverse(s);
printf("%s",s);
}
void reverse( char *s)
{
char *q,*p;
int j;
char t;
j=strlen(s);
--j;
q= s + j;
for(p=s; p < q; ++p,--q)
{
t=*p;
*p=*q;
*q=t;
}
}
推荐答案
文章< 11 * *********************@k58g2000hse.googlegroups .com> ;,
< su ******* @ yahoo。编写:
In article <11**********************@k58g2000hse.googlegroups .com>,
<su*******@yahoo.comwrote:
反向函数有什么问题,给出了分段违规。我认为它与数据对齐有关。(4个字节......)。
What is wrong with the reverse function, gives a segmentatiion
violation. I think it is relates to data alignment.(4 bytes ...).
> char * s =" abcdexgh" ;;
> char *s="abcdexgh";
该字符串可能无法修改。使用
char s [] =" abcdexgh";
- Richard
-
应考虑在一些字母表中需要多达32个字符
- 1963年的X3.4。
That string may not be modified. Use
char s[] = "abcdexgh";
-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
1月11日下午3:41,sunilk ... @ yahoo.com写道:
On Jan 11, 3:41 pm, sunilk...@yahoo.com wrote:
反向函数有什么问题,给出了分段
违规。我认为它与数据对齐有关。(4个字节......)。
What is wrong with the reverse function, gives a segmentatiion
violation. I think it is relates to data alignment.(4 bytes ...).
不是。
It''s not.
void main()
{
char * s =" abcdexgh" ;;
void main()
{
char *s="abcdexgh";
您的编译器选择将字符串存储在不可写的内存中
,因为它是常量。因此,如果你试图修改它
(正如你的反向函数那样),会引发一个sigsegv。
尝试用以下代码替换此行:
char * s = strdup(" abcdexgh");
Your compiler chose to store the string in non writable memory
as it''s a constant. Hence, if you try to modify it
(as your reverse function does it), a sigsegv is raised.
Try to replace this line with a:
char *s = strdup("abcdexgh");
reverse(s);
printf (QUOT;%S",S);
reverse(s);
printf("%s",s);
添加:
免费;
当你不再需要s时。
-
Pierre
Add a:
free(s);
when you no longer need s.
--
Pierre
sunilk .. 。@ yahoo.com写道:
sunilk...@yahoo.com wrote:
反向功能出了什么问题,给出了分段
违规。我认为它与数据对齐有关。(4个字节......)。
Hi,
What is wrong with the reverse function, gives a segmentatiion
violation. I think it is relates to data alignment.(4 bytes ...).
不,它没有......
No it doesn''t...
char * s =" abcdexgh" ;;
char *s="abcdexgh";
s指向字符串文字的第一个字符。这不是
保证在可写内存中(事实上,理想情况下不应该这样)。
char s [] =" abcdexgh" ;;
会有效。
我确定这是一个FAQ。
s points to the first character of a string literal. This is not
guaranteed to be in writable memory (indeed, it should ideally not be).
char s[] = "abcdexgh";
would work.
I''m sure this is an FAQ.
这篇关于分段错误... SIGSEGV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!