views:

138

answers:

3

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 corrupted. I thought at first i was overwriting but totalLines is increase. Hopefully this is enough information.

+4  A: 

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?

Eli Bendersky
The compiler probably was complaining, and the cast was probably a misguided attempt to shut it up. Alas.
jamesdlin
What I want to be able to hold lines of text. Such aslines[0]="Hello"lines[1]="Welcome"lines[2]="Test"and such. What i'm trying to do is create a simple chat program, and i'm wanting to hold the text in a array like that so i can simply code a scroll bar. Sorry, I should of mentioned that in the question.
BigBirdy
Alas- Your right, but i'm not trying to even use SetWindowText like that. I was just doing that to help explain the question.
BigBirdy
Eli is right, you only need a LPWSTR*, not a pointer-to-pointer-to-LPWSTR.
Ben Voigt
What, i could of swear i tryed that first. Grrr, Yea, it works fine now. Thanks Alot guys.
BigBirdy
A: 

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 LPWSTRs.

Billy ONeal
LPWSTR** lines= new LPWSTR*[totalLines]; has been changed to LPWSTR** lines= new LPWSTR*[size]; Sorry, i changed that before i posted this question but forgot to update the question.
BigBirdy
@BigBirdy: No problem :) I hope you find an acceptable solution.
Billy ONeal
You could just use `std::wstring` so you don't need to specify the template parameters to `basic_string`.
GMan
@GMan: Yep. I was just lazy :)
Billy ONeal
A: 

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.

BigBirdy
Why aren't you using `std::vector`? :( Bunnies are sad.
GMan