8 Replies - 378 Views - Last Post: 09 February 2013 - 08:19 AM Rate Topic: -----

#1 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Strange error on "delete[] array" operation

Posted 08 February 2013 - 03:09 PM

What's up all? I'm getting a strange error during the delete[] A process. I know it is happening at that point because it is the only thing following an output before the return 0; statement. I'm seeing the output, but getting this error:

Quote

Unhandled exception at 0x76FBF8E9 (ntdll.dll) in a.exe: 0xC0000005: Access violation writing location 0x007A5A74


Mind you, this is happening during the delete[] A operation. It has to be since the statement immediately preceding it is executing and the statement immediately following it is return 0; to end the program.

cout << endl << "Total time: " << timer << " ms\n"; //this output occurs

delete[] A;

return 0;



Anyone have any ideas on this?

Is This A Good Question/Topic? 0
  • +

Replies To: Strange error on "delete[] array" operation

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,441
  • Joined: 25-December 09

Re: Strange error on "delete[] array" operation

Posted 08 February 2013 - 03:12 PM

You need to show more code, how did you allocate the memory for your variable?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: Strange error on "delete[] array" operation

Posted 08 February 2013 - 03:13 PM

Here is the entire main() function:

int main(int argc, char** argv){

	//make sure all arguments are present
	if(argc != 4){
		errorMessage(argv[0]);
		return 0;
	}

	//cast the arguments to integers
	int algorithmPointer = *argv[1] - '0';
	int problemPointer = *argv[2] - '0';
	int sequencePointer = *argv[3] - '0';
	
	//make sure all arguments are within the acceptable range
	if(algorithmPointer < 1 || algorithmPointer > 4 || problemPointer < 1 || problemPointer > 4 || sequencePointer < 1 || sequencePointer > 4){
		errorMessage(argv[0]);
		return 0;
	}
		
	char algorithmChooser[][5] = {"null", "qsf", "qsl", "qsm", "qsr"};
	char* algorithmTitle = algorithmChooser[algorithmPointer];
	
	int nChooser[5] = {0, 64000, 256000, 1024000, 8192000};
	int n = nChooser[problemPointer];	
	
	char sequenceChooser[][20] = {"null", "random", "all keys equivalent", "decreasing sequence", "increasing sequence"};
	char* sequenceTitle = sequenceChooser[sequencePointer];
	
	cout << "\nChosen algorithm is: " << algorithmTitle << endl;
	cout << "Chosen problem size (n) is: " << n << endl;
	cout << "Chosen sequence is: " << sequenceTitle << endl;	
	
	//setup the key list array
	double *A = new double[n];
	setupKeys(A, sequencePointer, n);
	
	//displayArray(A, n);
	//cout << endl;
	
	//declare variables to hold timer values
	clock_t timer;
	
	//call the algorithm according to the command line argument
	switch(algorithmPointer){
		
		case 1:	timer = getTime();
				performJumble(A, n);
				qsf(A, 0, n);
				timer = getTime() - timer;
				break;
		
		case 2: timer = getTime();
				performJumble(A, n);
				qsl(A, 0, n);
				timer = getTime() - timer;
				break;
				
		case 3: timer = getTime();
				performJumble(A, n);
				qsm(A, 0, n);
				timer = getTime() - timer;
				break;
				
		case 4: timer = getTime();
				performJumble(A, n);
				qsr(A, 0, n);
				timer = getTime() - timer;
				break;
	
	}
	
	displayArray(A, n);
	cout << endl;
	
	cout << endl << "Total time: " << timer << " ms\n";

	delete[] A;
	
	return 0;
}


This post has been edited by axnjxn: 09 February 2013 - 08:01 AM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: Strange error on "delete[] array" operation

Posted 08 February 2013 - 04:27 PM

Between lines 34 and 35 do:
cout << A << endl;


and right before line 77 do:
cout << A << endl;



If the value of A changes between the time you allocated it on line 34, and the time you are ready to delete it on line 77, you have a memory corruption bug. Perhaps a stack overrun?

This post has been edited by Skydiver: 08 February 2013 - 04:27 PM

Was This Post Helpful? 0
  • +
  • -

#5 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: Strange error on "delete[] array" operation

Posted 09 February 2013 - 07:35 AM

Thanks for your reply. I've done what you asked and I seem to be getting the same addresses at both locations. However, the program still seems to hang right after printing the address the second time. Also, this seems to be happening only at inputs of size 64000. The larger inputs don't have a problem (inputs of 256000, 1024000, 8192000). For some reason, I'm no longer getting the crash & error message. It's just been hanging at the last address printout.

Any ideas? This is a strange one.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: Strange error on "delete[] array" operation

Posted 09 February 2013 - 07:50 AM

Are you sure you posted your current code above? Your list of input sizes above doesn't include 64000 as a choice.
Was This Post Helpful? 0
  • +
  • -

#7 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: Strange error on "delete[] array" operation

Posted 09 February 2013 - 08:01 AM

Sorry, I forgot to change the 100 back to 64000 (I was testing the actual sorting of the array and didn't want to check 64000 numbers). I will correct the code above to reflect this.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: Strange error on "delete[] array" operation

Posted 09 February 2013 - 08:14 AM

Well at this point, if you were running on a Linux system, I would recommend running your program through valgrind to try to see if there is some kind of memory corruption happening.

I don't think there is any free equivalent of valgrind for Windows. If you are using the Microsoft compiler and headers, I think that you can use a macro and a header file to tell the compiler and linker to enable debugging the memory allocator and be more paranoid about memory overwrites, but I don't recall what that macro and header file are right now. Something like MEM_CRT?
Was This Post Helpful? 0
  • +
  • -

#9 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: Strange error on "delete[] array" operation

Posted 09 February 2013 - 08:19 AM

I'm using MinGW compiler. I'm really not all that experienced with using macros, either. So you think it could be some kind of memory corruption? What would be a possible fix for that sort of thing? I'm not even very sure what could cause that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1