LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-14-2004, 09:19 AM   #1
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Cross-module in-place new allocation question (C++)


If I were to have the code below, each module being independently compiled, would the allocation 'new char[10]' (start at 'ProcessFunction()') use the heap for Module 1 or Module 2? The in-place new call is made in Module 1, however the actual object is constructed on Module 2's heap. Also, does 'ProcessedObject' need to be delete[]d as an 'unsigned char*', or can I delete it as-is? Thanks.
ta0kira

Code:
//Module 1----------------------------------------------------------------------
class Object
{
public:
	Object() : Data(new char[10]) {}

		void
	ObjectFunction();

	~Object() { delete[] Data; }

private:
		char
	*Data;
};


	size_t
ObjectSize() { return sizeof(Object); } //Give size of object to be created


	Object
*InsertObject(Object *oObj) { return new(oObj) Object(); } //Create object
//------------------------------------------------------------------------------


//Module 2----------------------------------------------------------------------
class Object;


extern
	size_t
ObjectSize();


extern
	Object
*InsertObject(Object*);


	void
ProcessFunction()
{
//Allocate space and insert a new object
Object *ProcessedObject =
InsertObject(reinterpret_cast <Object*> (new unsigned char[ObjectSize()]) );

//Call a function
ProcessedObject->ObjectFunction();

//Deallocate space
delete ProcessedObject;

//-or?-

ProcessedObject->~Object();
delete[] reinterpret_cast <unsigned char*> (ProcessedObject);
};
//------------------------------------------------------------------------------


//Main--------------------------------------------------------------------------
int main()
{
ProcessFunction();
return 0;
}
//------------------------------------------------------------------------------
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Inode allocation question. saudoi Solaris / OpenSolaris 5 11-15-2005 07:00 AM
Question about RAM allocation and what 'seems' like very odd swap use NoeticRapture Slackware 8 06-25-2005 03:08 PM
Where is modversion.h in 2.6? Trying to cross compile a module. jackcomputero Linux - Hardware 0 12-01-2004 01:51 PM
dynamic memory allocation in kernel module appas Programming 4 09-21-2004 06:36 AM
Memory allocation question! alexzander Programming 3 01-24-2003 10:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration