Access violation (and other) problems.

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2370 Views - Last Post: 07 March 2011 - 08:20 PM Rate Topic: -----

#1 ChrisYuki   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 04-March 11

Access violation (and other) problems.

Posted 07 March 2011 - 08:26 AM

Im gonna make this short and sweet. Its 7 am in the morning, i've had no sleep and im on pain meds, so im a bit out of it. I really cant find this error due to this, so i need a little help.

Im getting access violation errors, and a little beef about buffer size from one of the *_s functions (strcpy_s or strcat_s).


class STR
{
private:

	int len;

public:

	char* string;

	STR(char* text)
	{
		this->Assign(text);
	}

	~STR()
	{
		this->Delete();
	}

	STR& Assign(char* text)
	{
		this->Delete();

		int size = strlen(text);

		if(size)
		{
			string = new char[size+1];
			strcpy_s(string, size, text);

			len = size;
		}
		return *this;
	}

	STR& Delete()
	{
		if(string)
		{
			len = 0;
			delete[] string;
		}
		return *this;
	}

	STR& Resize(int length)
	{
		char* tmp;

		if(length)
		{
			tmp = new char[length+1];
			strcpy_s(tmp, length, string);

			this->Assign(tmp);

			return *this;
		}
		this->Delete();

		return *this;
	}

	STR& Append(char* text)
	{
		int size = strlen(text);

		if(size)
		{
			this->Resize((len+size)+1);
			strcat_s(string, size, text);
		}
		return *this;
	}
	
};




EDIT: All i did was use the constructor.

This post has been edited by ChrisYuki: 07 March 2011 - 08:29 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Access violation (and other) problems.

#2 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: Access violation (and other) problems.

Posted 07 March 2011 - 08:39 AM

Your constructor doesn't initialize string, so string may contain any garbage value when assign calls delete[] string;.

Try putting
string = NULL; as the first line of the constructor.

I noticed that your Resize function doesn't set a new value for len.
Was This Post Helpful? 2
  • +
  • -

#3 ChrisYuki   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 04-March 11

Re: Access violation (and other) problems.

Posted 07 March 2011 - 08:45 AM

View Postr.stiltskin, on 07 March 2011 - 08:39 AM, said:

Your constructor doesn't initialize string, so string may contain any garbage value when assign calls delete[] string;.

Try putting
string = NULL; as the first line of the constructor.

I noticed that your Resize function doesn't set a new value for len.


When i add that i no longer get a violation error, but i get a buffer is to small problem (big old message box).

also resize sets the value for len because it uses assign() :P.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Access violation (and other) problems.

Posted 07 March 2011 - 08:49 AM

Did you read the manual for strcpy_s???

Quote

The strcpy_s function copies the contents in the address of strSource, including the terminating null character, to the location specified by strDestination. The destination string must be large enough to hold the source string, including the terminating null character. The behavior of strcpy_s is undefined if the source and destination strings overlap.


EMPHASIS MINE.
Was This Post Helpful? 1
  • +
  • -

#5 ChrisYuki   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 04-March 11

Re: Access violation (and other) problems.

Posted 07 March 2011 - 08:53 AM

View PostJackOfAllTrades, on 07 March 2011 - 08:49 AM, said:

Did you read the manual for strcpy_s???

Quote

The strcpy_s function copies the contents in the address of strSource, including the terminating null character, to the location specified by strDestination. The destination string must be large enough to hold the source string, including the terminating null character. The behavior of strcpy_s is undefined if the source and destination strings overlap.


EMPHASIS MINE.


