Can't instaciate double pointers?

Hi. This works the first time but if it tries do run the else clause i get a bad_alloc exception.How come I can't do this?
1
2
3
4
5
6
7
8
9
if(m_lMap == NULL)
{
    m_lMap = new int*[Y+1];
    m_lMap[0] = new int[arraySizeX];
}
else
{
   int** m_lMap  = new int*[Y+1];
}
Last edited on
This may or may not be related, but how/why are you declaring m_lMap again inside the else statement? It obviously already exists if you are testing it for NULL.
Sorry for that typo. made a little copy pasting tying to urge the essential in the problem. perhaps I should show some more code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void MapHandler::addMapRow(int Y){
if(m_lMap == NULL)
{
    m_lMap = new NavGraphNode*[Y+1];
    m_lMap[0] = new NavGraphNode[arraySizeX];
}
else
{
    int** tempMap = new int*[Y+1]; // this is where I get the bad_alloc error
    int x=0;
    for(int y=0;y<Y;y++)
    {
        tempMap[y] = new NavGraphNode[arraySizeX];
        x=0;
        for(;x<arraySizeX;x++)
        {
            tempMap[y][x] = m_lMap[y][x];
        }
    }
}
}
//where m_lMap is a int**. 
Last edited on
The question wasn't answered.

//where m_lMap is a int**.

is this in scope? global?

I mean if you copy/paste line 9 into a sample program I would assume it works fine, so something else would be the problem.
Last edited on
m_lMap is in gloabal scope, MapHandler has the variable as private.

hmm... only thing I do elsewhere is to alter the NavGraphs that were created in the m_lMap the first time. And when I've done that I'm trying to add new rows to the m_lMap somehow.
This was the only way I could figure you'd do it
Topic archived. No new replies allowed.