为什么不能这样呢?

WJN92 2015-08-27 03:55:01
#define kb 1024
tL1[ 4* kb / sizeof( L2 ) ];

里面L2,kb已经定义了,当时编译报错,为什么呢?
...全文
231 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-08-27
  • 打赏
  • 举报
回复
VC编译选项加/EP /P,重新编译,查看宏展开后对应的.i文件。gcc加-E http://bbs.csdn.net/topics/391003898
Eleven 2015-08-27
  • 打赏
  • 举报
回复
引用 楼主 WJN92 的回复:
#define kb 1024 tL1[ 4* kb / sizeof( L2 ) ]; 里面L2,kb已经定义了,当时编译报错,为什么呢?
你这个是定义数组吗?类型呢?
WJN92 2015-08-27
  • 打赏
  • 举报
回复
引用 14 楼 zhao4zhong1 的回复:
对齐请使用 #pragma pack(16)
#include <stdio.h>
#define field_offset(s,f) (int)(&(((struct s *)(0))->f))
struct AD  { int a; char b[13]; double c;};
#pragma pack(push)
#pragma pack(1)
struct A1  { int a; char b[13]; double c;};
#pragma pack(2)
struct A2  { int a; char b[13]; double c;};
#pragma pack(4)
struct A4  { int a; char b[13]; double c;};
#pragma pack(8)
struct A8  { int a; char b[13]; double c;};
#pragma pack(16)
struct A16 { int a; char b[13]; double c;};
#pragma pack(pop)
int main() {
    printf("AD.a %d\n",field_offset(AD,a));
    printf("AD.b %d\n",field_offset(AD,b));
    printf("AD.c %d\n",field_offset(AD,c));
    printf("\n");
    printf("A1.a %d\n",field_offset(A1,a));
    printf("A1.b %d\n",field_offset(A1,b));
    printf("A1.c %d\n",field_offset(A1,c));
    printf("\n");
    printf("A2.a %d\n",field_offset(A2,a));
    printf("A2.b %d\n",field_offset(A2,b));
    printf("A2.c %d\n",field_offset(A2,c));
    printf("\n");
    printf("A4.a %d\n",field_offset(A4,a));
    printf("A4.b %d\n",field_offset(A4,b));
    printf("A4.c %d\n",field_offset(A4,c));
    printf("\n");
    printf("A8.a %d\n",field_offset(A8,a));
    printf("A8.b %d\n",field_offset(A8,b));
    printf("A8.c %d\n",field_offset(A8,c));
    printf("\n");
    printf("A16.a %d\n",field_offset(A16,a));
    printf("A16.b %d\n",field_offset(A16,b));
    printf("A16.c %d\n",field_offset(A16,c));
    printf("\n");
    return 0;
}
//AD.a 0
//AD.b 4
//AD.c 24
//
//A1.a 0
//A1.b 4
//A1.c 17
//
//A2.a 0
//A2.b 4
//A2.c 18
//
//A4.a 0
//A4.b 4
//A4.c 20
//
//A8.a 0
//A8.b 4
//A8.c 24
//
//A16.a 0
//A16.b 4
//A16.c 24
//
//
如果要计算4K一页呢?链表
worldy 2015-08-27
  • 打赏
  • 举报
回复
tL1[ 4* kb / sizeof( L2 ) ]; 这个算是什么?定义数组?数据类型呢?
schlafenhamster 2015-08-27
  • 打赏
  • 举报
回复
#define kb 1024 #define tL1 int // type of L1 #define tL2 int // type of L2 tL1 L1[4* kb / sizeof(tL2)]={0};
schlafenhamster 2015-08-27
  • 打赏
  • 举报
回复
根据 32 位 64位 定义 #def 32位 #define def_NumDetectUnitLineTableSize xxxx #else #define def_NumDetectUnitLineTableSize yyyy #endif
赵4老师 2015-08-27
  • 打赏
  • 举报
回复
对齐请使用 #pragma pack(16)
#include <stdio.h>
#define field_offset(s,f) (int)(&(((struct s *)(0))->f))
struct AD  { int a; char b[13]; double c;};
#pragma pack(push)
#pragma pack(1)
struct A1  { int a; char b[13]; double c;};
#pragma pack(2)
struct A2  { int a; char b[13]; double c;};
#pragma pack(4)
struct A4  { int a; char b[13]; double c;};
#pragma pack(8)
struct A8  { int a; char b[13]; double c;};
#pragma pack(16)
struct A16 { int a; char b[13]; double c;};
#pragma pack(pop)
int main() {
    printf("AD.a %d\n",field_offset(AD,a));
    printf("AD.b %d\n",field_offset(AD,b));
    printf("AD.c %d\n",field_offset(AD,c));
    printf("\n");
    printf("A1.a %d\n",field_offset(A1,a));
    printf("A1.b %d\n",field_offset(A1,b));
    printf("A1.c %d\n",field_offset(A1,c));
    printf("\n");
    printf("A2.a %d\n",field_offset(A2,a));
    printf("A2.b %d\n",field_offset(A2,b));
    printf("A2.c %d\n",field_offset(A2,c));
    printf("\n");
    printf("A4.a %d\n",field_offset(A4,a));
    printf("A4.b %d\n",field_offset(A4,b));
    printf("A4.c %d\n",field_offset(A4,c));
    printf("\n");
    printf("A8.a %d\n",field_offset(A8,a));
    printf("A8.b %d\n",field_offset(A8,b));
    printf("A8.c %d\n",field_offset(A8,c));
    printf("\n");
    printf("A16.a %d\n",field_offset(A16,a));
    printf("A16.b %d\n",field_offset(A16,b));
    printf("A16.c %d\n",field_offset(A16,c));
    printf("\n");
    return 0;
}
//AD.a 0
//AD.b 4
//AD.c 24
//
//A1.a 0
//A1.b 4
//A1.c 17
//
//A2.a 0
//A2.b 4
//A2.c 18
//
//A4.a 0
//A4.b 4
//A4.c 20
//
//A8.a 0
//A8.b 4
//A8.c 24
//
//A16.a 0
//A16.b 4
//A16.c 24
//
//
WJN92 2015-08-27
  • 打赏
  • 举报
