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
Create a program WordCount1Main.java doing the following:  For each word in the le word.txt { Create an object of the class Word { Add the object to a set of the type java.uti

how we write decision tables, flowcharts,pseudocode,and algorithm in result management system

Write a C++ program doing the following: The program reads from the keyboard the numbers of rows and columns of a 2-D array of integers. Then, it allocates this array (let’s name i

implement two stacks in one array A[1...n] in such a way that neither stack overflows unless the total number of elements in both stacks together is n. For this you need to produce

Given an integer n and a permutation of numbers 1, 2 ... , n-1, n write a program to print the permutation that lexicographically precedes the given input permutation. If the given



Define a class? A: It is an expanded concept of a data structure: rather than holding only data, it can hold data and functions both.

Recursive Functions Recursion is a process by which a function includes itself with a condition for its safe exit. It is best suitable for a recursive problem. A typical exampl

No. it's not required The C++ language guarantees that delete p will do nothing if p is equivalent to NULL. As you might get the test backwards, and as most testing methodologie