64位分段错误

编程入门 行业动态 更新时间:2024-10-22 16:36:27
本文介绍了64位分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我正在粘贴一段在32位系统上执行正常的代码,但是当编译64位编译器时,因分段错误而失败。我是 在PA-RISC系统上使用HP-UX C编译器。从提到32到64位 系统的可移植性问题的文件中获取此代码 。 但是当我包括系统文件< malloc.h,代码在两个系统上执行罚款 。 #include< stdio.h> int main(int argc,char ** argv) { char mystring1 [10] =" foo"; char * mystring2; mystring2 =(char *)malloc(sizeof(char)*(long)10); strcpy(mystring2," bar \ n \ 0"); printf("%s%s",mystring1,mystring2); 返回0; } 32位输出 foo bar 我会尝试调试但是无法针对原因。粘贴gdb输出的 剪辑。 程序收到信号SIGSEGV,分段错误。 0x800003ffff743ac8 in strlen + 0x10( )from /usr/lib/pa20_64/libc.2 (gdb)bt #0 0x800003ffff743ac8 in strlen + 0x10()来自/ usr / lib / pa20_64 / libc .2 #1 0x4000000000001f08 in main(argc = 1,argv = 0x800003ffff7f07a0) test1.c:8 我假设它与malloc参数10有关,这个参数被强制为 long但无法推理出来。任何人都可以解释一下细节吗?

Hi All, I am pasting a piece of code which executes fine on 32 bit system but fails with a segmentation fault when compiled 64 bit compiler.I am using a HP-UX C compiler on PA-RISC system. This code was picked up from a document mentioning portability issues from 32 to 64 bit systems. But when I include the system file <malloc.h, the code executes fine on both the systems. #include <stdio.h> int main(int argc, char **argv) { char mystring1[10] = "foo"; char *mystring2; mystring2 = (char *)malloc(sizeof(char)*(long)10); strcpy(mystring2, "bar\n\0"); printf("%s%s", mystring1, mystring2); return 0; } Output on 32 bit foo bar I''ll tried to debug it but was unable to target the cause. pasting a clip of the gdb output. Program received signal SIGSEGV, Segmentation fault. 0x800003ffff743ac8 in strlen+0x10 () from /usr/lib/pa20_64/libc.2 (gdb) bt #0 0x800003ffff743ac8 in strlen+0x10 () from /usr/lib/pa20_64/libc.2 #1 0x4000000000001f08 in main (argc=1, argv=0x800003ffff7f07a0) at test1.c:8 I assume it is to do with the malloc argument 10 which is typecasted to long but unable to reason out. Can anyone explain the details?

推荐答案

Linny说: Linny said: 大家好, 我正在粘贴一段代码,该代码在32位系统上执行正常但是当编译64位编译器时,因分段错误而失败。我是 在PA-RISC系统上使用HP-UX C编译器。从提到32到64位 系统的可移植性问题的文档中获取此代码 。 Hi All, I am pasting a piece of code which executes fine on 32 bit system but fails with a segmentation fault when compiled 64 bit compiler.I am using a HP-UX C compiler on PA-RISC system. This code was picked up from a document mentioning portability issues from 32 to 64 bit systems.

你没能为malloc提供原型,这不是一个函数 返回int,所以行为是未定义的。你的编译器会有 警告你,但你通过(毫无意义地)投射malloc'的结果来阻止它。 您的问题在一些细节中有详细解释关于铸造的论文,你可以找到 www.cpax.uk/prg/writings/casting.php

但是当我包含系统文件时< malloc.h,代码在两个系统上执行良好 。 But when I include the system file <malloc.h, the code executes fine on both the systems.

你真正想要的是适用于malloc的原型,你可以通过 获得,包括标准系统头,< stdlib。 h-而且,当你关于 时,为什么不添加< string.hfor strcpy? 标题不具装饰性!它们是出于某种目的而提供的。确保你 使用你需要的那些。

