Hi,

I have a problem with a random number generating function. This function works great on windows XP but when I run the function in Windows VISTA and Windows 7, I receive an error message that displays:

"Attempted to read or write protected memory. This is often an indication that other memory has been corrupted"

(I dont know if this is because I mix mananged and unmanaged code. I did find the srand a good random function from the unmanaged code so this is why I have mixed it here)

int Start = 0 int Start = 0;
 int End = 50;
 int NumberResults = 100;
 std::vector<int> FillData;
 int Number5 = 0;


 std::string Dummy;
 System::String^ text;




 srand((unsigned)time(nullptr)); 
 for( int i = 0; i < NumberResults + 1; i++ )
 {


	 Number5 = rand() % (End - Start + 1) + Start;


		stringstream v1;
		std::string v2;					

		v1 << Number5;
		v2 = v1.str();

		String^ Number = gcnew String(v2.c_str());

		this->textBox1->Text = text;
		this->textBox1->Multiline = true;
		this->textBox1->WordWrap = false;


		text += Number + System::Environment::NewLine;
 }

Recommended Answers

All 3 Replies

Line 35, you add to text which is declared as a handle at line 9 but never gets an object assigned to it.

I think, with my very limited knowledge of .NET, you need to gcnew String an object to text.

Keep in mind that windows Vista and 7 have much more memory read/ write protection than XP did. Try right clicking on your executable file in Vista/ 7 and choosing 'Run as Administrator'.

Hello Jenifer88

Your code is full of mixed Managed and Unmanaged code:-/
If you are going to write code in the .NET environment it is best to stick to managed code where possible.
I've commented out the unmanaged code in you program and given you something to get going with.
Hope it is some help to you.
Cheers
Milton

private:
		void Randomize(void)
		{
			int Start = 0;
			int End = 50;
			int NumberResults = 100;
			//std::vector<int> FillData;
			int Number5 = 0;
			Random^ rand = gcnew Random(); // Class for generating random numbers
			//std::string Dummy;
			System::String^ text = ""; // Best to initialise the variable
			//srand((unsigned)time(nullptr)); 
			 for( int i = 0; i < NumberResults + 1; i++ )
			 {
				Number5 = rand->Next() % (End - Start + 1) + Start;

				//stringstream v1;
				//std::string v2;					
				//v1 << Number5;
				//v2 = v1.str();
				
				text += Number5.ToString() + System::Environment::NewLine;
				this->textBox1->Text = text;
				/*
				Better to set these properties in the 
				design mode properties for the TB control.
				You do not want to set them in very "for loop"
				it just slows things down.
				**************************************************
				this->textBox1->Multiline = true;
				this->textBox1->WordWrap = false;
				// A verticle scoll bar is handy here
				this->textBox1->ScrollBars = ScrollBars::Vertical
				**************************************************/
			 }
		}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.