Megatron
Megatron

Reputation: 2949

C dynamic array initialization problem

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

}

Upvotes: 0

Views: 898

Answers (5)

Puppy
Puppy

Reputation: 146930

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.

Upvotes: 0

pmg
pmg

Reputation: 108938

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 */

Upvotes: 2

John Bode
John Bode

Reputation: 123468

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.

Upvotes: 1

Michael Burr
Michael Burr

Reputation: 340218

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().

Upvotes: 1

tyree731
tyree731

Reputation: 483

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.

Upvotes: 0

Related Questions