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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:48:06+00:00 2026-05-11T06:48:06+00:00

Is it considered bad manners/bad practice to explicitly place object members on the heap

  • 0

Is it considered bad manners/bad practice to explicitly place object members on the heap (via new)? I would think you might want to allow the client to choose the memory region to instantiate the object. I know there might be a situation where heap members might be acceptable. If you know a situation could you describe it please?

  • 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. 2026-05-11T06:48:07+00:00Added an answer on May 11, 2026 at 6:48 am

    If you have a class that’s designed for copy semantics and you’re allocating/deallocating a bunch of memory unnecessarily, I could see this being bad practice. In general, though, it’s not. There are a lot of classes that can make use of heap storage. Just make sure you’re free of memory leaks (deallocate things in the destructor, reference count, etc.) and you’re fine.

    If you want more flexibility, consider letting your user specify an Allocator. I’ll explain.

    Certain classes, e.g. std::vector, string, map, etc. need heap storage for the data structures they represent. It’s not considered bad manners; when you have an automatically allocated vector, the user is expected to know that a buffer is allocated when the vector constructor gets called:

    void foo() {     // user of vector knows a buffer that can hold at least 10 ints     // gets allocated here.     std::vector<int> foo(10);   } 

    Likewise, for std::string, you know there’s an internal, heap-allocated char*. Whether there’s one per string instance is usually up to the STL implementation; often times they’re reference counted.

    However, for nearly all of the STL classes, users do have a choice of where things are put, in that they can specify an allocator. vector is defined kind of like this:

    template <typename T, typename Alloc = DefaultAllocator<T> > class vector {     // etc. }; 

    Internally, vector uses Alloc (which defaults to whatever the default allocator is for T) to allocate the buffer and other heap storage it may need. If users doesn’t like the default allocation strategy, they can specify one of their own:

    vector<int, MyCustomAllocator> foo(10); 

    Now when the constructor allocates, it will use a MyCustomAllocator instead of the default. Here are some details on writing your own STL allocator.

    If you’re worried that it might be ‘bad manners’ to use the heap for certain storage in your class, you might want to consider giving users of your class an option like this so that they can specify how things are to be allocated if your default strategy doesn’t fit their needs.

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

Sidebar

Related Questions

Is it considered bad coding-practice to store functions in an object instead of just
Would this code be considered bad practice: <div id=sidebar> <% =DisplayMeetings(12) %> </div> This
Would it be considered bad practice to use sub-namespaces for purely organizational purposes? For
Would it be considered bad practice to have a viewmodel that has a property
Would this be considered bad practice? unless Link.exists?(:href => 'example.com/somepage') Domain.where(:domain => 'example.com').first.links.create(:href =>
Is it considered bad practice to have multiple try's in one method and structure
Is it considered bad practice to have multiple views for same URL in MVC,
Is it generally considered bad practice to provide Iterator implementations that are infinite; i.e.
Is using an instance of a class to access static methods considered bad practice?
In C, it is considered bad practice to call strlen like this: for (

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.