Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3969334
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:57:05+00:00 2026-05-20T03:57:05+00:00

I wanted to try out TBB’s scalable_allocator, but was confused when I had to

  • 0

I wanted to try out TBB’s scalable_allocator, but was confused when I had to replace some of my code.
This is how allocation is done with the allocator:

SomeClass* s = scalable_allocator<SomeClass>().allocate( sizeof(SomeClass) );

EDIT: What’s shown above is not how allocation is done with scalable_allocator. As ymett correctly mentioned, allocation is done like this:

int numberOfObjectsToAllocateFor = 1;
SomeClass* s = scalable_allocator<SomeClass>().allocate( numberOfObjectsToAllocateFor );
scalable_allocator<SomeClass>().construct( s, SomeClass());
scalable_allocator<SomeClass>().destroy(s);
scalable_allocator<SomeClass>().deallocate(s, numberOfObjectsToAllocateFor);

It’s pretty much like using a malloc:

SomeClass* s = (SomeClass*) malloc (sizeof(SomeClass));

This is the code I wanted to replace:

SomeClass* SomeClass::Clone() const
{
   return new SomeClass(*this);
}//Clone

So tried a program:

#include<iostream>
#include<cstdlib>
using namespace std;

class S
{
        public:
        int i;
        S() {cout<<"constructed"<<endl;}
        ~S() {cout<<"destructed"<<endl;}
        S(const S& s):i(s.i) {}
};

int main()
{
        S* s = (S*) malloc(sizeof(S));
        s = (S*) S();//this is obviously wrong
        free(s);
}

and here I found that calling malloc does not instantiate the object (I’ve never used malloc earlier). So before figuring out how to pass *this to the copy ctor, I’d like to know how to instantiate the object when working with malloc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:57:05+00:00Added an answer on May 20, 2026 at 3:57 am

    You’ll need to use placement new after getting the raw memory from malloc.

    void* mem = malloc(sizeof(S));
    S* s = new (mem) S(); //this is the so called "placement new"
    

    When you’re done with the object you have to make sure to explicitly call its destructor.

    s->~S();
    free(mem);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just wanted to try out this code... #include <iostream> #include <stdio.h> #include <string.h>
I wanted to try out this funny title bar coloring , but it doesn't
net but wanted to try to do this code in ASP.net instead of my
I wanted to try out some new features in Clang, and I was referred
I just wanted to try out some shaders on a flat image. Turns out
I'm working though SICP and wanted to try out some of the examples in
I wanted to try out some of the automation stuff available through BugSlayer ,
I wanted to try out this example of a self-hosted webservice (originally written in
I wanted to try out IntelliJ-IDEA but my experiments failed in a very early
I wanted to try out some simple operations on files and I started with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.