Jump to content

Confusion With Compiler Error

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
MichaelNQ

MichaelNQ

    Newbie

  • Members
  • PipPip
  • 11 posts
I have a list of user entered integers. I created a recursive function to search for a target. I designed this a certain way so I don't need implementation advice. When I compile, I get the following message

Quote


a.out(12744) malloc: *** error for object 0x7fff5fbff9a0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

I don't really understand the error message. But in my code the error pertains to lines 38 and 39. Can someone take a seconds and give me some advice?


#include <iostream>

#include <list>


using namespace std;


	


int maxList(list<int> array, int x);


int main() {

	

	int tempInt;

	char tempChar;

	list<int> L;

	

	cout << "\nEnter an array of ints, and end it with a # sign" << endl;


	cin >> tempChar;

	

	while(tempChar != '#') {

		tempInt = atoi(&tempChar);

		L.push_back(tempInt);

		cin >> tempChar;

	}

	

	maxList(L, 2);

	

	return 0;	

}


int maxList(list<int> array, int x) {

	

	if(*array.begin() == x) {

		cout << "Found" << endl;

		return 0;

	}

	if(*array.begin() != x) {

		array.pop_front();

		maxList(array, x);

	}

	if(array.begin() == array.end()) {

		cout << "Doesn't exist";

		return 1;

	}

}



#2
asafe

asafe

    Programmer

  • Members
  • PipPipPipPip
  • 107 posts
I added #include<cstdlib> and compiled with no problem. But prefer sstream instead of atoi and its friends.

#3
MichaelNQ

MichaelNQ

    Newbie

  • Members
  • PipPip
  • 11 posts
Sorry, I should of specified that it is a run-time error. When the list.pop_front() function is invoked, it all goes to hell.

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Well, first I'd have the array.begin() == array.end() check first in the maxList function, not last, since nothing will inevitably not equal x, and so that if statement will be true and try to pop_front() the array with no elements. That could very easily lead to disaster. Second, you're using atoi wrong, you need to have a null terminating character after the first character in atoi (I had this problem before). Finally, it's expensive to use pass by value in the function, I'd use pass by reference.

Anyway, I gave it a shot and I'm not finding any build errors. The only warning even given is that maxList returns an int which was not received by anything (Control reaches end of non-void function). You will need to modify maxList to have the If(array.begin() == array.end()) in front if you want to avoid freeing memory that doesn't exist runtime error. Like so:
int maxList(list<int>& array, int x) {

	if(array.begin() == array.end()) {
		cout << "Doesn't exist";
		return 1;
	}
	if(*array.begin() == x) {
		cout << "Found" << endl;
		return 0;
	}
	else {
		array.pop_front();
		maxList(array, x);
	}
}

Wow I changed my sig!

#5
MichaelNQ

MichaelNQ

    Newbie

  • Members
  • PipPip
  • 11 posts
I appreciate the help. I did pass by val instead of ref for a reason, although it isn't obvious to anyone other than myself. But I do agree that passing by ref is more logical given most circumstances. Anyway, thanks for the advice guys, I'll be working on this a bit more.

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Why are you referencing array? *array.begin() just doesn't look right.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
array.begin() returns an iterator to the first function, and the * is dereferencing that iterator to the first field. I'll admit, I'd use array.front(), but the OP's working it however they need to!
Wow I changed my sig!

#8
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
What is the latest state of the code, and what kind of input produces the error (a list with a 2 or without)?

Taking some of the suggestions, such as,

ZekeDragon said:

Well, first I'd have the array.begin() == array.end() check first in the maxList function, not last, since nothing will inevitably not equal x, and so that if statement will be true and try to pop_front() the array with no elements. That could very easily lead to disaster.
...I don't see the issue.
#include <iostream>
#include <list>
using namespace std;

int maxList(list<int> &array, int x)
{

   if ( array.begin() == array.end() )
   {
      cout << "Doesn't exist";
      return 1;
   }
   if ( *array.begin() == x )
   {
      cout << "Found" << endl;
      return 0;
   }
   if ( *array.begin() != x )
   {
      array.pop_front();
      maxList(array, x);
   }
}

int main() 
{
   int one[] = {1,2,3,4,5};
   int two[] = {1,3,5,7,9};
   list<int> list1(one, one + sizeof one / sizeof *one);
   list<int> list2(two, two + sizeof two / sizeof *two);
   cout << "list1:\n";
   maxList(list1, 2);
   cout << "list2:\n";
   maxList(list2, 2);
   return 0;   
}

/* my output
list1:
Found
list2:
Doesn't exist
*/