C中变量声明的排列决定了执行哪一个?
需要认真的帮助-我遇到了一个非常奇怪的问题 我的程序是一个在库文件C中变量声明的排列决定了执行哪一个?,c,pointers,struct,declaration,C,Pointers,Struct,Declaration,需要认真的帮助-我遇到了一个非常奇怪的问题 我的程序是一个在库文件Test.h中定义不同结构的程序。这是Test.h文件: typedef struct InstructionFields{ unsigned int op; /* opcode: bits 31-26 */ unsigned int rs; /* first register source operand: bits 25-21 */ unsigned int rt;
Test.h
中定义不同结构的程序。这是Test.h
文件:
typedef struct InstructionFields{
unsigned int op; /* opcode: bits 31-26 */
unsigned int rs; /* first register source operand: bits 25-21 */
unsigned int rt; /* second register source operand: bits 20-16 */
unsigned int rd; /* destination register: bits 15-11 */
unsigned int shamt; /* shift amount: bits 10-6 */
unsigned int immedOrAddress; /* constant or address: bits 15-0 */
unsigned int target; /* jump target: bits 25-0 */
unsigned int funct; /* function: bits 5-0 */
} IF, *IF_ptr;
typedef struct ControlSignalsList{
unsigned int RegDst; /* Register Destination */
unsigned int RegWrite; /* Write Register */
unsigned int ALUSrc; /* ALU Source */
unsigned int MemRead; /* Mem Read */
unsigned int MemWrite; /* Mem Write */
unsigned int MemtoReg; /* Memory to Register*/
unsigned int ALUControl ; /*4 bit ALU control */
unsigned int Branch; /* BEQ */
unsigned int Jump; /* Jump*/
} CS, *CS_ptr;
typedef struct RegisterStructure{
int t0; /* Register t0 */
int t1; /* Register t1 */
int t2; /* Register t2 */
int t3; /* Register t0 */
int ReadData1; /* Read Data 1 buffer */
int ReadData2; /* Read Data 2 buffer */
int WriteReg;
unsigned int readReg1; /* address of rs */
unsigned int readReg2; /* address of rt */
/* WriteReg */
} RG, *RG_ptr;
typedef struct ALUStructure{
int DataOut;
unsigned int Zero;
} AS, *AS_ptr;
#include "Test.h"
#include <stdio.h>
int main() {
CS_ptr controlSignals; // Line 5
IF_ptr iFields; // Line 6
RG_ptr registers; // Line 7
AS_ptr as; // Line 8
controlSignals->RegDst = 0; // Line 11
//registers->t0 = 0; // Line 12
//iFields->op = 0; // Line 13
//as->DataOut = 0; // Line 14
printf("This is fine\n");
return 0;
}
接下来,一个文件Test.c
包含这个库,声明使用这些结构的变量,然后将值分配给结构的不同字段
这是Test.c
文件:
typedef struct InstructionFields{
unsigned int op; /* opcode: bits 31-26 */
unsigned int rs; /* first register source operand: bits 25-21 */
unsigned int rt; /* second register source operand: bits 20-16 */
unsigned int rd; /* destination register: bits 15-11 */
unsigned int shamt; /* shift amount: bits 10-6 */
unsigned int immedOrAddress; /* constant or address: bits 15-0 */
unsigned int target; /* jump target: bits 25-0 */
unsigned int funct; /* function: bits 5-0 */
} IF, *IF_ptr;
typedef struct ControlSignalsList{
unsigned int RegDst; /* Register Destination */
unsigned int RegWrite; /* Write Register */
unsigned int ALUSrc; /* ALU Source */
unsigned int MemRead; /* Mem Read */
unsigned int MemWrite; /* Mem Write */
unsigned int MemtoReg; /* Memory to Register*/
unsigned int ALUControl ; /*4 bit ALU control */
unsigned int Branch; /* BEQ */
unsigned int Jump; /* Jump*/
} CS, *CS_ptr;
typedef struct RegisterStructure{
int t0; /* Register t0 */
int t1; /* Register t1 */
int t2; /* Register t2 */
int t3; /* Register t0 */
int ReadData1; /* Read Data 1 buffer */
int ReadData2; /* Read Data 2 buffer */
int WriteReg;
unsigned int readReg1; /* address of rs */
unsigned int readReg2; /* address of rt */
/* WriteReg */
} RG, *RG_ptr;
typedef struct ALUStructure{
int DataOut;
unsigned int Zero;
} AS, *AS_ptr;
#include "Test.h"
#include <stdio.h>
int main() {
CS_ptr controlSignals; // Line 5
IF_ptr iFields; // Line 6
RG_ptr registers; // Line 7
AS_ptr as; // Line 8
controlSignals->RegDst = 0; // Line 11
//registers->t0 = 0; // Line 12
//iFields->op = 0; // Line 13
//as->DataOut = 0; // Line 14
printf("This is fine\n");
return 0;
}
起初我认为这是一个统一化指针的问题,所以我将指针初始化为NULL
。但它没有修复任何问题,甚至产生了更多的分段错误。所以我就不理他们了
然后,我试着移动东西。奇怪的是,如果我将第7行移到第5行之前的顶部(首先声明RG_ptr
),并取消对第11行的注释,那么registers->t0=0
将被执行,分段错误被修复
其他指针声明也会发生同样的情况。只有当指针声明在开头为时,才会执行相应的字段赋值。未注释的任何其他字段分配都会导致分段错误
发生了什么事?如何解决这个问题?谢谢你的帮助 有问题的代码是:
RG_ptr registers;
registers->t0 = 0;
请注意,RG_ptr
是一个指针,它从未初始化。使用未初始化的指针是未定义的行为,您的程序会崩溃
NULL
指针实际上未“初始化”。这只会生成一个空指针,指向无效内存。如果没有崩溃,也无法取消引用该指针。通常,人们使用空指针来表示“未分配”或“不相关”的内容,而不是未初始化,这意味着指针可能包含垃圾数据
必须通过分配内存或将其指向现有的RG
结构来初始化该指针
例如:
RG registers;
RG_ptr registers_pointer = ®isters;
或:
请记住,如果您正在使用已分配的内存,您需要非常严格地确保所有分配都与
free
进行相应的释放,否则您将泄漏内存。您没有为结构分配空间。当取消对未初始化指针的引用时,您会得到预期的segfault
顺便说一句,更新的typedef指针
RG*寄存器;
RG_ptr寄存器
第二个声明看起来不像指针。这是难以发现的错误的潜在来源。使用星星作为指针。谢谢!这很有道理。绝对不能忘记内存分配!你完全正确!谢谢!更新的typedef指针是什么意思