What you really want is a proper prototype for malloc, which you can get by including the standard system header, <stdlib.h- and, while you''re about it, why not add <string.hfor strcpy? Headers are not decorative! They are provided for a purpose. Make sure you use the ones you need.

> #include< stdio.h> ; int main(int argc,char ** argv) { char mystring1 [10] =" foo"; char * mystring2; mystring2 =(char *)malloc(sizeof(char)*(long)10); strcpy(mystring2," ; bar\\\\0"); printf("%s%s",mystring1,mystring2); 返回0; } > #include <stdio.h> int main(int argc, char **argv) { char mystring1[10] = "foo"; char *mystring2; mystring2 = (char *)malloc(sizeof(char)*(long)10); strcpy(mystring2, "bar\n\0"); printf("%s%s", mystring1, mystring2); return 0; }

#include< stdlib.h> #include< string.h> #include< stdio.h> int main(int argc,char ** argv) { char mystring1 [10] =" foo"; char * mystring2; mystring2 = malloc(sizeof * mystring2 * 10); if(mystring2!= NULL) { strcpy(mystring2," bar \ n"); printf ("%s%s",mystring1,mystring2); fr ee(mystring2); } 返回0; } 我在这里:〜/ scratchmake gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings -Wno-conversion -ffloat-store -O2 -g -pg -c -o foo.o foo.c foo.c:在函数`main'': foo.c:5:警告:未使用的参数`argc'' foo.c:5:警告:未使用的参数`argv'' gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes -Wstrict -prototypes -Wmissing-declarations -Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings -Wno-conversion -ffloat-store -O2 -g -pg -o foo foo.o -lm 我@here:〜/ scratch。/ foo foobar - Richard Heathfield Usenet是一个奇怪的地方 - dmr 29/7/1999 www.cpax.uk 电子邮件:rjh在上面的域名(但显然放弃了www)

#include <stdlib.h> #include <string.h> #include <stdio.h> int main(int argc, char **argv) { char mystring1[10] = "foo"; char *mystring2; mystring2 = malloc(sizeof *mystring2 * 10); if(mystring2 != NULL) { strcpy(mystring2, "bar\n"); printf("%s%s", mystring1, mystring2); free(mystring2); } return 0; } me@here:~/scratchmake gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings -Wno-conversion -ffloat-store -O2 -g -pg -c -o foo.o foo.c foo.c: In function `main'': foo.c:5: warning: unused parameter `argc'' foo.c:5: warning: unused parameter `argv'' gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings -Wno-conversion -ffloat-store -O2 -g -pg -o foo foo.o -lm me@here:~/scratch./foo foobar -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 www.cpax.uk email: rjh at above domain (but drop the www, obviously)

Linny写道: = Linny wrote: = 我粘贴了一段代码,该代码在32位系统上执行正常,但 在编译时因分段错误而失败64位编译器。我是在PA-RISC系统上使用HP-UX C编译器的。从提到32到64位 系统的可移植性问题的文件中获取此代码 。 但是当我包括系统文件< malloc.h, I am pasting a piece of code which executes fine on 32 bit system but fails with a segmentation fault when compiled 64 bit compiler.I am using a HP-UX C compiler on PA-RISC system. This code was picked up from a document mentioning portability issues from 32 to 64 bit systems. But when I include the system file <malloc.h,

当然你的意思是< stdlib.h>?没有< malloc.hin 标准C.

Surely you mean <stdlib.h>? There''s no such thing as <malloc.hin standard C.

代码执行正常 $ b两个系统都是$ b。 #include< stdio.h> int main(int argc,char ** argv) { char mystring1 [10] =" foo"; char * mystring2; mystring2 =(char *)的malloc(的sizeof(char)的*(长)10); the code executes fine on both the systems. #include <stdio.h> int main(int argc, char **argv) { char mystring1[10] = "foo"; char *mystring2; mystring2 = (char *)malloc(sizeof(char)*(long)10);

