首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Linux-64 (x86_64)整数值16被截断为0。

在Linux-64 (x86_64)整数值16被截断为0。
EN

Stack Overflow用户
提问于 2012-10-23 05:04:26
回答 1查看 174关注 0票数 0

在某些(并非所有)64位linux上,int值16被截断为0,我面临着一个奇怪的问题。

  • 是截断,是因为将int转换为空*,然后再转换为int (请理解这不是一个好做法)。

代码语言:javascript
复制
- 16 becomes hex-10 and [http://qb64.net/wiki/index.php?title=%26B](http://qb64.net/wiki/index.php?title=%26B)
- though void\* is 8 byte on 64-bit OS, it can estimate only LSBs?? is this the cause?? If so why this is not easily reproducible all the time??
- or this is due to big-endian?? 
- Sample source code as below

样本源

代码语言:javascript
复制
    int main() {
    int i = 0;
    void *ptr = NULL;
    printf("\nsizeof - void(*) : %d , int : %d", sizeof(void*), sizeof(int));

    for(i = 0; i < 20; i++) {
          ptr = &i;
          printf("\n%d", *((int*)ptr));
    }

    return 0;
    }
EN

回答 1

Stack Overflow用户

发布于 2012-10-23 17:35:55

要演示OP的注释中引用的文档中描述的问题,请将代码修改如下:

代码语言:javascript
复制
      ptr = &i;
      printf("\n%d", (int)ptr))

若要查看地址更改,请添加以下行:

代码语言:javascript
复制
      printf("\n%p", &i))

并且看到两个打印的值是不同的。

所有这一切都假定sizeof(void*)sizeof(int)一样大。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13023873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档