pointer to pointer

i want to retrive a list of month names through pointer to pointer..
But whenever i free my allocated memory by free() it throws error. i think the reason behind that, my code make some changes in heap manager while executing..

my code is given below


#include<iostream>
#include<esent.h>
using namespace std;

bool GetMonthList(WCHAR **pwstrMonthList)
{
int i=0;
WCHAR *c[] = {L"Jan",L"Feb",L"Mar",L"Apr",L"May",L"Jun",L"Jul",L"Aug",L"Sep",L"Oct",L"Nov",L"Dec"};

*pwstrMonthList = (WCHAR *) malloc (12 * sizeof(WCHAR));

while(i<12)
{
pwstrMonthList[i] = (WCHAR*) malloc (80);
wcscpy(pwstrMonthList[i],c[i]);
cout<<pwstrMonthList[i]<<"\t";
wprintf(L"%s\n",pwstrMonthList[i]);
i++;
}

free(pwstrMonthList);
return true;
}

int main()
{
WCHAR *pwstrMonthList = NULL;
bool bReturn = GetMonthList(&pwstrMonthList);

if(bReturn)
{
if(pwstrMonthList == NULL)
{
cout<<"\n Main Function \n\n";
}
}
}
You have quite a few problems/oddities:

1) Why does GetMonthList take the pwstrMonthList as a parameter? If it mallocs and frees it all in the function, taking it as a parameter is pointless. It might as well be local to the function.

2) Why are you using malloc/free anyway? new/delete should be preferred in C++.

3) You have some type issues. Remember that both * and [] operators dereference the pointer. Therefore *pwstrMonthList and pwstrMonthList[0] are equivilent and interchangable.

4) You are attempting to free pwstrMonthList, but pwstrMonthList was not allocated with malloc. This is why your program is crashing.

5) You are also (possibly) stepping outside the allocated bounds in your while loop. WCHAR* is likely bigger than WCHAR.



If you want to create a 2D array to hold these strings, you need to:
- allocate an array of pointers.
- for each of those pointers, allocate an array of WCHARs
- To avoid memory leaks, you'll also need to destroy everything you've allocated

it'll look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
WCHAR** p;  // p is a pointer to a pointer
p = new WCHAR*[12];  // allocate 12 pointers
for(int i = 0; i < 12; ++i)
{
  p[i] = new WCHAR[80];  // for each pointer, allocate 80 WCHARs
}

// .. then, to clean up

for(int i = 0; i < 12; ++i)
{
  delete[] p[i];  // delete each set of 80 allocated WCHARs
}
delete p;  // delete the allocated pointers 



Of course... all of this is much, much simpler and safer if you just use C++ container classes:

1
2
3
vector< wstring > myarray(12);  // make a resizable array of strings with an initial size of 12

// all done.  Don't have to worry about clean up or destroying anything 
thanks Disch,
for this valuable reply..
But i want to this program using malloc to clear my concepts..
once again, thank for ur reply..
Ur code works fine...
But i want to this program using malloc to clear my concepts..


The concept behind new and malloc are the same.

The only real difference between the two is that malloc doesn't call constructors, which can be dangerous in C++.
 
free(pwstrMonthList);
should be:
1
2
free(*pwstrMonthList);
*pwstrMonthList = 0;
@kbw: That would solve one of the problems, yes.

But he's still corrupting the heap and leaking memory aside from that
Quite right, there are other problems.
Topic archived. No new replies allowed.