Jump to content
  • Advertisement

Archived

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

GeekPlusPlus

Char String/Memory problems

This topic is 5227 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

Hey guys, i''m trying to make a very simple console program and i''m stuck on, of all things, logging... I have a CLog class which makes txt file logs through streaming. For example... CLog Log; Log<<"This number is important: "<< iVar< for each line, so I can push and pop them in and out depending on the maximum lines to display. OK, onto the problem... I get access vilations when I try crafting my string.. I''ll walk you through it... First off in the main() I''ll write out a "to-be-logged" line like this (I''ll just use characters for now as ints get harder): Log<<"This is my string"<
CLog &CLog::operator<< (char* szString)
{
	if(m_bWriteFile)
	{
		if(!m_bActive)
		{
			m_bActive = true;
			TimeStamp();
		}

		m_ofStream << szString;
	}
	
	if(m_bWriteWindow)
	{
		m_pBuffer->AddToBuffer(szString);
	}

	return (*this);
}
so if WriteFile is toggled on everything is written to the txt just fine. then if WriteWindow is toggled on it runs AddToBuffer() from the buffer pointer... which looks like this...
void CBuffer::AddToBuffer(char* szString)
{
	strcat(m_vBufferLines[m_iBufferIndex], szString);
}
So this function just takes that string and just copies it onto the end of whatever vector string position we are at, remeber m_vBufferLines is a std::vector. But I get an access violation on that line inside AddToBuffer(); At first I thought it was my lack of initilization, but in my CBuffer constructor I have...
CBuffer::CBuffer()
{
	m_vBufferLines.push_back("");
	m_iBufferIndex = 0;
	m_iMaxLines = 5;
}
so m_vBufferLines[0] = "" to start off... So I think i''m just missing some sort of memory theory or something that''s keeping this from working.... Hope someone can help. - Newb Programmer: Geek++

Share this post


Link to post
Share on other sites
Advertisement
When I use the & it returns the error:

''strcat'' : cannot convert parameter 1 from ''char ** '' to ''char *''

since it''s an array of type then each iterator is a pointer to that charpointer...

and strcat takes char*...

- Newb Programmer: Geek++

Share this post


Link to post
Share on other sites
I think you should overload the operator << for all types. If you wish you be able to MyLog << MyInt, you need to overload the operator << for ints. If you need to be able to use std::string, you need to overload operator << for std::string.

The same goes for other functions, you can''t just throw in variables, you need to overload the function to be able to take it.

And you should use const char * instead of char *. You don''t need to modify the szString, so you pass it as a const.

Toolmaker



-Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites
I have the other types in CLog, i'm just using the szString as an example because it's already a char*.

My problem is I need to end up with a vector list of character arrays.

I want to be able to do this sort of thing....



std::vector<char*> List;
List.push_back("Line1: ");

strcat(List[0], "Added");
strcat(List[0], " More Added");
strcat(List[0], "\r\n"); // end the line


List.push_back("Line2: ");

strcat(List[1], "Added");
strcat(List[1], " More Added2");
strcat(List[1], "\r\n"); // end the line




I'm about ready to give up and use CString... There's something to be said about sString += sOtherString;

- Newb Programmer: Geek++


[edited by - GeekPlusPlus on March 1, 2004 3:08:58 AM]

Share this post


Link to post
Share on other sites
Why not use
std::vector< std::string > myBuffer;
to represent your in memory file.

Then when you need to append stuff to your file you do something like this

myBuffer[ 1 ] += "My added text!"

Much easier in IMO.

There are tons of little helper functions for the std::string class and it isn''t that hard to derive from.

Share this post


Link to post
Share on other sites
I''ve started trying it with std::string, and it seems to work... except I get these ugly warnings when it runs... any ideas on these?

c:\program files\microsoft visual studio\vc98\include\vector(48) : warning C4786: ''??0?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QAE@IABV
?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@1@ABV?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@1@@Z'' : identifier was truncated to ''255'' characters in the browser information
c:\documents and settings\matthew\my documents\programming\kahsmud\buffer.h(17) : see reference to class template instantiation ''std::vector,class std::allocator >,class std::a
llocator,class std::allocator > > >'' being compiled

c:\[path]\vector(61) : warning C4786: ''??0?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QAE@PBV?
$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@1@0ABV?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@1@@Z'' : identifier was truncated to ''255'' characters in the browser information
c:\[path]\buffer.h(17) : see reference to class template instantiation ''std::vector,class std::allocator >,class std::a
llocator,class std::allocator > > >'' being compiled

they point to this line:

std::vector m_vBufferLines;



- Newb Programmer: Geek++

Share this post


Link to post
Share on other sites
As they tell, the debugging information for those lines is truncated to 255 characters as the debugger cannot handle longer lines. Insert

#pragma warning (disable: 4786)

before your #include lines and those warnings will disappear.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!