-1

I am using Windows 10, Visual Studio 2015.

Alright so anyways here is my code and I am getting this error:

uninitialized local variable 'driver'

I am just following a tutorial from the main MySQL website (The example) and that seems to happen, so can someone explain why its doing that or tell me a fix. :) I tried to make it static, it let to go through until I clicked the button then I got a invalid write to memory message. Hopefully this is enough for you to tell me whats up.

  LRESULT CALLBACK WndProc(...)
    {
       ...
       switch (LOWORD(wparam))
                    {
                        case SQL_BUTTON_LAUNCH:
                            using namespace sql;
                            Driver *driver;
                            Connection *con;
                            Statement *stmt;
                            ResultSet *res;

                            con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
                            con->setSchema("test");

                            stmt = con->createStatement();
                            res = stmt->executeQuery("SELECT 'Hello World!' AS _message");


                            delete res;
                            delete stmt;
                            delete con;
                            //SQLConnection();
                            break;
                    }

    }
  • 2
    Error message tells you all that is needed. Note that int* ip; *ip = 1; gives the same error. – Karlis Olte Oct 28 '15 at 22:59
  • I know I need to give it a value, but I don't exactly know what type of value to give it. I looked at it in the debugger and the Driver Variable is NULL (It will compile if I assign it a variable). When it tries to use Driver->Connect it gets the writing to memory error. – Trevin Corkery Oct 28 '15 at 23:02
  • 1
    `driver` is an uninitialized pointer. The statement `driver->connect( ... );` dereferences the pointer. This pointer points *somewhere*. The compiler is telling you, that you need an instance of type `Driver`, which you never create. – IInspectable Oct 28 '15 at 23:06
  • What's unclear about the message? – Lightness Races in Orbit Oct 29 '15 at 00:21

1 Answers1

4

You need to add

driver = get_driver_instance();

before calling

driver->connect (..)

Otherwise, Driver *driver; just declares a pointer. It won't have a valid value assigned to it. So the call connect(..) through it dereferences this pointer which in turn results in undefined behavior. A specific case of the undefined behavior is memory access violation error as mentioned in the question.

When get_driver_instance () is called, it executes the steps required to acquire a valid pointer and returns it, which you can then use for connecting.

ramana_k
  • 1,933
  • 2
  • 10
  • 14
  • `driver` is not a `NULL` pointer. It has an indiscriminate value, just as you wrote in the sentence before. – IInspectable Oct 29 '15 at 18:27
  • @IInspectable, You are right. It was sloppy use of language on my part. Let me make it more precise. – ramana_k Oct 29 '15 at 18:31
  • C++ doesn't know what *"stack"* is. It does, however, introduce the concept of *undefined behavior*. Dereferencing a pointer that doesn't point to an object of appropriate type results in undefined behavior. An access violation is one manifestation of undefined behavior. Undefined behavior can also mean, that the code works as expected. – IInspectable Oct 29 '15 at 18:52
  • I think for the purpose of initialization, C++ distinguishes between static/global variables and other variables that are usually allocated on stack. The first type of variables get initialized to zero and the second type do not. http://stackoverflow.com/questions/17783210/when-are-static-and-global-variables-initialized – ramana_k Oct 29 '15 at 19:01
  • C++ distinguishes between objects, that have *static storage duration*, and those, that do not. Objects with static storage duration are zero-initialized. Regardless of whether you dereference a `nullptr`, or a pointer that doesn't point to an object of appropriate type, the outcome is *undefined behavior*. – IInspectable Oct 29 '15 at 19:13