免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1878 | 回复: 6
打印 上一主题 下一主题

[C++] C++程序平台移植问题。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-08 16:23 |只看该作者 |倒序浏览
我现在有一个程序原来在linux平台(AS4.0 + gcc version 3.4.3 20041212 (Red Hat 3.4.3-9.EL4))下运行。一切正常。
现在需要将这个程序移植到solaris9平台下(sunos 5.9 + gcc version 3.3.2)。
但是程序总是在 new 的地方core掉,实在是不理解。
core内容:

  1. (gdb) bt
  2. #0  0xfefc3004 in _malloc_unlocked () from /usr/lib/libc.so.1
  3. #1  0xfefc2db4 in malloc () from /usr/lib/libc.so.1
  4. #2  0xff0e1e34 in operator new(unsigned) (sz=376) at ../../../../libstdc++-v3/libsupc++/new_op.cc:48
复制代码


部分代码

  1. int  InitLogFile()
  2. {
  3.     char strFileName[PATH_MAX] = {0};
  4.    
  5.     g_pcDebugLog = g_pcFWDGWLog = g_pcTotalLog = NULL;
  6.    
  7.     snprintf(strFileName,PATH_MAX,"%sdebug.log",g_ocCfgParam.m_strLogFilePath);
  8.     g_pcDebugLog = new CLogFile(strFileName, CLogFile::timeDateTime,CLogFile::modeBoth, 4096, 3);
  9.     if( 0 == g_pcDebugLog->IsInit() )
  10.     {
  11.         printf("%s 调试日志创建失败![LogFileName:%s]\n",
  12.                 g_ocSysTime.m_strTime,strFileName);
  13.         return -1;
  14.     }
  15.     else
  16.     {
  17.         printf("%s 调试日志创建成功![LogFileName:%s]\n",
  18.                 g_ocSysTime.m_strTime,strFileName);
  19.     }

  20.     snprintf(strFileName,PATH_MAX,"%sfwdgwpacket.log",g_ocCfgParam.m_strLogFilePath);
  21.     g_pcFWDGWLog = new CLogFile(strFileName, CLogFile::timeDateTime,CLogFile::modeBoth, 4096, 3);
  22.     if( 0 == g_pcFWDGWLog->IsInit() )
  23.     {
  24.         delete g_pcDebugLog;
  25.         g_pcDebugLog = NULL;
  26.         printf("%s 接受网关信息包日志创建失败![LogFileName:%s]\n",
  27.                 g_ocSysTime.m_strTime,strFileName);
  28.         return -3;
  29.     }
  30.     else
  31.     {
  32.         printf("%s 接受网关信息包日志创建成功![LogFileName:%s]\n",
  33.                 g_ocSysTime.m_strTime,strFileName);
  34.     }
复制代码

该函数可以正常制成第一个new,也就是 debug.log日志可以正常建立。但是在建立第二个的时候,就会core掉。百思不得其解啊,请各位帮忙

论坛徽章:
0
2 [报告]
发表于 2006-10-08 16:33 |只看该作者
这种事情经常发生在32位应用移植到64位平台,有的时候是字节对齐的问题,有的时候是数据截断或者指针的问题。
如果愿意,不妨把代码作为附件放上来,给你在Sparc平台(我这边这样的机器很多,各种版本都有)上调试调试

论坛徽章:
0
3 [报告]
发表于 2006-10-08 17:10 |只看该作者
看看是哪个 new 失败
new CLogFile? 还是CLogFile里面的 new?
程序中可以try catch 啊
btw: CLogFile 需要 new 很多次? 是否禁止了多次new?

论坛徽章:
0
4 [报告]
发表于 2006-10-08 17:13 |只看该作者
谢谢。非常感谢~~
我在我的类声明前加上 #pragma pack(1) ,就可以执行通过了。
能够告知一下,为什么会这样呢?

论坛徽章:
0
5 [报告]
发表于 2006-10-08 17:44 |只看该作者
原帖由 UnixPanther 于 2006-10-8 17:13 发表
谢谢。非常感谢~~
我在我的类声明前加上 #pragma pack(1) ,就可以执行通过了。
能够告知一下,为什么会这样呢?

编译器的事情啦

论坛徽章:
0
6 [报告]
发表于 2006-10-08 17:45 |只看该作者
找到了那部分代码

  1. #include "new"
  2. #include <cstdlib>
  3. #include <exception_defines.h>

  4. using std::new_handler;
  5. using std::bad_alloc;
  6. using std::malloc;

  7. extern new_handler __new_handler;

  8. void *
  9. operator new (std::size_t sz) throw (std::bad_alloc)
  10. {
  11.   void *p;

  12.   /* malloc (0) is unpredictable; avoid it.  */
  13.   if (sz == 0)
  14.     sz = 1;
  15.   p = (void *) malloc (sz);
  16.   while (p == 0)
  17.     {
  18.       new_handler handler = __new_handler;
  19.       if (! handler)
  20. #ifdef __EXCEPTIONS
  21.         throw bad_alloc();
  22. #else
  23.         std::abort();
  24. #endif
  25.       handler ();
  26.       p = (void *) malloc (sz);
  27.     }

  28.   return p;

复制代码

论坛徽章:
0
7 [报告]
发表于 2006-10-08 17:48 |只看该作者
类声明后是不是应该
#pragma pack() // 取消1字节对齐方式
另外还有没有更好的解决办法,这样改变对齐方式在一些体系的机器上是不是还会有问题,再说效率也是个问题。

[ 本帖最后由 HAMMER 于 2006-10-8 17:50 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP