12 Replies - 1589 Views - Last Post: 01 July 2010 - 08:36 PM Rate Topic: -----

#1 starving_mathematician  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

malloc error

Posted 01 July 2010 - 06:03 PM

I'm writing a program using dynamically allocated arrays for the first time, and I'm getting a malloc error (uh oh). This is it:

Assignment 3(17913) malloc: *** error for object 0x1001b0: double free *** set a breakpoint in malloc_error_break to debug

I can tell you that the "Assignment 3" at the beginning of the error is the name of the C++ project I'm working on, but aside from that I don't know what it means. It doesn't pop up as a normal error either, but shows up in the console along with my output. Can anyone tell me what this means, and how to make it go away? I can post the code if that would help.

This post has been edited by starving_mathematician: 01 July 2010 - 06:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: malloc error

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: malloc error

Posted 01 July 2010 - 06:15 PM

No code, no help.

EDIT: Clarifying this, you have a bug in your code. We can't help you with the bug without the code. Alternatively, you do what Oler1s says...fire up the debugger.
Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: malloc error

Posted 01 July 2010 - 06:18 PM

First result of a Google search. Also, error message: "set a breakpoint in malloc_error_break to debug". You're being given a hint as to how to find out more information...

No way to tell what the actual problem is without debugging and looking at code. But you obviously should fire up the debugger and gather more information to isolate the problem.
Was This Post Helpful? 1
  • +
  • -

#4 starving_mathematician  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

Re: malloc error

Posted 01 July 2010 - 06:28 PM

The debugger runs the whole way through, but prints the same message at the end (except that the number after "Assignment 3" changes each time). The error is clearly occurring when I increment the array size (to do this I have to destroy the array and make a new one). The code is in 3 pieces:

class ASCIICode
{
public:
	ASCIICode(int);
	ASCIICode();
	~ASCIICode();
	
	void print(int) const;
	void print() const;
	
	void save();
	int showSize();
	
	ASCIICode &operator++();
	ASCIICode operator++(int);
	
	static int showCount();
	static int count;
private:
	int size;
	char *arrayPtr;
	
	void initialize();
	void setRandomSize();
	void boundsCheck();
};



#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include "ASCIICode.h"
using namespace std;

int ASCIICode::count=0;

int ASCIICode::showCount()
{
	return count;
}

ASCIICode::ASCIICode(int length)
{
	count++;
	
	if(length>=1 && length<=255)
	{
		size=length;
		initialize();
	}
	else
	{
		cout << "\nYour input was not in the valid range.\n";
		cout << "The array's size will be determined randomly.\n";
		setRandomSize();
		initialize();
	}
}

ASCIICode::ASCIICode()
{
	count++;
	
	setRandomSize();
	initialize();
}

ASCIICode::~ASCIICode()
{
	delete [] arrayPtr;
}

void ASCIICode::setRandomSize()
{
	srand(time(0));
	size=(rand()%255)+1;
}

void ASCIICode::initialize()
{
	arrayPtr=new char[size];
	
	for(int i=0; i<size; i++)
		arrayPtr[i]=i;
}

void ASCIICode::boundsCheck()
{
	if(size>=255)
	{
		cout << "\nError! Only 255 ASCII values exist.\n";
		cout << "No more can be added to the array.\n";
		delete [] arrayPtr;
		exit(1);
	}
}

ASCIICode &ASCIICode::operator++()
{
	boundsCheck();
	
	delete [] arrayPtr;
	size+=1;
	
	initialize();
	
	return *this;
}

ASCIICode ASCIICode::operator++(int)
{
	boundsCheck();
	
	ASCIICode tempArray=*this;
	
	delete [] arrayPtr;
	size+=1;
	
	initialize();
	
	return tempArray;
}

int ASCIICode::showSize()
{
	return size;
}

void ASCIICode::save()
{
	ofstream outFile("ASCII characters.txt", ios::out);
	
	if(!outFile)
	{
		cerr << "File could not be opened" << endl;
		delete [] arrayPtr;
		exit(1);
	}
	
	for(int j=0; j<size; j++)
	{
		outFile << j << "\t\t" << arrayPtr[j] << endl;
	}
	
	outFile.close();
}

void ASCIICode::print(int subscript) const
{
	cout << arrayPtr[subscript] << endl;
}

void ASCIICode::print() const
{
	for(int k=0; k<size; k++)
	{
		cout << k << "\t\t" << arrayPtr[k] << endl;
	}
}



#include <iostream>
#include "ASCIICode.h"
using namespace std;

