- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I just started playing with memory allocators - up to now I only used new/delete.
And possibly I observeda bug - the following code crashes on my box (for both scalable and cache aligned allocators):
#include
#include "tbb/scalable_allocator.h"
#include "tbb/cache_aligned_allocator.h"
struct sth {
tbb::scalable_allocator alloc;
//tbb::cache_aligned_allocator alloc;
int main () {
As a matter of fact, switching the method to be non-virtual makes the program work correctly.
Is it a bug ? Or possibly I'm doing something wrong ?
Best regards
Bartlomiej
I just started playing with memory allocators - up to now I only used new/delete.
And possibly I observeda bug - the following code crashes on my box (for both scalable and cache aligned allocators):
#include
#include "tbb/scalable_allocator.h"
#include "tbb/cache_aligned_allocator.h"
struct sth {
double x;
int i;
int i;
sth (double ax, int ai) : x(ax), i(ai) {};
virtual void show (void) {
std::cout << "x = " << x << std::endl;
std::cout << "i = " << i << std::endl;
};
};tbb::scalable_allocator
//tbb::cache_aligned_allocator
int main () {
sth *ex = alloc.allocate (1);
ex->x = 3.1415926;
ex->i = 13;
ex->show ();
alloc.deallocate (ex, 1);
return 0;
}As a matter of fact, switching the method to be non-virtual makes the program work correctly.
Is it a bug ? Or possibly I'm doing something wrong ?
Best regards
Bartlomiej
1 Solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - bartlomiej
Also, I hope, using the new() operator that way does not require any synchronization with other threads?
Yes, it's no more that a call of a class constructor. If constructor is empty and class does not have virtual functions, then this call will be completely optimized away in release build.
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Besides allocation of a memory for the object, you have to construct the object itself:
void* mem = alloc.allocate(1);
sth* ex = new (mem) sth (3.14, 13);
Otherwise you just consider memory block filled with trash as an C++ object which is dead wrong.
See the ISO C++ Standard section 3.8. Object Lifetime and 20.8.2 (may differ in your revision) Allocators.
void* mem = alloc.allocate(1);
sth* ex = new (mem) sth (3.14, 13);
Otherwise you just consider memory block filled with trash as an C++ object which is dead wrong.
See the ISO C++ Standard section 3.8. Object Lifetime and 20.8.2 (may differ in your revision) Allocators.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - bartlomiej
Also, I hope, using the new() operator that way does not require any synchronization with other threads?
Yes, it's no more that a call of a class constructor. If constructor is empty and class does not have virtual functions, then this call will be completely optimized away in release build.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page