An array of LPWSTR pointers, not working right
Declare:
LPWSTR** lines= new LPWSTR*[totalLines];
then i set using:
lines[totalLines]=&totalText;
SetWindowText(totalChat,(LPWSTR)lines[totalLines]);
totalLines++;
Now I know totalText is right, cause if i SetWindowText using totalText it works fine. I need the text in totalLines too.
I'm also doing:
//accolating more memory.
int orgSize=size;
LPWSTR** tempArray;
if (totalLines == size) {
size *= 2;
tempArray = new LPWSTR*[size];
memcpy(tempArray, lines,sizeof(LPWSTR)*orgSize);
delete [] lines;
lines = tempArray;
}
to allocate more memory when needed.
My problem is that the lines is not getting the right data. It works for the first time around then it get cor开发者_运维知识库rupted. I thought at first i was overwriting but totalLines is increase. Hopefully this is enough information.
LPWSTR
is already a pointer, so you're creating a 2D array of pointers - is that what you wanted? I think not, because this:
SetWindowText(totalChat,(LPWSTR)lines[totalLines]);
Casts LPWSTR*
to LPWSTR
. Isn't your compiler complaining?
These two statements:
LPWSTR** lines= new LPWSTR*[totalLines];
lines[totalLines]=&totalText;
invoke undefined behavior. The problem is that the maximum index of an array totalLines
long is totalLines-1
.
If you'd post what exactly you were trying to accomplish we might be able to help better. For example, it seems this problem could be much better solved with a std::vector<std::vector<wchar_t> >
or std::vector<std::basic_string<wchar_t> >
rather than an explicitly allocated array of LPWSTR
s.
Thanks to Ben and Eli I have my answer. It should be LPWSTR* lines= new LPWSTR[size]; since LPWSTR is already a pointer. Thanks guys.
精彩评论