Skip to main content
added context
Source Link
noearchimede
  • 482
  • 2
  • 7
  • 20

I have an array of function pointers:

unsigned int dimension = 8;
void (* myFunctions [dimension])();

This array is a member of a class.:

class myClass {
    
    static const unsigned int dimension;

    void (* myFunctions [dimension])();
}

and in a cpp file:

const unsigned int dimension = 8;

It's size is known at compile time, but it's values will be added through a public method in unknown parts of the code. I need to be able to find out at any time whether there is a valid pointer at some index in that array at any time, and my idea was to do that by comparing the pointer against nullptr: if it doesn't equals it then I'll assume that it holds a valid function pointer.

I was wondering if I need to initialize the array before making that kind of speculation. How is this array initialized if I only write the above two lines of code? Can I assume something like myFunctions[x] == nullptr is true in that case? Or do I need to do an explicit initialization like the following one?

for(int i = 0; i < dimension; i++)
    myFunctions[i] = nullptr;

I have an array of function pointers:

unsigned int dimension = 8;
void (* myFunctions [dimension])();

This array is a member of a class. It's size is known at compile time, but it's values will be added through a public method in unknown parts of the code. I need to be able to find out at any time whether there is a valid pointer at some index in that array at any time, and my idea was to do that by comparing the pointer against nullptr: if it doesn't equals it then I'll assume that it holds a valid function pointer.

I was wondering if I need to initialize the array before making that kind of speculation. How is this array initialized if I only write the above two lines of code? Can I assume something like myFunctions[x] == nullptr is true in that case? Or do I need to do an explicit initialization like the following one?

for(int i = 0; i < dimension; i++)
    myFunctions[i] = nullptr;

I have an array of function pointers:

unsigned int dimension = 8;
void (* myFunctions [dimension])();

This array is a member of a class:

class myClass {
    
    static const unsigned int dimension;

    void (* myFunctions [dimension])();
}

and in a cpp file:

const unsigned int dimension = 8;

It's size is known at compile time, but it's values will be added through a public method in unknown parts of the code. I need to be able to find out at any time whether there is a valid pointer at some index in that array at any time, and my idea was to do that by comparing the pointer against nullptr: if it doesn't equals it then I'll assume that it holds a valid function pointer.

I was wondering if I need to initialize the array before making that kind of speculation. How is this array initialized if I only write the above two lines of code? Can I assume something like myFunctions[x] == nullptr is true in that case? Or do I need to do an explicit initialization like the following one?

for(int i = 0; i < dimension; i++)
    myFunctions[i] = nullptr;
Source Link
noearchimede
  • 482
  • 2
  • 7
  • 20

Initialization of function pointer array

I have an array of function pointers:

unsigned int dimension = 8;
void (* myFunctions [dimension])();

This array is a member of a class. It's size is known at compile time, but it's values will be added through a public method in unknown parts of the code. I need to be able to find out at any time whether there is a valid pointer at some index in that array at any time, and my idea was to do that by comparing the pointer against nullptr: if it doesn't equals it then I'll assume that it holds a valid function pointer.

I was wondering if I need to initialize the array before making that kind of speculation. How is this array initialized if I only write the above two lines of code? Can I assume something like myFunctions[x] == nullptr is true in that case? Or do I need to do an explicit initialization like the following one?

for(int i = 0; i < dimension; i++)
    myFunctions[i] = nullptr;