Redian新闻
>
Unix下好好的到了linux下segment fault
avatar
a*e
2
use gdb to trace.
it is very common that a program runs correctly in one OS, but not
in another.

【在 d*******e 的大作中提到】
: 是在程序返回的时候出错。
: 会是为啥?

avatar
a*n
3
without the code, how can others help you? BTW, I am not an expert...
But for example, Linux with gcc deals with string constant differently
with other unix platforms. This is an example which core dumped on Linux
but not on others:
/>cat test.c
#include
#include
#include
int main()
{
char * p = "hello";
*p='p';
printf("%s\n", p);
return 0;
}
/Tru64Unix>cc -V
Compaq C V6.3-025 on Compaq Tru64 UNIX V5.1 (Rev. 732)

【在 d*******e 的大作中提到】
: 是在程序返回的时候出错。
: 会是为啥?

avatar
o*v
4
这code是合法但不合理的样子......
各个cc的实现不一样吧.

【在 a**n 的大作中提到】
: without the code, how can others help you? BTW, I am not an expert...
: But for example, Linux with gcc deals with string constant differently
: with other unix platforms. This is an example which core dumped on Linux
: but not on others:
: />cat test.c
: #include
: #include
: #include
: int main()
: {

avatar
t*t
5
you may try different compilers, like transfering from gcc
to g++. Sometimes this helps..

【在 d*******e 的大作中提到】
: 是在程序返回的时候出错。
: 会是为啥?

avatar
t*c
6
why this one does not work for gcc?
or more specifically, if I change char *p to char p[], it works fine.
any particular reason for this?

【在 a**n 的大作中提到】
: without the code, how can others help you? BTW, I am not an expert...
: But for example, Linux with gcc deals with string constant differently
: with other unix platforms. This is an example which core dumped on Linux
: but not on others:
: />cat test.c
: #include
: #include
: #include
: int main()
: {

avatar
a*n
7
if you define char *p = "hello";
then it mean p pointer to a string literal, which is const, and you are
not supposed to change it. You can refer C++ primer or any other book
for this.
If you define it as char p[], it is a different story.

【在 t**c 的大作中提到】
: why this one does not work for gcc?
: or more specifically, if I change char *p to char p[], it works fine.
: any particular reason for this?

avatar
t*c
8
that's what I suspect, but just don't know the implementation details.
thanks.

【在 a**n 的大作中提到】
: if you define char *p = "hello";
: then it mean p pointer to a string literal, which is const, and you are
: not supposed to change it. You can refer C++ primer or any other book
: for this.
: If you define it as char p[], it is a different story.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。