c/c++语言开发共享为什么我的程序在Ubuntu gcc上运行但不在OSX gcc上运行?

所以我的作业,我在Ubuntu中运行它,它编译得很好并且运行方式应该如此。 但是当我在Mac OSX中运行它时,会出现总线错误。 这是为什么?

我正在使用gcc -m32 source.c -o test编译

这是Mac OSX版本(添加了前缀下划线):

 #include  #include  #include  char phrase[] = "slow but sure"; int sz; int phrasesz; char *arg; char *result; // Add any extra variables you may need here. int main(int argc, char* argv[]) { if (argc != 2) { printf("Usage: %s takes 1 string argument.n", argv[0]); exit(1); } // Allocate memory and copy argument string into arg. sz = strlen(argv[1]) + 1; arg = malloc(sz); strcpy(arg, argv[1]); // Allocate lots of memory for the result. phrasesz = strlen(phrase) + 1; result = malloc(sz * phrasesz); // Now copy phrase into result, while replacing SPACE // with SPACE+arg+SPACE. __asm__("n leal _phrase, %esin movl _result, %ebxn outerLoop:n cmpb $0, (%esi)n je finishedn forLoop:n cmpb $32,(%esi)n je endLoopn cmpb $0, (%esi)n je finishedn mov (%esi), %eaxn mov %eax, (%ebx)n incl %ebxn incl %esin jmp forLoopn endLoop:n mov (%esi), %eaxn mov %eax, (%ebx)n incl %ebxn incl %esin movl _arg, %edxn copyArgv1IntoResult:n cmpb $0, (%edx)n je finishedCopyingArgv1n mov (%edx), %ecxn mov %ecx, (%ebx)n incl %ebxn incl %edxn jmp copyArgv1IntoResultn finishedCopyingArgv1:n movb $32, (%ebx)n incl %ebxn jmp outerLoopn finished:n movb $0, (%ebx)n "); printf("%sn", result); return 0; } 

更新:

我在gdb调试器中运行它,这是我得到的错误。

 Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_PROTECTION_FAILURE at address: 0x00000000 0x00001ee8 in finished () 1: x/i $pc 0x1ee8 : mov (%eax),%eax 

此外,我正在删除Ubuntu版本,因此滚动较少。

    你的一些指示,如…

     mov (%esi), %eax 

    …一次从字符缓冲区复制多个字节。 我认为这是偶然的? 你最好用C语言编写代码,然后使用gcc -S并与你手写的代码进行比较。 即使缓冲区与字边界对齐,您也要将指针递增一个字节 ,因此必须尝试对齐未对齐的内存。 sigbus基本上意味着你试图从一个地址中读取一个字的内存值,该地址指向一个不在对齐字开头的字节,但是一些CPU默默地如果慢慢地进行战斗而其他人挽救。 我不知道你的主机之间的硬件差异。

      以上就是c/c++开发分享为什么我的程序在Ubuntu gcc上运行但不在OSX gcc上运行?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/523234.html

      (0)
      上一篇 2020年12月11日
      下一篇 2020年12月11日

      精彩推荐