分段错误... SIGSEGV

编程入门 行业动态 更新时间:2024-10-18 12:27:28
本文介绍了分段错误... 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) { 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 > ;, < su ******* @ yahoo。编写: In article <11**********************@k58g2000hse.googlegroups >, <su*******@yahoowrote: 反向函数有什么问题,给出了分段违规。我认为它与数据对齐有关。(4个字节......)。 What is wrong with the reverse function, gives a segmentatiionviolation. 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写道: On Jan 11, 3:41 pm, sunilk...@yahoo 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写道: sunilk...@yahoo 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

本文发布于:2023-10-09 05:53:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474886.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   SIGSEGV

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!