No i didnt read the manual for it. Also look through my code, i clearly have a +1 for the terminating '\0' character. Thats why im a bit stumped to be honest :(.

If you can point out what exactly is wrong i'd be very happy (im VERY out of it, sorry if im missing something stupid easy lol).
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: Access violation (and other) problems.

Posted 07 March 2011 - 09:06 AM

I rarely use Windows and never use any of those MS-specific functions, but glancing at the link that JackOfAllTrades posted tells me that the second argument to strcpy_s (size) is the size of the destination buffer, not the size of the source.
Was This Post Helpful? 1
  • +
  • -

#7 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Access violation (and other) problems.

Posted 07 March 2011 - 09:11 AM

Yeah, I misread it myself. Stupid Microsoft "security enhancements". :(
Was This Post Helpful? 0
  • +
  • -

#8 ChrisYuki   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 04-March 11

Re: Access violation (and other) problems.

Posted 07 March 2011 - 09:14 AM

View Postr.stiltskin, on 07 March 2011 - 09:06 AM, said:

I rarely use Windows and never use any of those MS-specific functions, but glancing at the link that JackOfAllTrades posted tells me that the second argument to strcpy_s (size) is the size of the destination buffer, not the size of the source.


You guys are very confusing (sorry, its probably the vicodin lol). That is exactly how im using it though. The size of the source and destination should be EXACTLY the same (with the exception of the +1 i added to the destination for the terminating null character). Afterall i re-size the destination buffer to fit the source (and the null character that follows).

Im really at a loss here :\.
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: Access violation (and other) problems.

Posted 07 March 2011 - 09:19 AM

The length of the source is size.
You made the length of the destination size + 1. Good.

So when you call the function, the call should be
strcpy_s( destination, size + 1, source );
Was This Post Helpful? 2
  • +
  • -

#10 ChrisYuki   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 04-March 11

Re: Access violation (and other) problems.

Posted 07 March 2011 - 09:31 AM

View Postr.stiltskin, on 07 March 2011 - 09:19 AM, said:

The length of the source is size.
You made the length of the destination size + 1. Good.

So when you call the function, the call should be
strcpy_s( destination, size + 1, source );



Hmmm that seems to have worked for the assigning part of it (using the constructor) ... the append member function still has the same problem :(.

Honestly its weird that you have to include +1 for the size, this is the first language i've programmed where thats the case :|.


class STR
{
private:

	int len;

public:

	char* string;

	STR(char* text)
	{
		string = NULL;

		this->Assign(text);
	}

	~STR()
	{
		this->Delete();
	}

	STR& Assign(char* text)
	{
		this->Delete();

		int size = strlen(text);

		if(size)
		{
			string = new char[size+1];
			strcpy_s(string, size+1, text);

			len = size;
		}
		return *this;
	}

	STR& Delete()
	{
		if(string)
		{
			len = 0;
			delete[] string;
		}
		return *this;
	}

	STR& Resize(int length)
	{
		char* tmp;

		if(length)
		{
			tmp = new char[length+1];
			strcpy_s(tmp, length+1, string);

			this->Assign(tmp);

			return *this;
		}
		this->Delete();

		return *this;
	}

	STR& Append(char* text)
	{
		int size = strlen(text);

		if(size)
		{
			this->Resize((len+size)+1);
			strcat_s(string, size+1, text);
		}
		return *this;
	}
	
};


This post has been edited by ChrisYuki: 07 March 2011 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

#11 ChrisYuki   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 04-March 11

Re: Access violation (and other) problems.

Posted 07 March 2011 - 09:46 AM

Solved. Thanks to everyone for helping me through this. I have theory down, just gotta get the language down :P.

Sorry for the nooby mistakes.
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Access violation (and other) problems.

Posted 07 March 2011 - 10:02 AM

Where you're writing in C++ you could instead use the std::string class and get around these sorts of issues.

C does not have a separate "string" type; instead strings are arrays of characters, and what makes these arrays "special" is the use of a null terminator, which allows you to use the standard library functions (such as strcpy, strcmp, etc). Without the null terminator those standard library functions would have no way of knowing where the data belonging to the string ends.
Was This Post Helpful? 2
  • +
  • -

#13 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: Access violation (and other) problems.

Posted 07 March 2011 - 10:07 AM

Why are you using those functions strcpy_s and strcat_s rather than the standard C library functions strcpy and strcat?

Are you aware that the result is that you end up with non-portable code? If your code includes their proprietary functions, it can only be compiled by a Microsoft compiler.
Was This Post Helpful? 1
  • +
  • -

#14 ChrisYuki   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 04-March 11

Re: Access violation (and other) problems.

Posted 07 March 2011 - 05:20 PM

View Postr.stiltskin, on 07 March 2011 - 10:07 AM, said:

Why are you using those functions strcpy_s and strcat_s rather than the standard C library functions strcpy and strcat?

Are you aware that the result is that you end up with non-portable code? If your code includes their proprietary functions, it can only be compiled by a Microsoft compiler.


Well honestly because i get tons of warnings when i use the standard versions. Also having a size param sounds like a really good idea, but its been causing all of these headaches lol!

Since this morning i've only used the standard functios, and the code is working fine, except for my append function. Im getting errors that "application wrote memory after end of heap buffer". Im a bit new to c++ so no idea what that means.

All my code makes sense in theory, its just this damn language that is keeping me down -.- lol.


Edit: also yes i know about the string class. Im making this just to do it :P. I like to see if i can recreate stuff, to see if i at least have a small idea whats going on in the code that i use.

class STR
{
private:

	int len;

public:

	char* string;

	STR(char* text)
	{
		string = NULL;

		this->Assign(text);
	}

	~STR()
	{
		this->Delete();
	}

	STR& Assign(char* text)
	{
		this->Delete();

		int size = strlen(text);

		if(size)
		{
			string = new char[size+1];
			strcpy(string, text);

			len = size;
		}
		return *this;
	}

	STR& Delete()
	{
		if(string)
		{
			len = 0;
			delete[] string;
		}
		return *this;
	}

	STR& Resize(int length)
	{
		char* tmp;

		if(length)
		{
			tmp = new char[length+1];
			strcpy(tmp, string);

			this->Assign(tmp);

			delete[] tmp;

			return *this;
		}
		this->Delete();

		return *this;
	}

	STR& Append(char* text)
	{
		int size = strlen(text);

		if(size)
		{
			this->Resize((len+size));
			strcat(string, text);
		}
		return *this;
	}
	
};



The error happens when you append text twice.

(like test.append(" lol").append(" k"); etc).

This post has been edited by ChrisYuki: 07 March 2011 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

#15 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: Access violation (and other) problems.

Posted 07 March 2011 - 07:19 PM

You can safely ignore the warning about "strcpy may be unsafe..." etc. as long as you are careful to make the buffer big enough -- which as you have seen you have to do even with MS's "safe" version.

I don't see why you're getting that memory error -- let's see the main function that produces it.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2