0

I'm having a problem initializing an array of structs in my C program. Here's the function where it gets initialized:

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

Counter_Count is an integer global variable and SatCounterTable is declared earlier in the C source file as

static struct SatCounterTableEntry* SatCounterTable;

and if it's relevant this is my SatCounterTable struct

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

md_addr_t is just a label for an unsigned int corresponding to a memory address

The problem is that when I try and compile, I get the following error

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

And the array initialization in my IntitializeBPStructures() is on line 129. I'm not sure why this line is a problem. Any ideas?

EDIT:

Here's some additional lines of code around the function

    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++;

}
3
  • 1
    You also use WAY too many global variables. That stuff is totally deprecated. Commented Nov 16, 2010 at 19:04
  • The error might not on line 129. You might miss something somewhere near that line. Commented Nov 16, 2010 at 19:09
  • Do you have another type defined called SatCounterTable? It should be recognized by the compiler as an expression since it is a (global) variable. Commented Nov 16, 2010 at 19:09

5 Answers 5

2

You're missing a semicolon at line 126.


Edit: new idea

Do you perhaps have a #define with an extra =?

#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 */
Sign up to request clarification or add additional context in comments.

2 Comments

I can't find anywhere where I'm missing a semicolon, and if I comment out that line, th eprogram compiles fine. If I was missing a semicolon earlier, the compiler would complain wouldn't it?
That was it, Thanks. Sorry, I really don't program in C often heh
1

SatCounterTable is declared earlier in the C source file as

static struct SatCounterTableEntry* SatCounterTable;

Is that declaration made at file scope or is it within another function? If the latter, then the SatCounterTable name won't be visible inside InitializeBPStructures().

Comments

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

Ugh. Do me a favor and rewrite that as

SatCounterTable = malloc(sizeof *SatCounterTable * Counter_Count);

You really don't need to cast the result of malloc(); that hasn't been necessary since C89 was adopted. And using sizeof on the object being allocated rather than the type can save you some heartburn (if nothing else, it saves some keystrokes).

The error text suggests that something hasn't been defined properly prior to this call; for some reason it isn't recognizing SatCounterTable. I think pmg's on the right track. You must be missing a semicolon or a curly bracket or something prior to this call.

Comments

0

The C compiler you are using has some reason to believe that SatCounterTable is not an lvalue or primary expression. Given how your variables are named (confusingly I might add), is it possible that you defined a variable at a closer scope also with the name SatCounterTable, such that SatCounterTable is not an assignable expression?

Edit: I would also seriously consider pmg's answer.

Comments

0

I compiled this code:

#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() {
}

You must have errors elsewhere in your code. Have I mentioned how incredibly hideous this design is? You should really be using objects for this.

1 Comment

I can't. This is for a Computer Arc assignment, and we have to use the SimpleScalar simulator, which is C. This is also the only source file I can change. pmg had the right answer though, I had an = sign in a #define

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.