C/C++ 获取结构体中变量的偏移量

某些特殊需求下,我们需要知道某个变量在其结构体中的偏移位置。

通常的做法就是定义一个宏变量,如下:

#define OFFSET(structure, member) ((int64_t)&((structure*)0)->member) // 64位系统
#define OFFSET(structure, member) ((int32_t)&((structure*)0)->member) // 32位系统

使用示例:

#include <stdio.h>
#include <stdint.h>

#define OFFSET(structure, member) ((int64_t)&((structure*)0)->member)

typedef struct _A {
    int32_t a;
    int64_t b;
    int64_t c;
    int32_t d;
} A;

int main()
{
    printf("offset b: %ld\n", OFFSET(A, b));
    printf("size: %ld\n", sizeof(A));
    return 0;
}

执行结果:

offset b: 8
size: 32

分析:

这里变量 b 的偏移量是 8 个字节,是因为内存对齐的原因,变量 a 虽然是 int32_t 类型,但是也会占用 8 个字节。如果我们直接使用 sizeof(int32_t) 作为变量 b 的偏移量是很容易出错的(比如没有考虑到内存对齐),而且变量多的时候也是很容易出错的,所以建议使用上例中的方法,既方便也不容易出错。

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的PL/0编译程序的语法分析程序,使用C语言实现。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #define MAX_SYMBOL_TABLE_SIZE 1000 #define MAX_CODE_LENGTH 1000 #define MAX_IDENTIFIER_LENGTH 10 // Token类型 enum TokenType { IDENTIFIER, NUMBER, RESERVED_WORD_BEGIN, RESERVED_WORD_END, RESERVED_WORD_VAR, OPERATOR_ASSIGN, OPERATOR_PLUS, OPERATOR_MINUS, PUNCTUATION_SEMICOLON, PUNCTUATION_PERIOD, SYMBOL_UNKNOWN }; // Token结构体,表示语法分析的一个单词 struct Token { enum TokenType type; // 单词类型 char value[MAX_IDENTIFIER_LENGTH]; // 单词值 }; // 符号表结构体,表示程序的标识符 struct SymbolTable { char name[MAX_IDENTIFIER_LENGTH]; // 标识符名称 int level; // 标识符所在层次 int address; // 标识符在栈的地址 }; // 代码结构体,表示程序的一条指令 struct Code { int op; // 操作码 int l; // 层数差 int m; // 栈偏移量 }; // 词法分析函数,根据输入的字符串生成Token序列 int lex(char *input, struct Token *tokens) { int i = 0, j = 0, k = 0; while (input[i] != '\0') { // 跳过空格、制表符、换行符等空白符 if (input[i] == ' ' || input[i] == '\t' || input[i] == '\r' || input[i] == '\n') { i++; continue; } // 处理标识符或保留字 if ((input[i] >= 'a' && input[i] <= 'z') || (input[i] >= 'A' && input[i] <= 'Z')) { j = 0; while ((input[i] >= 'a' && input[i] <= 'z') || (input[i] >= 'A' && input[i] <= 'Z') || (input[i] >= '0' && input[i] <= '9')) { tokens[k].value[j] = input[i]; i++; j++; } tokens[k].value[j] = '\0'; if (strcmp(tokens[k].value, "begin") == 0) { tokens[k].type = RESERVED_WORD_BEGIN; } else if (strcmp(tokens[k].value, "end") == 0) { tokens[k].type = RESERVED_WORD_END; } else if (strcmp(tokens[k].value, "var") == 0) { tokens[k].type = RESERVED_WORD_VAR; } else { tokens[k].type = IDENTIFIER; } k++; continue; } // 处理数字 if (input[i] >= '0' && input[i] <= '9') { j = 0; while (input[i] >= '0' && input[i] <= '9') { tokens[k].value[j] = input[i]; i++; j++; } tokens[k].value[j] = '\0'; tokens[k].type = NUMBER; k++; continue; } // 处理运算符或赋值符号 if (input[i] == ':') { i++; if (input[i] == '=') { tokens[k].type = OPERATOR_ASSIGN; tokens[k].value[0] = ':'; tokens[k].value[1] = '='; tokens[k].value[2] = '\0'; i++; k++; continue; } else { tokens[k].type = SYMBOL_UNKNOWN; tokens[k].value[0] = ':'; tokens[k].value[1] = '\0'; k++; continue; } } if (input[i] == '+') { tokens[k].type = OPERATOR_PLUS; tokens[k].value[0] = '+'; tokens[k].value[1] = '\0'; i++; k++; continue; } if (input[i] == '-') { tokens[k].type = OPERATOR_MINUS; tokens[k].value[0] = '-'; tokens[k].value[1] = '\0'; i++; k++; continue; } // 处理分号和句号 if (input[i] == ';') { tokens[k].type = PUNCTUATION_SEMICOLON; tokens[k].value[0] = ';'; tokens[k].value[1] = '\0'; i++; k++; continue; } if (input[i] == '.') { tokens[k].type = PUNCTUATION_PERIOD; tokens[k].value[0] = '.'; tokens[k].value[1] = '\0'; i++; k++; continue; } // 处理未知符号 tokens[k].type = SYMBOL_UNKNOWN; tokens[k].value[0] = input[i]; tokens[k].value[1] = '\0'; i++; k++; } return k; } // 语法分析函数,根据Token序列生成语法树 void parse(struct Token *tokens, int num_tokens) { int i = 0; bool has_error = false; while (i < num_tokens) { // 处理变量声明 if (tokens[i].type == RESERVED_WORD_VAR) { i++; while (tokens[i].type == IDENTIFIER) { i++; if (tokens[i].type == SYMBOL_UNKNOWN || tokens[i].type == PUNCTUATION_SEMICOLON) { break; } if (tokens[i].type != OPERATOR_ASSIGN) { printf("Error: Expecting := at line %d\n", __LINE__); has_error = true; break; } i++; if (tokens[i].type != NUMBER) { printf("Error: Expecting number at line %d\n", __LINE__); has_error = true; break; } i++; } if (tokens[i].type != PUNCTUATION_SEMICOLON) { printf("Error: Expecting ; at line %d\n", __LINE__); has_error = true; break; } i++; continue; } // 处理语句 if (tokens[i].type == IDENTIFIER) { i++; if (tokens[i].type != OPERATOR_ASSIGN) { printf("Error: Expecting := at line %d\n", __LINE__); has_error = true; break; } i++; if (tokens[i].type != NUMBER) { printf("Error: Expecting number at line %d\n", __LINE__); has_error = true; break; } i++; if (tokens[i].type != PUNCTUATION_SEMICOLON) { printf("Error: Expecting ; at line %d\n", __LINE__); has_error = true; break; } i++; continue; } // 处理begin-end语句块 if (tokens[i].type == RESERVED_WORD_BEGIN) { i++; while (tokens[i].type != RESERVED_WORD_END) { if (tokens[i].type == IDENTIFIER) { i++; if (tokens[i].type != OPERATOR_ASSIGN) { printf("Error: Expecting := at line %d\n", __LINE__); has_error = true; break; } i++; if (tokens[i].type != NUMBER) { printf("Error: Expecting number at line %d\n", __LINE__); has_error = true; break; } i++; if (tokens[i].type != PUNCTUATION_SEMICOLON) { printf("Error: Expecting ; at line %d\n", __LINE__); has_error = true; break; } i++; continue; } printf("Error: Expecting identifier at line %d\n", __LINE__); has_error = true; break; } if (tokens[i].type != RESERVED_WORD_END) { printf("Error: Expecting end at line %d\n", __LINE__); has_error = true; break; } i++; if (tokens[i].type != PUNCTUATION_PERIOD) { printf("Error: Expecting . at line %d\n", __LINE__); has_error = true; break; } i++; continue; } printf("Error: Unexpected token at line %d\n", __LINE__); has_error = true; break; } if (!has_error) { printf("Parsing finished successfully!\n"); } } int main() { char input[] = "var x; begin x := 1; end."; struct Token tokens[MAX_CODE_LENGTH]; int num_tokens = lex(input, tokens); parse(tokens, num_tokens); return 0; } ``` 在这里,我们定义了Token、SymbolTable和Code结构体,分别表示语法分析的一个单词、程序的标识符和程序的一条指令。然后实现了一个lex函数,根据输入的字符串生成Token序列。接下来实现了parse函数,根据Token序列生成语法树。最后在main函数调用lex和parse函数进行测试。 需要注意的是,这只是一个简单的PL/0编译程序的语法分析程序,可能并不完整或准确。具体的实现方式还需要根据PL/0编译程序的具体语法规则来进行调整和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值