int main()
{
	ASCIICode code1(100);
	cout << "The number of elements in the array is " << code1.showSize() << endl;
	cout << "The number of objects created is " << ASCIICode::showCount() << endl;
	cout << "The 34th element in the array is: ";
	code1.print(33);
	code1.save();
	cout << "All of the elements in the array are:\n";
	code1.print();
	code1++;
	cout << "The number of elements in the array is now " << code1.showSize() << endl;
	
	return 0;
}


This post has been edited by starving_mathematician: 01 July 2010 - 06:39 PM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: malloc error

Posted 01 July 2010 - 06:57 PM

Check your increment overloads. Are the both correct?
Was This Post Helpful? 0
  • +
  • -

#6 starving_mathematician  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

Re: malloc error

Posted 01 July 2010 - 07:04 PM

That's another problem. I'm totally new to operator overloading, and frankly I don't know whether they're right or not. Based on the output I get, they look like they're doing what they're supposed to (increase the size of the array by 1, and reinitialize it). As far as I can tell I'm properly deallocating the memory before I initialize a new array, but again, I'm not sure.
Was This Post Helpful? 0
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: malloc error

Posted 01 July 2010 - 07:17 PM

I see the problem. Here's a question. What happens to the pointer (in the class), when an object is destroyed?

EDIT:

Ok, I'll admit my question is way too cryptic. Here's a point. Would you agree that the following is bad?

delete [] somePtr;
delete [] somePtr;



In other words, freeing memory that is already freed is not a good idea.

Now, I want you to change your destructor to:

ASCIICode::~ASCIICode()
{
    cout << "Destroyed";
    delete [] arrayPtr;
}



Just add in that output. It should be enlightening.

This post has been edited by Oler1s: 01 July 2010 - 07:24 PM

Was This Post Helpful? 1
  • +
  • -

#8 starving_mathematician  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

Re: malloc error

Posted 01 July 2010 - 07:31 PM

Again, I'm pretty iffy on all of this (I'm taking an absurdly fast-faced summer course; this is our first real program using pointers or destructors), but I think that when an object gets destroyed the destructor is called and the operation to release the pointer's array space is carried out. Does it have something to do with me changing something about the nature of the pointer when I increment, so that the destructor doesn't execute properly?

(Btw, I appreciate your teaching style; I intend to be a math professor eventually)
Was This Post Helpful? 0
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: malloc error

Posted 01 July 2010 - 07:37 PM

Why are you guessing? I gave you a big hint, by telling you where exactly to put in output. Did you modify the destructor?

What did you see when you modified the destructor, and ran your code?
Was This Post Helpful? 1
  • +
  • -

#10 starving_mathematician  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

Re: malloc error

Posted 01 July 2010 - 07:39 PM

Sorry, that last post was put up before the page refreshed and I saw your updated comment.

Using your tip, I see that the destructor is being called when I increment the array. But why is my destroying the pointer resulting in the destruction of the entire object? I thought the destructor was only called when the compiler had passed an object's scope?

Does it have to do with how I'm returning tempArray?

This post has been edited by starving_mathematician: 01 July 2010 - 07:54 PM

Was This Post Helpful? 0
  • +
  • -

#11 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: malloc error

Posted 01 July 2010 - 07:52 PM

Quote

But why is my destroying the pointer resulting in the destruction of the entire object?
You're flubbing terminology and abandoning logic here. Freeing memory, by calling delete on a pointer. You are asking why freeing memory causes destruction of the object. That doesn't logically follow. Consider.

Why does pointer get freed? Because destructor is invoked? Why is destructor invoked? Because object is destroyed. Why is destructor invoked twice? There must have been two objects.

Was this your intention? Did you intend for two objects to exist? If yes, then we can examine the logic in the destructor to see why two destructions are problematic.

If no, you should find out where you unexpectedly created object, so you correct your understanding of your own code. But there is no reason why destruction of two objects should crash program. So you still must examine the destructor, and see if there's anything it does that is problematic.
Was This Post Helpful? 1
  • +
  • -

#12 starving_mathematician  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

Re: malloc error

Posted 01 July 2010 - 08:13 PM

Dear god that was difficult, but your "two objects" line saved the day. tempArray was acting as an independent object, and things were getting messed up upon its deletion.

That's the last time I follow a textbook's advice word for word. I was following an example that wasn't using dynamically allocated arrays, and I ended up running into problems that didn't arise in the example.

I can't thank you enough for wasting so much time on me. After this disaster, the next step of adding a proxy class should be a piece of cake (I hope).

This post has been edited by starving_mathematician: 01 July 2010 - 08:13 PM

Was This Post Helpful? 0
  • +
  • -

#13 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: malloc error

Posted 01 July 2010 - 08:36 PM

What book are you using?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1