回复
引用 12 楼 zhao4zhong1 的回复:
宏是一种语法糖。 语法糖越甜,编译调试查错越苦! 把有限的生命浪费在品尝/品鉴无穷多种的语法糖中,我认为不值当。
但是我这样灵活啊,好像在编译64位 和 32位的代码时候 因为希望能存优化对齐,所以才使用宏来计算,否则的话,我要把所有的定义都要改一遍,还不知道有没有遗落
zhusg 2015-08-27
  • 打赏
  • 举报
回复
你这语句是对tL1赋值还是取值呢?
赵4老师 2015-08-27
  • 打赏
  • 举报
回复
宏是一种语法糖。 语法糖越甜,编译调试查错越苦! 把有限的生命浪费在品尝/品鉴无穷多种的语法糖中,我认为不值当。
WJN92 2015-08-27
  • 打赏
  • 举报
回复
引用 10 楼 schlafenhamster 的回复:
Incomplete Types An incomplete type is a type that describes an identifier but lacks information needed to determine the size of the identifier. An “incomplete type” can be: A structure type whose members you have not yet specified. A union type whose members you have not yet specified. An array type whose dimension you have not yet specified. The void type is an incomplete type that cannot be completed. To complete an incomplete type, specify the missing information. The following examples show how to create and complete the incomplete types. To create an incomplete structure type, declare a structure type without specifying its members. In this example, the ps pointer points to an incomplete structure type called student. struct student *ps; To complete an incomplete structure type, declare the same structure type later in the same scope with its members specified, as in struct student { int num; } /* student structure now completed */ To create an incomplete array type, declare an array type without specifying its repetition count. For example: char a[]; /* a has incomplete type */ To complete an incomplete array type, declare the same name later in the same scope with its repetition count specified, as in char a[25]; /* a now has complete type */ 注意 def_NumDetectUnitLineTableSize 必须 有 值
我想让编译器帮我计算而已
schlafenhamster 2015-08-27
  • 打赏
  • 举报
回复
Incomplete Types An incomplete type is a type that describes an identifier but lacks information needed to determine the size of the identifier. An “incomplete type” can be: A structure type whose members you have not yet specified. A union type whose members you have not yet specified. An array type whose dimension you have not yet specified. The void type is an incomplete type that cannot be completed. To complete an incomplete type, specify the missing information. The following examples show how to create and complete the incomplete types. To create an incomplete structure type, declare a structure type without specifying its members. In this example, the ps pointer points to an incomplete structure type called student. struct student *ps; To complete an incomplete structure type, declare the same structure type later in the same scope with its members specified, as in struct student { int num; } /* student structure now completed */ To create an incomplete array type, declare an array type without specifying its repetition count. For example: char a[]; /* a has incomplete type */ To complete an incomplete array type, declare the same name later in the same scope with its repetition count specified, as in char a[25]; /* a now has complete type */ 注意 def_NumDetectUnitLineTableSize 必须 有 值
赵4老师 2015-08-27
  • 打赏
  • 举报
回复
引用 8 楼 WJN92 的回复:
[quote=引用 5 楼 zhao4zhong1 的回复:] VC编译选项加/EP /P,重新编译,查看宏展开后对应的.i文件。gcc加-E http://bbs.csdn.net/topics/391003898
设置过了,好像也不行呢 Error:incomplete type is not allowed[/quote] 前提是能编译通过。 项目、属性、配置属性、C/C++、预处理器、预处理到文件:是,预处理取消显示行号:是
WJN92 2015-08-27
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
VC编译选项加/EP /P,重新编译,查看宏展开后对应的.i文件。gcc加-E http://bbs.csdn.net/topics/391003898
设置过了,好像也不行呢 Error:incomplete type is not allowed
WJN92 2015-08-27
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
VC编译选项加/EP /P,重新编译,查看宏展开后对应的.i文件。gcc加-E http://bbs.csdn.net/topics/391003898
应该怎样设置呢? 我用的是vs2015, 设置的项目叫什么名字呢? 谢谢老师
WJN92 2015-08-27
  • 打赏
  • 举报
回复
引用 4 楼 VisualEleven 的回复:
[quote=引用 楼主 WJN92 的回复:] #define kb 1024 tL1[ 4* kb / sizeof( L2 ) ]; 里面L2,kb已经定义了,当时编译报错,为什么呢?
你这个是定义数组吗?类型呢?[/quote] 有的,我忘记复制而已 #define def_NumDetectUnitLineTableSize (4 * kb / sizeof(FE_DetectUnitL1)) struct FE_DetectUnitLT { DWORD dwPointer; FE_DetectUnitL1 tL1DetectUnit[def_NumDetectUnitLineTableSize];

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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