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

What are compound statements, What are compound statements? - Compound ...

What are compound statements? - Compound statements are made up of two or more program statements that are executed together. They may be executed with a loop. - Curly brack

What is the real function of class - to export data, What is the real funct...

What is the real function of class - to export data? No, Real purpose of a class isn't to export data. Instead, it's to provide services. Class provides a way to abstract behav

Implement a algorithm to verify if the link list , Implement a Algorithm to...

Implement a Algorithm to verify if the link list is in Ascending order? A: template bool linklist::isAscending() const{ nodeptr ptr = head; while (ptr->_next)

String, Write a program that takes 3 small letters as input and sort them a...

Write a program that takes 3 small letters as input and sort them according to their ASCII value.

Area Under Curve, Write a program to find the area under the curve y = f(x)...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Using nested if statement, write a program in c/C++ using nested if stateme...

write a program in c/C++ using nested if statement for calculating the average marks and grades of 5 subjects

OpenGL configured environment, 1. Using Visual C++ and your OpenGL co...

1. Using Visual C++ and your OpenGL configured environment, write an application that displays a “unique” graphical scene that you designed and coded for this course. What yo

C program, program for lexicographically precedes the given input permutati...

program for lexicographically precedes the given input permutation

Hwid spoofer for windows 7, HWID Spoofer for Windows 7, 8 x64 Project De...

HWID Spoofer for Windows 7, 8 x64 Project Description: I want a simple program that will spoof my hardware id. I should be able to prepare the id to spoof to in the program a

Constructor , What is constructor or ctor? need help

What is constructor or ctor? need help

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