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 6994173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:51:30+00:00 2026-05-27T19:51:30+00:00

Is it necessary for me to call allocator.construct() for an array of primitive types

  • 0

Is it necessary for me to call allocator.construct() for an array of primitive types allocated using an arbitrary allocator, as in the code listing below? The class doesn’t require the allocated memory to be initialized to any particular value, so it seems to me that calling allocator.construct() with a newly-allocated chunk of memory would be unnecessary. Is there any danger in not calling this method, given that the array always consists of primitive types?

template <class T, template <class> class Allocator = std::allocator>
class foo
{
public:
    typedef Allocator<T> allocator;
    typedef typename allocator::pointer pointer;
private:
    unsigned size_;
    allocator alloc_;
    pointer t_;
public:
    foo(unsigned n) throw(std::bad_alloc) : size_(n), alloc_(),
    t_(alloc_.allocate(n))
    {
        // Note that I do not call alloc_.construct() here.
    }

    ~foo() { alloc_.deallocate(t_, size_); }
};
  • 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-27T19:51:31+00:00Added an answer on May 27, 2026 at 7:51 pm

    Yes. The allocator is free to impose whatever custom book-keeping it wants, including the number of existing objects. There is no guarantee at all that it simply does new (memory) T(...). And in addition, it would be a very nasty surprise for a person to change your code so that it’s no longer just primitives and then find it randomly breaks sometime later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using PHP with Curl is it necessary to call curl_close() after every call
An application is using JBoss 4.2.2, and I have found it necessary to call
I'm wondering for a class like the following , is that necessary to call
I have the code snippet below in C++ which basically calculates the pi using
The Win32 API call RegisterTypeLib() is used to create the registry keys necessary to
Is it necessary or advantageous to write custom connection pooling code when developing applications
I want to use/call a method inside EdumateSuperClass that I created in Navigation class.
I've got some code that uses type-punning to avoid having to call a member
Which one do I call? Is it necessary to call both? Will the other
When working with programmatic transactions in Hibernate, is it necessary to explicitly call rollback

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.