user1672690
user1672690

Reputation: 23

allocated but not initialized

I have met aN allocated but not initialized issue.

here is part of the code:

void test2(vector<string> names, int num) // just for test
{
    const char  **tmp = new const char *[num]; // issue happends here.
    for(int i = 0; i < num; i ++)
        tmp[i] = names[i].c_str(); // is it not inialization???
    //call another function using tmp
    delete []tmp; 
}

well in the line 3 of the code, i got an issue: Assigning: "tmp" = "new char const *[num]", which is allocated but not initialized.

i believe i got confused with the 2-d arrays allocations and initialization. I think that the tmp is the const char * array, and I just want to convert the vertor to const char **;

then in the code, is it right to use the new and delete here?

i know that if the 2d array is int*, then if I want to assign value to it, i need to new int[num], then do a for loop to new int[]; but how about my case here?

can someone help me with this piece of code?

Upvotes: 2

Views: 1710

Answers (1)

Christopher Bales
Christopher Bales

Reputation: 1071

Don't use const in this situation because you're allocating data post-initialization.

Upvotes: 1

Related Questions