473,408 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

allocation and initialization

In one section of my code, the allocating of memory is chewing up a
significant amount of time. I have solved most of the problem by
creating a custom memory allocator which uses memory pools.

Anyway, my problem is I don't now how to use this custom allocator for
non-builtin types. So for things like ints, doubles, etc this works
great but if I have a class called ClassA can I still do this?

Example:

// For basic types

varx = (int*)myallocator(10*sizeof(int));
// For ClassA

varx = (ClassA*)myallocator(10*sizeof(ClassA));
// but varx's constructor never gets called????

How do I use my custom allocator and initialize the object?

I can't globally overload new because I only want the special allocator
used in the one small area of my code. I can't overload new for
'ClassA' because of the same reason.

Nathan Bullock

Jul 28 '05 #1
1 1238
na*****************@yahoo.ca wrote:
In one section of my code, the allocating of memory is chewing up a
significant amount of time. I have solved most of the problem by
creating a custom memory allocator which uses memory pools.

Anyway, my problem is I don't now how to use this custom allocator for
non-builtin types. So for things like ints, doubles, etc this works
great but if I have a class called ClassA can I still do this?

Example:

// For basic types

varx = (int*)myallocator(10*sizeof(int));
// For ClassA

varx = (ClassA*)myallocator(10*sizeof(ClassA));
// but varx's constructor never gets called????

How do I use my custom allocator and initialize the object?

I can't globally overload new because I only want the special
allocator used in the one small area of my code. I can't overload new
for 'ClassA' because of the same reason.


Unless you use 'new' operator, the constructor is not going to be called.
If you want to allocate some memory yourself, then you are obligated to
use "placement new" to initialise the objects.

varx = new (myallocator(10*sizeof(ClassA))) ClassA[10];

V
Jul 28 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tom | last post by:
What I mean is why can I only allocate const size stuff on the stack in C++? If I want to allocate a variable amount I need to use the OS API (Win32 in my case). Thanks, Tom.
2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
5
by: Lionel | last post by:
Hi all, Just wondering exactly how memory is allocated using the new operator? More specifically I am interested in how the memory is calculated for globable variables? I recently stumbled into...
8
by: Marcus Kwok | last post by:
Given a user defined class with a default constructor (let's call the class C), is there any semantic difference between C* c = new C; and C* c = new C(); ? I am thinking they are...
1
by: Divick | last post by:
Hi I want to know when does the storate allocation happens for member static data? For example I have following class declared in a header typedef void (*fptr)(void); class DllMethods {...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
20
by: sam_cit | last post by:
Hi Everyone, I just heard from a friend of mine that there are few c compilers that give an error when pointers are not initialised to NULL. Is it correct? and if so, is there any standard for...
0
by: kumar | last post by:
HI, I am constantly getting below error. I search on internet but didn't get any help, I would really appreciate if anyone can help me in resolving this issue. I am using DB2 V9.1 and OS is...
5
by: subramanian100in | last post by:
Consider the following classes without ctors. class Sample { private: double d_val; double* d_ptr; }; class Test
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.