C can compilers optimize function call through function calls

I have functions, which take a pointer to a struct, which are only accessed through function pointers in an array, can the compiler recognise this or do i need to make the structure volatile, to not break with optimizations?

struct data {
    bool running;
};

int (*functions[16])(struct data *);

struct data data = {
    .running = true;
};

while (data.running) {
    functions[rand()%16](&data);
}

Rand is just used as an example to say that it isnt known at compile time which function gets called.



Read more here: https://stackoverflow.com/questions/65725279/c-can-compilers-optimize-function-call-through-function-calls

Content Attribution

This content was originally published by Björn Max Jakobsen at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: