2010-11-16 94 views
0

我在初始化C程序中的结构数组时遇到问题。在此处,它被初始化函数:C动态数组初始化问题

void InitializeBPStructures() { 
    SatCounterTable = (struct SatCounterTableEntry *)malloc(sizeof(struct SatCounterTableEntry) * Counter_Count); 
} 

Counter_Count是一个整数全局变量和SatCounterTable较早的C源文件中声明为

static struct SatCounterTableEntry* SatCounterTable; 

,如果它是有关这是我SatCounterTable struct

struct SatCounterTableEntry { 
    enum SatCounter_State Predict_State; 
    md_addr_t tag; 
}; 

md_addr_t只是unsigned int的标签,对应于存储器地址

的问题是,当我尝试编译,我得到以下错误

sim-safe.c:129: error: expected expression before ‘=’ token 

在我IntitializeBPStructures()上线129的数组初始化我不知道为什么这条线是一个问题。有任何想法吗?

编辑:

下面是一些代码其他行绕功能

struct SatCounterTableEntry 
{ 
    enum SatCounter_State Predict_State; 
    md_addr_t tag; 
}; 

/* simulated registers */ 
static struct regs_t regs; 

/* simulated memory */ 
static struct mem_t *mem = NULL; 

/* track number of refs */ 
static counter_t sim_num_refs = 0; 

/* maximum number of inst's to execute */ 
static unsigned int max_insts; 

static struct SatCounterTableEntry* SatCounterTable; 

void InitializeBPStructures() 
{ 
    SatCounterTable = (struct SatCounterTableEntry *)malloc(sizeof(struct SatCounterTableEntry) * Counter_Count); 
} 

void BranchPredict(md_addr_t PC, md_addr_t nextPC, enum Branch_Result result) 
{ 
    if (result == N) 
     sim_num_mispred_static++; 
    if (result != (myrand() % 2)) 
     sim_num_mispred_random++; 

     sim_num_br++; 

} 
+1

你也使用WAY太多的全局变量。这些东西完全不赞成。 – Puppy 2010-11-16 19:04:34

+0

错误可能不在第129行。您可能会错过某条线附近的某处。 – arifwn 2010-11-16 19:09:28

+0

你有另一种类型定义为“SatCounterTable”吗?它应该被编译器认为是一个表达式,因为它是一个(全局)变量。 – 2010-11-16 19:09:52

回答

2

你缺少一个分号线126


编辑:新的想法

你可能有#define额外=

#define Counter_Count = 42; /* WRONG */ 
#define Counter_Count = 42 /* WRONG */ 
#define Counter_Count 42; /* WRONG, but it works some time */ 
#define Counter_Count 42 /* CORRECT */ 
+0

我找不到任何地方我错过了一个分号,如果我注释掉那行,那么这个eprogram编译得很好。如果我之前错过了分号,编译器会抱怨不是吗? – Megatron 2010-11-16 19:15:42

+1

就是这样,谢谢。对不起,我真的不用C编程经常嘿 – Megatron 2010-11-16 19:33:44

+0

很高兴我能帮上忙。玩的开心! – pmg 2010-11-16 19:39:32

0

您使用的C编译器有一些理由相信SatCounterTable不是左值或主表达式。鉴于你的变量是如何命名的(我可能会添加令人困惑的),是否有可能在名称SatCounterTable的附近定义了一个变量,这样SatCounterTable不是可赋值的表达式?

编辑:我也会认真考虑pmg的答案。

1

SatCounterTable较早的C源文件中声明为

static struct SatCounterTableEntry* SatCounterTable; 

是在文件范围内作出声明或者是另一个函数内?如果是后者,那么名称将在InitializeBPStructures()内部看不到。

+0

它的文件范围 – Megatron 2010-11-16 19:14:31

1
SatCounterTable = (struct SatCounterTableEntry *)malloc(sizeof(struct SatCounterTableEntry) * Counter_Count); 

呃。帮我一个忙,并重写为

SatCounterTable = malloc(sizeof *SatCounterTable * Counter_Count); 

你真的不需要投的malloc()的结果;自从C89通过以来,这一切都没有必要。并且在被分配的对象上使用sizeof而不是类型可以节省一些烧心(如果没有其他的话,它可以节省一些击键)。

错误文字暗示东西尚未在本次调用之前被正确定义;由于某种原因,它不承认SatCounterTable。我认为pmg在正确的轨道上。此通话之前,您必须丢失分号或大括号或其他内容。

+0

感谢您的提示。 – Megatron 2010-11-16 19:37:55

0

我编译这段代码:

#include <stdlib.h> 

typedef unsigned int md_addr_t; 
typedef unsigned int counter_t; 

int myrand() { return 0; } 

struct SatCounterTableEntry 
{ 
    enum SatCounter_State Predict_State; 
    md_addr_t tag; 
}; 

static unsigned int Counter_Count; 
static unsigned int sim_num_mispred_static; 
static unsigned int sim_num_mispred_random; 
static unsigned int sim_num_br; 
static const unsigned int N = 0; 

/* simulated registers */ 
static struct regs_t {} regs; 

/* simulated memory */ 
static struct mem_t *mem = NULL; 

/* track number of refs */ 
static counter_t sim_num_refs = 0; 

/* maximum number of inst's to execute */ 
static unsigned int max_insts; 

static struct SatCounterTableEntry* SatCounterTable; 

void InitializeBPStructures() 
{ 
    SatCounterTable = (struct SatCounterTableEntry *)malloc(sizeof(struct SatCounterTableEntry) * Counter_Count); 
} 

void BranchPredict(md_addr_t PC, md_addr_t nextPC, enum Branch_Result result) 
{ 
    if (result == N) 
     sim_num_mispred_static++; 
    if (result != (myrand() % 2)) 
     sim_num_mispred_random++; 

     sim_num_br++; 

} 

int main() { 
} 

你必须在你的代码的其他地方有错误。我提到这种设计有多难以置信吗?你应该真的使用这个对象。

+0

我不行。这是计算机弧分配,我们必须使用SimpleScalar模拟器,它是C.这也是我可以改变的唯一的源文件。 pmg虽然有正确的答案,但我在#define中签了一个=号 – Megatron 2010-11-16 19:40:01