Copying string in Visual Studio

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:

1
2
3
4
5
data->GetName(dispinfo->item.pszText);
char * pointme;
strcpy(pointme,dispinfo->item.pszText);
strcat(pointme,"\0");
pointme+=6;


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?
Last edited on
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.
ok and how to initiate the pointer? Some test:
1
2
char test[500000];
char * ptest;

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?
I am trying to copy some variable into string to monitor what happened. But it does not work as i expected.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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"
Last edited on
is pszText is really an integer? Is hItem really a char pointer and does it ponts to valid allocated memory?
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?
Char array converted to pointer when passed to functions s it is irrelevant.
I wonder how to do it to get something like:
Append in loop? Currently you are only writing string once and seeing it once, all checks.
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.
Last edited on
This is the function which I am fighting with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
Are you sure that optimisations are turned off and generation of debug information is enabled?
Ah, great idea! I have been running the "Release" not "Debug" configuration.

Thanks. Now I see the variables.

Unfortunately the debug version crashes when I try to copy item in the list so I cannot test the bug.
Last edited on
I have idea how to simulate the paste action without the need to display the tree item (so I can prevent from repetitively breaking the debug).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void TrigTree_HandleGetDispInfo(NMTVDISPINFO *dispinfo, HWND dialog)
{
	class ItemData *data = (class ItemData*)dispinfo->item.lParam;
	char test[500000];
	char * ptest = test;
	char * pctest = ptest;
	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;
				
				HWND treeview = GetDlgItem(dialog, IDC_T_TREE);	//all use this
				if (GetFocus() == treeview)
					TrigTree_Paste(dialog);

			}

			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);
}


But I need to add condition to recognise when I hit ctrl+v hotkeys. Can you tell me how to get the last pressed hotkeys?

Edit:
I found it.
Last edited on
So I simulated the paste action and got here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
		trig_data = static_cast<char*>(GlobalLock(clip_data));
		if (trig_data)
		{
			try
			{
				Trigger t(MemBuffer(trig_data, clip_size));
				index = scen.insert_trigger(&t, index_sel);
				GlobalUnlock(clip_data);

				pasted = TrigTree_AddTrig(treeview, index, selected);

				TreeView_SelectItem(treeview, pasted);
			}
			catch (std::exception &ex)
			{
				printf("Paste: %s\n", ex.what());
				MessageBox(dialog, "Error placing trigger.", szTrigTitle, MB_ICONWARNING);
			}
		}


it crashes on scen.insert_trigger()
Last edited on
Topic archived. No new replies allowed.