1

I have this struct:

typedef struct {
    void (*func)(instruction);
    union {
        double db;
        char ch;
    };
} instruction;

Within my source file, I have an array of these instructions. My question is, how would I loop through that array and execute each struct's function?

I thought I knew how to do this, but the fact that the instruction function has a parameter that is an instruction seems to cause problems. As such, this does not work:

int i;
    for (i = 0; i <= instr_count; i++) {
        (*instr[i].func) (instr[i]);
        }

instr being the array of instructions.

Here is the function that populates the array. The array itself is declared at the top of the file.

void read_instructions()
{
    char* str;
    char *instruct;
    int n;
    char c;
    instruction next = {0};
    while (!feof(datafile)) {
        // Fetch the next string
        // if (push or pop), get the next argument
        // create instructiwn and add to instruction array
        str = get_next_string();

        instruct = strtok (str, " ");

        if (strncmp (instruct, "P", 1) == 0) {
            char *arg = strtok (NULL, " ");
            if (strncmp (str, "PUSH", 4) == 0) {
                next.func = pushFunc;
            }
            else {
                next.func = popFunc;
            }
            n = arg[0];
            if (n > 64 || n < 71)
                next.ch = n;
            else {
                double n;
                scanf ("%lf", arg, n);
                next.db = n;
            }
            instr[instr_count] = next;
            instr_count++;
        }
        else {
            c = instruct[0];


            switch (c) {
            case 'A' :
                next.func = addFunc;
                break;
            case 'S' :
                next.func = subFunc;
                break;
            case 'M' :
                next.func = multFunc;
                break;
            case 'D' :
                next.func = divFunc;
                break;
            default :
                break;
            }
            instr[instr_count] = next;
            instr_count++;
        }
    }
    fclose (datafile);
}

As a quick explanation, this code takes a file of "intermediate code", determines the instruction, and creates an instruction struct for each that holds the proper function pointer.

Running the code gives me this runtime error:

"Unhandled exception at 0x00000000 in Interpreter.exe: 0xC0000005: Access violation." (Compiled using VS 2010).

2
  • 1
    Looks like one of your function pointers was a NULL pointer. Commented Jul 9, 2012 at 2:55
  • This does crash in exactly which line? Commented Jul 9, 2012 at 8:13

2 Answers 2

1

Depending on whether instr is an array of instruction or instruction *, you want either

instr[i].func(&instr[i]);

or

instr[i]->func(instr[i]);

I suspect you want the first (array of instruction), as what you have will compile (with warnings on most compilers) for that; you'll just get garbage for the argument in the function.

The exception you are getting suggests that this isn't your problem, however -- you probably have an instruction with a NULL func pointer in your array.

Edit

Looks like you're making the classic while(!feof(input)) error -- whenever you see this, its almost always wrong.

The problem is that after you read the last line of the input, feof will still return false -- it won't return true until you've tried to read PAST the end of the input an received an EOF result. So you'll get an extra iteration of the loop with an blank line after the end of your input code, which probably results in the crash you see.

what you probably want is something like while (!(str = get_next_string())) or perhaps while (!(str = fgets(buffer, sizeof(buffer), datafile)))

Sign up to request clarification or add additional context in comments.

1 Comment

I've editted my post to include the function that is suppose to fill the array.
0

It's hard to tell without looking at the rest of your code, but the address listed in the error (0x00000000) implies that one of your function pointers in the array is NULL.

Could you verify that your array is correctly initialized before you walk through it?

9 Comments

I've editted my post to include the function that is suppose to fill the array.
@Troncoso Consider the case of something other than 'P', 'A', 'S', 'M', or 'D' being encountered. What does your input look like?
My input is very strict. The instructions are only Pop, Push, Add, Sub, Mult, and Div. Each instruction is on a separate line. Because this is a class assignment, it's not meant to test for incorrect input.
@Troncoso ...and you're absolutely certain that instruct[0] is always one of 'P', 'A', 'S', 'M', or 'D'? Either way, you should try running this under a debugger to see where the crash is occurring, and ideally to step through your logic to understand why.
Yes. Every line start with one of those characters. I have a counter initialized to 0 and increases each time an instruction is added. The crash occurs at that line I'm trying to write. Without it, the program compiles just fine.
|

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.