二进制数计算出问题

eagle216 2008-07-23 11:45:01
1、在64位机上编译程序碰到这样一个错误
../../ipa_link/libiberty/objalloc.c: In function `objalloc_create':
../../ipa_link/libiberty/objalloc.c:106: warning: implicit declaration of function `__builtin_offsetof'
../../ipa_link/libiberty/objalloc.c:106: error: syntax error before "struct"
make[3]: *** [objalloc.o] Error 1

2、源代码:
105 ret->current_ptr =(char *) chunk
106 + CHUNK_HEADER_SIZE;

3、其中出错的原因是CHUNK_HEADER_SIZE,定义如下:
#define CHUNK_HEADER_SIZE \
((sizeof (struct objalloc_chunk) + OBJALLOC_ALIGN - 1) \
&~ (OBJALLOC_ALIGN - 1))

4、
感觉是只要(OBJALLOC_ALIGN - 1) &~ (OBJALLOC_ALIGN - 1)就会有问题
ret->current_ptr 为char *
chunk 为 stuct objalloc_chunk *
...全文
136 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
快乐田伯光 2008-07-24
  • 打赏
  • 举报
回复
应该不是4的问题,报的是语法错误,应该跟64位机没什么干系,应该是有其它浅显的错误没发现。使用这个宏的时候有没有包括定义结构stuct objalloc_chunk的头文件?
eagle216 2008-07-24
  • 打赏
  • 举报
回复
有stuct objalloc_chunk的定义。
struct objalloc_chunk
{
/* Next chunk. */
struct objalloc_chunk *next;
/* If this chunk contains large objects, this is the value of
current_ptr when this chunk was allocated. If this chunk
contains small objects, this is NULL. */
char *current_ptr;
};

发现应该是OBJALLOC_ALIGN 有问题,自己添加了一条语句
104 unsigned long tmp;
105 tmp=OBJALLOC_ALIGN ;
106 ret->current_ptr =(char *) chunk+ tmp;
107 // + CHUNK_HEADER_SIZE;
108 ret->current_space = CHUNK_SIZE - CHUNK_HEADER_SIZE;

然后报错:
../../ipa_link/libiberty/objalloc.c -o objalloc.o
../../ipa_link/libiberty/objalloc.c:107:1: warning: C++ style comments are not allowed in ISO C90
../../ipa_link/libiberty/objalloc.c:107:1: warning: (this will be reported only once per input file)
../../ipa_link/libiberty/objalloc.c: In function `objalloc_create':
../../ipa_link/libiberty/objalloc.c:104: warning: ISO C90 forbids mixed declarations and code
../../ipa_link/libiberty/objalloc.c:105: warning: implicit declaration of function `__builtin_offsetof'
../../ipa_link/libiberty/objalloc.c:105: error: syntax error before "struct"
make[3]: *** [objalloc.o] Error 1

其中OBJALLOC_ALIGN定义如下
struct objalloc_align { char x; double d; };
#if defined (__STDC__) && __STDC__
#ifndef offsetof
#include <stddef.h>
#endif
#endif
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
#endif
#define OBJALLOC_ALIGN offsetof (struct objalloc_align, d)
air_snake 2008-07-23
  • 打赏
  • 举报
回复
呵呵,不是很明白。

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