Yet another CString to char* problem
Author |
Message |
Morten Wennevi #1 / 6
|
 Yet another CString to char* problem
I noticed this topic was up not too many days ago, but my news server's turnaround isn't long enough so it's gone. Anyway, I'm making a treectrl by adding strings from a stringlist. The strings are CStrings, and I'm porting them to a char* array. However, each previous addition to that array gets changed to reflect the new addition. char* testchar[10]; int testint = 0; char test[10]; for( POSITION pos = pDoc->MPList.GetHeadPosition(); pos != 0;) { CString teststring = ((Monster*)pDoc->MPList.GetNext(pos))->monsterName const char* test2 = (LPCTSTR)teststring; strcpy(test, test2); testchar[testint] = test; testint++; Quote: }
Basically the array will be like this ... name1, zxc, zxc, zxc, zxc, zxc, zxc name2, name2, zxc, zxc, zxc, zxc, zxc name3, name3, name3, zxc, zcx,zcx, zxc and so forth (zxc is just whatever is in that part of memory) I'm probably doing a fairly obvious mistake, but with my limited knowledge I can't find out what.
|
Thu, 18 Dec 2003 18:20:17 GMT |
|
 |
Pete Barret #2 / 6
|
 Yet another CString to char* problem
Quote: >I noticed this topic was up not too many days ago, but my news server's >turnaround isn't long enough so it's gone. >Anyway, I'm making a treectrl by adding strings from a stringlist. >The strings are CStrings, and I'm porting them to a char* array. >However, each previous addition to that array gets changed to reflect the >new addition. >char* testchar[10]; >int testint = 0; >char test[10]; >for( POSITION pos = pDoc->MPList.GetHeadPosition(); pos != 0;) >{ > CString teststring = ((Monster*)pDoc->MPList.GetNext(pos))->monsterName > const char* test2 = (LPCTSTR)teststring; > strcpy(test, test2); > testchar[testint] = test;
test is the same every time you go through the loop! Quote: > testint++; >} >Basically the array will be like this ... >name1, zxc, zxc, zxc, zxc, zxc, zxc >name2, name2, zxc, zxc, zxc, zxc, zxc >name3, name3, name3, zxc, zcx,zcx, zxc >and so forth (zxc is just whatever is in that part of memory) >I'm probably doing a fairly obvious mistake, but with my limited knowledge >I can't find out what.
Try: char* testchar[10][10]; int testint = 0; char test[10]; for( POSITION pos = pDoc->MPList.GetHeadPosition(); pos != 0;) { CString teststring = ((Monster*)pDoc->MPList.GetNext(pos))->monsterName const char* test2 = (LPCTSTR)teststring; strcpy(&testchar[testint][0], test2); testint++; Quote: }
Pete Barrett
|
Thu, 18 Dec 2003 19:53:16 GMT |
|
 |
Jan Raddat #3 / 6
|
 Yet another CString to char* problem
Well easy problem :) you copy your string always in the same buffer (test). strcpy(test, test2); now you put the address of your buffer (test) into the testchar array so you have the address of test max ten times in your array. testchar[testint] = test; Everytime you use strcpy you should use another destination buffer. This should work: ============ char* test; for( POSITION pos = pDoc->MPList.GetHeadPosition(); pos != 0;) { CString teststring = ((Monster*)pDoc->MPList.GetNext(pos))->monsterName const char* test2 = (LPCTSTR)teststring; test = new char [10]; strcpy(test, (LPCTSTR) teststring ); testchar[testint] = test; testint++; Quote: }
Sincerely Jan
Quote: > I noticed this topic was up not too many days ago, but my news server's > turnaround isn't long enough so it's gone. > Anyway, I'm making a treectrl by adding strings from a stringlist. > The strings are CStrings, and I'm porting them to a char* array. > However, each previous addition to that array gets changed to reflect the > new addition. > char* testchar[10]; > int testint = 0; > char test[10]; > for( POSITION pos = pDoc->MPList.GetHeadPosition(); pos != 0;) > { > CString teststring = ((Monster*)pDoc->MPList.GetNext(pos))->monsterName > const char* test2 = (LPCTSTR)teststring; > strcpy(test, test2); > testchar[testint] = test; > testint++; > } > Basically the array will be like this ... > name1, zxc, zxc, zxc, zxc, zxc, zxc > name2, name2, zxc, zxc, zxc, zxc, zxc > name3, name3, name3, zxc, zcx,zcx, zxc > and so forth (zxc is just whatever is in that part of memory) > I'm probably doing a fairly obvious mistake, but with my limited knowledge > I can't find out what.
|
Thu, 18 Dec 2003 19:58:43 GMT |
|
 |
Morten Wennevi #4 / 6
|
 Yet another CString to char* problem
: This should work: : ============ : char* test; : for( POSITION pos = pDoc->MPList.GetHeadPosition(); pos != 0;) : { : CString teststring = ((Monster*)pDoc->MPList.GetNext(pos))->monsterName : const char* test2 = (LPCTSTR)teststring; : test = new char [10]; : strcpy(test, (LPCTSTR) teststring ); : testchar[testint] = test; : testint++; : } since test now is a NEW char, should I delete it somewhere? or does the loop take care of it?
|
Thu, 18 Dec 2003 22:04:03 GMT |
|
 |
Nish #5 / 6
|
 Yet another CString to char* problem
Quote: > since test now is a NEW char, should I delete it somewhere? or does the > loop take care of it?
no delete it after the loop like :- delete test;
|
Fri, 19 Dec 2003 12:19:04 GMT |
|
 |
Jan Raddat #6 / 6
|
 Yet another CString to char* problem
Loop through your array after you don't need it any more and delete the strings. Otherwise you get a memory leak :) Bye Jan
Quote: > : This should work: > : ============ > : char* test; > : for( POSITION pos = pDoc->MPList.GetHeadPosition(); pos != 0;) > : { > : CString teststring = ((Monster*)pDoc->MPList.GetNext(pos))->monsterName > : const char* test2 = (LPCTSTR)teststring; > : test = new char [10]; > : strcpy(test, (LPCTSTR) teststring ); > : testchar[testint] = test; > : testint++; > : } > since test now is a NEW char, should I delete it somewhere? or does the > loop take care of it?
|
Fri, 19 Dec 2003 16:24:44 GMT |
|
|
|