Click here to Skip to main content
15,920,896 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Problems with realloc - heap corruption Pin
nadiric7-Sep-06 7:03
nadiric7-Sep-06 7:03 
GeneralRe: Problems with realloc - heap corruption Pin
Waldermort7-Sep-06 7:20
Waldermort7-Sep-06 7:20 
GeneralRe: Problems with realloc - heap corruption Pin
nadiric7-Sep-06 7:24
nadiric7-Sep-06 7:24 
GeneralRe: Problems with realloc - heap corruption Pin
Waldermort7-Sep-06 7:28
Waldermort7-Sep-06 7:28 
AnswerRe: Problems with realloc - heap corruption Pin
Jun Du7-Sep-06 7:15
Jun Du7-Sep-06 7:15 
GeneralRe: Problems with realloc - heap corruption Pin
nadiric7-Sep-06 7:22
nadiric7-Sep-06 7:22 
GeneralRe: Problems with realloc - heap corruption Pin
Jun Du7-Sep-06 7:43
Jun Du7-Sep-06 7:43 
GeneralRe: Problems with realloc - heap corruption Pin
Zac Howland7-Sep-06 8:39
Zac Howland7-Sep-06 8:39 
nadiric wrote:
child *child1 = new child; //creating a child somewhere in code

parent1.addchild(child1); //adding the child to a parent (the code in question)


You may have issues here as well. new and malloc/calloc/realloc use different heaps. You are using new to allocate your objects and realloc to allocate your pointers. Mixing heaps like this can cause some interesting and hard to debug issues.

As a side note, you would probably get better performance by using a deque or a vector (with a pre-allocated buffer). Since each time you are adding a child, you are basically forcing a new memory allocation (actually 2, since you had to allocate the child object and then a pointer to it). You might find it easier, and more efficient to do something like the following:

class Child
{
public:
	Child() : m_Data(0)
	{
	}
	
	// whatever other methods you want here
private:
	int m_Data;	// could be anything
};

// deque version
class Parent
{
public:
	Parent()
	{
	}

	void addChild(Child* pChild)
	{
		m_Children.push_back(pChild);
	}
private:
	std::deque<Child*> m_Children;
};

// vector version
const unsigned long DEFAULT_SIZE = 100;	// whatever a good default size for your application is
class Parent
{
public:
	Parent()
	{
		m_Children.reserve(DEFAULT_SIZE);
	}

	void addChild(Child* pChild)
	{
		m_Children.push_back(pChild);
	}
private:
	std::vector<Child*> m_Children;
};




If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week

Zac

AnswerRe: Problems with realloc - heap corruption Pin
Stephen Hewitt8-Sep-06 2:03
Stephen Hewitt8-Sep-06 2:03 
QuestionAnother impossible question Pin
Waldermort7-Sep-06 6:50
Waldermort7-Sep-06 6:50 
AnswerRe: Another impossible question Pin
JWood7-Sep-06 7:09
JWood7-Sep-06 7:09 
GeneralRe: Another impossible question Pin
Waldermort7-Sep-06 7:15
Waldermort7-Sep-06 7:15 
GeneralRe: Another impossible question [modified] Pin
Anonymuos7-Sep-06 11:38
Anonymuos7-Sep-06 11:38 
AnswerRe: Another impossible question Pin
Jun Du7-Sep-06 7:36
Jun Du7-Sep-06 7:36 
QuestionRe: Another impossible question Pin
Zac Howland7-Sep-06 8:50
Zac Howland7-Sep-06 8:50 
AnswerRe: Another impossible question Pin
Waldermort7-Sep-06 18:41
Waldermort7-Sep-06 18:41 
Questionpreprocessor directive Pin
Lord_Vader7-Sep-06 6:49
Lord_Vader7-Sep-06 6:49 
QuestionRe: preprocessor directive Pin
David Crow7-Sep-06 8:18
David Crow7-Sep-06 8:18 
AnswerRe: preprocessor directive Pin
Chris Losinger7-Sep-06 8:22
professionalChris Losinger7-Sep-06 8:22 
AnswerRe: preprocessor directive Pin
Hamid_RT8-Sep-06 7:28
Hamid_RT8-Sep-06 7:28 
QuestionMFC Toolbar handle Pin
ninolaroca7-Sep-06 6:05
ninolaroca7-Sep-06 6:05 
Questionwprintf failing me Pin
Waldermort7-Sep-06 5:33
Waldermort7-Sep-06 5:33 
AnswerRe: wprintf failing me Pin
Zac Howland7-Sep-06 5:50
Zac Howland7-Sep-06 5:50 
GeneralRe: wprintf failing me Pin
Waldermort7-Sep-06 5:56
Waldermort7-Sep-06 5:56 
GeneralRe: wprintf failing me Pin
Waldermort7-Sep-06 5:59
Waldermort7-Sep-06 5:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.