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

Assignment Help:

 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

 


Related Discussions:- Is there present a way to force new to allocate memory

Padovan string, padovan string for n natural numbers p(n)=1,p(n)=2,p(n)=3 a...

padovan string for n natural numbers p(n)=1,p(n)=2,p(n)=3 and use padovan formul to get output 1

Palindrome, Problem : Change to palindrome A palindrome is a string that r...

Problem : Change to palindrome 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 ta

C program to print r diagonal triangle, C program to print R diagonal trian...

C program to print R diagonal triangle: #define rows 3 #define cols 3 void main() {                 int i=0,j=0;                 int arr[rows][cols];

C program to swap the two values, Program is to swap the two values which a...

Program is to swap the two values which are enter by user: Program is to swap the two values which are enter by user through the function with values changed using pointers

Input, I want to take 1.1 as input but when I am declaring it as float the ...

I want to take 1.1 as input but when I am declaring it as float the output is given as 1.1000000

D, drawbacks in assignments in engeenirng

drawbacks in assignments in engeenirng

Develop an auto-click bot, What I need is an auto-click bot. The auto-click...

What I need is an auto-click bot. The auto-click bot would ask me for a url(or it would be pre-defined too) so that it loads that specific url and click an ad automatically. This w

Required code for problem, 6#666#665533999 where # is used as space.output ...

6#666#665533999 where # is used as space.output is monkey

Board coloring, color representation 0,1,2,3,4,5,6,7...

color representation 0,1,2,3,4,5,6,7...

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd