In my C application, I have a logging method similar to this:
void logError(const char* module, const char* message, ... );
When this method is called, callers often do something like the following:
logError("foo", "bar");
// or even
#define FOOMODULE "foo"
logError(FOOMODULE, "bar");
No matter how the char*
module gets passed in, the char*
is static memory stored in the application binary.
My question is, does the compiler/linker collapse all instances of a statically declared string into a single declaration? As such, is it valid to assume that comparing the pointers ("foo" == FOOMODULE
) of my statically declared strings will always work as expected (assuming, of course, that the strings are equivalent as in my example above and that both are indeed statically allocated in the app's binary)?
I'm working on a way to filter on log messages as they come in. For example, perhaps I only want to see log messages from a single 'module'. It would be great to compare the pointers and not each byte in the string when deciding if I want to actually print a given log message.
Thanks!