免费注册 查看新帖 |

Chinaunix

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

[C] sizeof参数问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-08-20 19:12 |只看该作者 |倒序浏览
  1. #include "stdio.h"
  2. #include "malloc.h"
  3. #include "assert.h"
  4. #include "stdarg.h"
  5. #include "stdint.h"
  6. #include "time.h"
  7. #include "limits.h"
  8. #include "string.h"
  9. #include "unistd.h"
  10. #include "stddef.h"
  11. #include "errno.h"


  12. typedef struct {                         /* type of structure for an element of a list */
  13.   char *ptr;                             /* pointer to the region */
  14.   int size;                              /* size of the effective region */
  15. } TCLISTDATUM;

  16. typedef struct {                         /* type of structure for an array list */
  17.   TCLISTDATUM *array;                    /* array of data */
  18.   int anum;                              /* number of the elements of the array */
  19.   int start;                             /* start index of used elements */
  20.   int num;                               /* number of used elements */
  21. } TCLIST;

  22. int main()
  23. {

  24.   TCLIST *list = (TCLIST *)malloc(sizeof(*list));
  25.   printf("%d\n", sizeof(TCLIST));
  26.   printf("%d\n", sizeof(*list));
  27.   printf("%d\n", sizeof(list));
  28.   printf("\n", sizeof(list));
  29.   list->anum = 1;
  30.   printf("%d\n", sizeof(*list->array));
  31.   printf("%d\n", sizeof(TCLISTDATUM));
  32.   printf("%d\n", sizeof(list->array));
  33.   printf("%d\n", sizeof(list->array[0]));
  34.   printf("%d\n", sizeof(list->array[1]));
  35.   printf("%d\n", sizeof(list->array[2]));
  36.   list->array = (TCLISTDATUM *)malloc(sizeof(TCLISTDATUM) * list->anum);
  37.   list->start = 0;
  38.   list->num = 0;
  39. }
复制代码
输出是这样的
[root@localhost www]# ./test
24
24
8

16
16
8
16
16
16


请问  printf("%d\n", sizeof(list));和  printf("%d\n", sizeof(list->array));怎么变成8了,
然后能解释一下每个输出的含义吗

论坛徽章:
0
2 [报告]
发表于 2014-08-20 19:36 |只看该作者
list是个指针,这里显示8B,说明这个应该是64位的环境,那么sizeof(list->array)也就好理解了,另外还要注意的这里是8字节对齐,分析一下,不难理解。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP