简单的C code正常工作的HPUX,但设计缺陷在Linux上。为什么? [英] Simple C code works fine on HPUX but segfaults on Linux. Why?
本文介绍了简单的C code正常工作的HPUX,但设计缺陷在Linux上。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我没有在很长一段时间做任何严重的C和AP将preciate的简单说明。下面code编译并运行在HP / UX罚款。它编译没有在Ubuntu上GCC 4.3.2任何警告(甚至用gcc -Wall),但设计缺陷时,在Linux上运行。
任何人能解释为什么吗?
的#include<&stdio.h中GT;诠释主(){
字符*人[] = {阿比盖尔,鲍勃}; 的printf(第一:'%s'的\\ n,让人[0]);
的printf(二:'%s'的\\ n,人[1]); / *此出现segfaults在Linux上,但是,HP / UX工程确定* /
人[1] [0] =R; 的printf(第一:'%s'的\\ n,让人[0]); 返回(0);
}
解决方案
您人阵,其实是字符常量*人[]
。文字字符串是典型的只读存储器在许多系统上。你不能给他们写信。显然,这是不是在HP / UX的情况。
I have not done any serious C in a long, long time and would appreciate a quick explanation. The following code compiles and runs fine on HP/UX. It compiles without any warning on GCC 4.3.2 in Ubuntu (even with gcc -Wall), but segfaults when run on Linux.
Can anyone explain why?
#include <stdio.h>
int main() {
char *people[] = { "Abigail", "Bob" };
printf("First: '%s'\n", people[0]);
printf("Second: '%s'\n", people[1]);
/* this segfaults on Linux but works OK on HP/UX */
people[1][0] = 'R';
printf("First: '%s'\n",people[0]);
return(0);
}
解决方案
Your people array is in fact a char const *people[]
. Literal strings are typically in read-only memory on many systems. You can't write to them. Apparently, this is not the case on HP/UX.
这篇关于简单的C code正常工作的HPUX,但设计缺陷在Linux上。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文