When I started to learn C/C++ I started in Code::Blocks IDE. I was used to possibility to configure item in Watch panel so that I can see null character at the string (buffer). Now I started to use Visual Studio C++ 2010 (Windows XP) and I tried this code:
GetName is some function which performs strcpy(char * buffer, char * name).
Well, I wanted to see if there is null pointer in the dispinfo->item.pszText and pointme but no one is displayed in the Watch panel after I added the variable. I also added pointme and *pointme but pointme shows "R" instead \0. Cam you explain why, and how to correct and how to display the null character in the watch panel in VS?
Lets start with problems here:
1) pointme is uninitialized, so using it in lines 3 and 4 invokes undefined behavior.
2) Line 4 is redundand as it is not doing anything: it encounters null character as first character of second argument and terminates instantly. Additionally strcpy already adds terminating null to string.
Make it actually point to something. For example to your test buffer:
1 2
char test[500000];
char * ptest = test;
Some questions you should ask whenever you see the pointer:
1) Where it is pointing to?
2) When memory area it points to was allocated, who is owning it and when it is going to be deallocated?
3) What is the size of memory I can use through this pointer?
char test[500000];
char * ptest = test;
if (dispinfo->item.mask == TVIF_TEXT)
{
try
{
data->GetName(dispinfo->item.pszText);
if (strcmp(dispinfo->item.pszText," dea BLACK")==0)
{
sprintf(ptest, "new:%d,%s",dispinfo->item.hItem,dispinfo->item.pszText);
ptest+=18;
}
if (setts.intense)
printf("GetDispInfo returned name: %s\n", dispinfo->item.pszText);
}
catch (std::exception& ex) // don't let any bubble up
{
strcpy(dispinfo->item.pszText, ex.what());
}
}
In the watch panel I see:
test 0x000b51b8 "new:1454832, dea BLACK" char [500000]
I wonder how to do it to get something like:
test 0x000b51b8 "new:1454832, dea BLACKnew:1454832, dea BLACKnew:1454832, dea BLACKnew:1454832, dea BLACKnew:1454832, dea BLACK"
Am I using the sprinf wrong when I need to append into the string? I see that in the example here: http://www.cplusplus.com/reference/cstdio/sprintf/ they R using not pointer but char array. But how to do it to add to string not to overwritten data which I added to it?
It is in a function which is called when item (node in a Tree) is displayed. There is condition to run it only when the particular item is displayed. At this moment when i am writing I suddenly dont see any data filled in the disinfo object... I am not quite sure what there is going on. I could share my project but it is in Visual Studio 2010.
Here I have uploaded the project: https://sourceforge.net/projects/autots/files/AOKTS%20update/aokts-1.0.1%20test.zip/download
project file to open:
aokts.sln
in file view/trigedit.cpp line #1193.
f7 to compile, f5 to debug, open file from menu: test_scen.scx and then the item at bottom, I try to copy/paste it. Now I realized that the condition cannot work for the pasted item because it's name contains some strange characters on the end. This is the bug which I try to solve.
void TrigTree_HandleGetDispInfo(NMTVDISPINFO *dispinfo)
{
class ItemData *data = (class ItemData*)dispinfo->item.lParam;
char test[500000];
char * ptest = test;
char * pctest = pctest;
int countme = 0;
if (dispinfo->item.mask == TVIF_TEXT)
{
countme++;
try
{
data->GetName(dispinfo->item.pszText);
if (strcmp(dispinfo->item.pszText," dea BLACK")>=0)
{
sprintf(ptest, "new:%d,%s",dispinfo->item.hItem,dispinfo->item.pszText);
ptest+=18;
}
if (setts.intense)
printf("GetDispInfo returned name: %s\n", dispinfo->item.pszText);
}
catch (std::exception& ex) // don't let any bubble up
{
strcpy(dispinfo->item.pszText, ex.what());
}
}
else
assert(false);
}
When I break on the sprintf command and I put mouse cursor on countme variable it should show the number. But it does not. It does not acceess the memebr dispinfo->item.pszText and the other members in item. Why? I cannot get it. If I put countme to Watch panel it should display some number but I have message:
countme CXX0017: Error: symbol "countme" not found
Anybody knows how to access the variables and why they are not accessed? The function is called when message TVN_GETDISPINFO is passed through function INT_PTR CALLBACK TrigDlgProc