BOOOOOOOOOOOOM。 你做了什么?你没有在范围内声明`malloc` 。编译器必须为您声明它。它给了 它是一个返回类型的`int`,因为它必须。演员到char * 隐瞒了这一点。 我的赌注是调用约定不同于 64位实现和32位实现, ,会发生什么是64位地址获得 传回一个不同于int的寄存器, 所以代码将垃圾转换为char *。 (或者 结果被截断为32位,或者其他什么。谁知道知道什么?)

BOOOOOOOOOOOOM. What did you do? You did not have a declaration of `malloc` in scope. The compiler had to declare it for you. It gave it a return-type of `int`, as it had to. The cast to char* conceals this. My bet is that the calling conventions are different on the 64-bit implementation and the 32-bit implementation, and what happens is that the 64-bit address gets passed back in a different register than an int would, so the code casts garbage to char*. (Or maybe the result gets truncated to 32 bits, or something. Who knows?)

我假设它与malloc参数10有关,这个参数被强制为 long但无法推理出来。任何人都可以解释细节吗? I assume it is to do with the malloc argument 10 which is typecasted to long but unable to reason out. Can anyone explain the details?

不,这是因为编译器首先被误导,然后 被禁止投诉。开头的代码是错的, 你所看到的是/原因/为什么它错了。 #include< ; stdlib.h> ... char * mystring2 = malloc(10 * sizeof(* mystring2)); ... 会解决它。 - Chris" Essen -6并且计算 ; Dollin 得分,哇。如果我想得分,我会去玩/年龄的蒸汽/。

No, it''s because the compiler was first misled and then prevented from complaining. The code was wrong to start with, and what you''re seeing is the /reason/ why it''s wrong. #include <stdlib.h> ... char *mystring2 = malloc( 10 * sizeof( *mystring2 ) ); ... would fix it. -- Chris "Essen -6 and counting" Dollin Scoring, bah. If I want scoring I''ll go play /Age of Steam/.

Linny写道: Linny wrote: 大家好, 我粘贴了一段在32位系统上执行正常的代码,但是当编译64位编译器时,因分段错误而失败.I我在PA-RISC系统上使用HP-UX C编译器获得。从提到32到64位 系统的可移植性问题的文件中获取此代码 。 但是当我包括系统文件< malloc.h,代码在两个系统上执行罚款 。 #include< stdio.h> Hi All, I am pasting a piece of code which executes fine on 32 bit system but fails with a segmentation fault when compiled 64 bit compiler.I am using a HP-UX C compiler on PA-RISC system. This code was picked up from a document mentioning portability issues from 32 to 64 bit systems. But when I include the system file <malloc.h, the code executes fine on both the systems. #include <stdio.h>

您忘了包含malloc的头文件。 我会留给您找到您需要的头文件 - 它虽然不是malloc.h。

You forgot to include the header file for malloc. I''ll leave it up to you to find which one you need - it is not malloc.h though.

int main(int argc,char ** argv) { char mystring1 [10] =" foo"; char * mystring2; mystring2 =(char *)malloc(sizeof (字符)*(长)10); int main(int argc, char **argv) { char mystring1[10] = "foo"; char *mystring2; mystring2 = (char *)malloc(sizeof(char)*(long)10);

不需要这个转换为char *,并且可能会发出警告 你应该关心。 sizeof(char)总是一个,并且没有任何理由 对于长时间的演员来说。除非你知道 究竟是什么原因,否则不要四处乱扔。

This cast to a char *is not needed, and might supress a warning you should care about. sizeof(char) is always one, and there isn''t any justification for the cast to long. Don''t throw around casts unless you know exactly why.

strcpy(mystring2," bar\\\\0" ); strcpy(mystring2, "bar\n\0");

字符串文字已经空终止,在这种情况下无需再添加另一个。

String literals are already null terminated, no need to add another one in this case.

printf("%s%s",mystring1,mystring2); 返回0; } 输出32位 printf("%s%s", mystring1, mystring2); return 0; } Output on 32 bit

[snip]

[snip]

更多推荐

64位分段错误

本文发布于:2023-11-26 17:56:37,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误

发布评论

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

>www.elefans.com

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