Run time error

Hi all,

I have this piece of code and I run this on windows (vc++) and in Ubuntu (g++ 4.4). In Linux it works fine and works without any error (neither at compile time nor at runtime), but on windows it crashes with error "Variable used without initializing" at runtime. Any comments on the behavior? Is it because of the difference in OS?

static int COUNTER = 0;

class DeleteTest {
public:
DeleteTest() { cout << endl << "DeleteTest::DeleteTest(). Counter: " << ++COUNTER << endl; };
~DeleteTest() { cout << endl << "DeleteTest::~DeleteTest(). Counter: " << COUNTER-- << endl; };
};

int main(int argc, char **argv) {
cout << "Now constructing..." << endl;
DeleteTest **doubleArray;

for (int i = 0; i < 5; i++) {
doubleArray[i] = new DeleteTest[5];
}

cout << endl << "Now deleting..." << endl;

for (int i = 4; i >= 0; i--) {
delete[] doubleArray[i];
}

return 0;
}

Thanks,
-Bharath
closed account (z05DSL3A)
...on windows it crashes with error "Variable used without initializing" at runtime. Any comments on the behavior
Windows is doing the correct thing, Linux is letting you get away with dodgy code.

I'm assuming that you are trying to make a DeleteTest[5][5]. Your DeleteTest **doubleArray; has to point to an array of pointers to DeleteTest *, so you need to new an array of such a type.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

static int COUNTER = 0;

class DeleteTest 
{
public:
    DeleteTest() 
    {
        cout << endl << "DeleteTest::DeleteTest(). Counter: " << ++COUNTER << endl; 
    };
    
    ~DeleteTest() 
    { 
        cout << endl << "DeleteTest::~DeleteTest(). Counter: " << COUNTER-- << endl; 
    };
};

const int array_size = 5;

int main(int argc, char **argv) 
{
    cout << "Now constructing..." << endl;
    DeleteTest **doubleArray = new DeleteTest *[array_size];

    for (int i = 0; i < array_size; i++) 
    {
        doubleArray[i] = new DeleteTest[array_size];
    }

    cout << endl << "Now deleting..." << endl;

    for (int i = 0; i < array_size; i++) 
    {
        delete[] doubleArray[i];
    }
    delete[] doubleArray;

    return 0;
}

Last edited on
Hi Grey,

Thanks for that. That is what I am trying to achieve. Can you explain a little as to what you're trying to do with line 25? And why is g++ taking care of this internally? Well, I get a warning saying 'doubleArray' is being used without initialization and that's it.

-Bharath
closed account (z05DSL3A)
I'll try to explain a bit later, I have to get back to work...
From line 25, Grey is allocating and deleting memory for a pointer to a pointer (Double Pointers)

May be this thread can help you?

http://www.cplusplus.com/forum/beginner/8199/
Last edited on
closed account (z05DSL3A)
A DeleteTest ** is a pointer to a DeleteTest *, as you are doing a 2d array (an array of arrays), doubleArray has to point to an array of DeleteTest *s, that is what line 25 is doing.

Visually it would be something like:

doubleArray = new DeleteTest *[array_size];
┌──┐          ┌───┐
│  │─────────→│ 0 │
└──┘          ├───┤
              │ 1 │
              ├───┤
              │ 2 │
              ├───┤
              │ 3 │
              ├───┤
              │ 4 │
              └───┘

In the for loop

              doubleArray[0] = new DeleteTest[array_size];
┌──┐          ┌───┐            ┌─┬─┬
│  │─────────→│ 0 │───────────→│0│1│ ...
└──┘          ├───┤            └─┴─┴
              │ 1 │
              ├───┤
              │ 2 │
              ├───┤
              │ 3 │
              ├───┤
              │ 4 │
              └───┘

              doubleArray[1] = new DeleteTest[array_size];
┌──┐          ┌───┐            ┌─┬─┬─
│  │─────────→│ 0 │───────────→│0│1│ ...
└──┘          ├───┤            ├─┼─┼─
              │ 1 │───────────→│0│1│ ...
              ├───┤            └─┴─┴─
              │ 2 │
              ├───┤
              │ 3 │
              ├───┤
              │ 4 │
              └───┘


g++ doesn't really take care of this internally, it is just that your code doesn't do anything that would make it crash.

Always pay attention to warnings, preferably set your compiler to treat them as errors.
Thanks Grey for the explanation. This makes it clear.

-Bharath
Topic archived. No new replies allowed.