Search code examples
c++strict-aliasing

Strict aliasing and callbacks with user data


I have a c++ library with a callback based C API. A callback type looks like this:

typedef struct {...} Result;
typedef void (*callback) (const Result* result, void* userData);

The user can register a callback like this, and can set a pointer to arbitrary data, and the library will pass that pointer back trough the callback.

My main concern here is: Is it violate the strict aliasing rule or not? Do I need to change the type of the userData to char*?


Solution

  • No, aliasing only applies when pointers are indirected, not when they are passed as pointer values. As long as the user behaves consistently you'll be fine.

    That is, the user must ensure that if they pass userData of actual type T, then they always cast it to T * in the callback function before accessing it.

    I'm assuming of course that you're not indirecting userData in your library, although again accessing it as a pointer (e.g. printf("DEBUG: %p", userData)) is fine.