Is there present a way to force new to allocate memory, C/C++ Programming

 Yes. "Memory pools" are useful in many situations. The bad news is that I'll need to drag you through the mire of how it acts before we talk about all the uses.

Firstly, recall that a memory allocator is simply imagined to return uninitialized bits of memory; this is not supposed to generate "objects." Particularly, the memory allocator is not imagined to set the virtual-pointer or any other part of object, since that is the job of the constructor that runs after memory allocator. Beginning with a simple memory allocator function, allocate(), you would employ placement new to build an object in that memory. In other terms, the following is equivalent morally to new Foo():

void* raw = allocate(sizeof(Foo)); // line 1

Foo* p = new(raw) Foo(); // line 2

Supposing you've utilized placement new and have survived the above two lines of code, the after that step is to turn your memory allocator in an object. This type of object is called as a or a "memory arena." Or "memory pool" .This allows your users have more than one "pool" or "arena" from which memory shall be allocated. Each memory pool objects will allocate a big chunk of memory via some specific system call (for example. shared memory, persistent memory, stack memory, etc.; ), and will dole it out in little chunks as required. Your memory-pool class might look something as:

class Pool {

public:

void* alloc(size_t nbytes); void dealloc(void* p); private:

...data members used in your pool object...

};

 

void* Pool::alloc(size_t nbytes)

{

...your algorithm goes here...

}

 

void Pool::dealloc(void* p)

{

...your algorithm goes here...

}

Now one of your users might have a Pool called pool, from which they could allocate objects such as

 

Posted Date: 3/16/2013 3:24:06 AM | Location : United States







Related Discussions:- Is there present a way to force new to allocate memory, Assignment Help, Ask Question on Is there present a way to force new to allocate memory, Get Answer, Expert's Help, Is there present a way to force new to allocate memory Discussions

Write discussion on Is there present a way to force new to allocate memory
Your posts are moderated
Related Questions
Project Description: collect / modify person records / photo images/ lookup / facebook iphone and ipad App basic function : 1 user will configure facebook connection

A:  it is containing and hiding Information regarding an object, like internal data structures and code. It isolates the internal complication of an object's operation from the res

Objective of this Program: (a) To learn more about modularization of a program into independent modules, (b) To use arrays in a program, (c) To get some experience with us

application problem of tower of hanoi

Newton's method for cube roots is based on the fact that if y is an approximation to the cube root of x, then a better approximation is given by the value: (x/y 2 +2y)/3 (a)

I would like to get you to collaborate in my project. We want a C programmer to Develop applications for Windows, the first application may be an Instant messenger encrypted in a w

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

Define the Data Type Qualifiers in c Language? 1. const 2. volatile const: The const qualifier is used to tell C that the variable value can't change after initialization.

Create a class Word, representing a word. Two words should be considered equal if they consist of the same sequence of letters and we consider upper case and lower case as equal. F

C Program for REMOVING CHAR WHICH U WANT void main() {           int i,j;           char a[100],r;           clrscr();           for(i=0;i