On 7 Feb 2009 at 1:30, Barry Schwarz wrote:
> On Fri, 6 Feb 2009 15:37:18 -0800 (PST),
> wrote:
>> if (!mysql_real_connect(conn, server, user, password, database, 0,
>>NULL, 0)) {
>
> conn was never assigned a value. This statement invokes undefined
> behavior by trying to pass that (non-existent) value to the function.
As the OP described himself as "fairly new to C", it's probably worth
unpacking that rather cryptic statement written in standardese, as this
is a very common setup in C APIs.
Very frequently, libraries need to work with compound data structures -
what in other languages would be classes. Of course, C doesn't have
constructors, destructors, methods and the rest, so a very common way of
"doing OO programming in C" is for the library to deal in struct
pointers.
The relevant struct, let's say struct foo, will be defined in the header
file you #include to use the library. Then in your code, you'll create a
struct foo variable, say myfoo (statically, on the stack, or got from
malloc() at your choice). The library then provides a function with a
name like foo_init(), which is effectively the constructor. You call it
like
foo_init(&myfoo); /* maybe you need to pass some parameters too */
The library then fills in the fields in the struct pointed to by myfoo,
allocates any resources it needs, and generally does everything you'd
expect a class ctor to do.
And from then on, you can pass the address of myfoo to the other library
functions to manipulate it as you want:
foo_work_magic(&myfoo, 42); /* morally, this is myfoo->work_magic(42); */
foo_print_answer(&myfoo, stdout);
etc.
Finally, you call a function like foo_clear(), which is the destructor
that frees any memory that foo_init() allocated, etc.
It's the fact that your program clearly violates this paradigm by
defining a pointer to MYSQL directly, rather than having an actual MYSQL
and taking its address, that rang alarm bells for Barry.
>>static void register_hooks(apr_pool_t* pool)
>>{
>> ap_hook_handler(mytest_handler, NULL, NULL, APR_HOOK_MIDDLE);
>
> Shouldn't this function use the parameter in some way?
I don't know about this particular function, but it's very common in
general for event handling functions to ignore some of their parameters,
so it's probably OK.
I agree with the suggestion that your direct problem (symbol not found)
is likely to be a linking problem caused by not linking with the
appropriate mysql library.