Archived

This topic is now archived and is closed to further replies.

Very strange - have to call cout

This topic is 5538 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When I´m trying to print the results on the screen after calling SplitPathList, I have to make a call to cout either before calling the function or before exiting the function. If I don´t, then I get some crap to output of lpFirstPath. Why? Here´s the code:
  
void SplitPathList( LPSTR* lpPathList, LPSTR* lpFirstPath )
{
	char	cFirstPath[MAX_PATH];
	char	cPathList[MAX_PATH];
	char	cSource[MAX_PATH];
	int		iCurPos = 0;

	memset( cFirstPath, 0, sizeof(&cFirstPath) );
	memset( cSource, 0, sizeof(&cSource) );

	memcpy( cPathList, *lpPathList, strlen(*lpPathList) + 1 );

	while( iCurPos < strlen(*lpPathList) )
	{
		if( cPathList[iCurPos] == '','' )
		{
			if( cPathList[iCurPos + 1] == '' '' )
			{
				iCurPos += 2;
			}
			else
			{
				iCurPos++;
			}

			for( int i = iCurPos; i<sizeof(cSource) - iCurPos; i++ )
			{
				cSource[i - iCurPos] = cPathList[i];
			}
			break;
		}

		cFirstPath[iCurPos] = cPathList[iCurPos];

		iCurPos++;
	}

	*lpFirstPath = cFirstPath;
	*lpPathList = cSource;
}

void main()
{
	LPSTR lpFirstPath;
	LPSTR lpPathList; // lpCmdLine


	lpPathList = "Blablabla, ABC, Bla";

	SplitPathList( &lpPathList, &lpFirstPath );

	cout << "lpFirstPath: " << lpFirstPath << endl;
	cout << "lpPathList (rest): " << lpPathList << endl;
}
  

Share this post


Link to post
Share on other sites
I´ve now stated that somethings wrong with cFirstPath, it works if I transfer that array to another array in the SplitPathList function. What is wrong with cFirstPath?

Share this post


Link to post
Share on other sites
You wrote:

cout << "lpFirstPath: " << lpFirstPath << endl;
cout << "lpPathList (rest): " << lpPathList << endl

the garbage that u see may be because u are printing the value of the pointer lpPathList, in the cout. Try writing this

cout << "lpFirstPath: " << *lpFirstPath << endl;
cout << "lpPathList (rest): " << *lpPathList << endl

so it can print the value at lpPathList.


Hope it helps

Dark Star
UK

Share this post


Link to post
Share on other sites
You are returning pointers to local variables cFirstPath and cSource. They are not valid pointers after the function has exited! The memory areas pointed to may still contain what they''re supposed to, but the memory itself has been freed and the result of this function is really undetermined.

Share this post


Link to post
Share on other sites