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

  • Home
  • SEARCH
  • 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 8008099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:02:10+00:00 2026-06-04T18:02:10+00:00

I am fully aware that if I want to create a smart pointer to

  • 0

I am fully aware that if I want to create a smart pointer to an array the best way is to use

     boost::shared_array<T>( new T[20] );

What I don’t understand is the crash I have when a shared pointer of this type gets out of scope.

     boost::shared_ptr<T>( new T[20] );

I always thought that the code above would create a leak since it calls delete on the first element of the array by leaving untouched the others but this is not the case and I have a segmentation fault.

Can you help me understanding why? I have worst behavior if I don’t use the shared pointer but a plain vanilla pointer

       CMyClass *p = new CMyCLass[10];
       ///....do stuff
       delete p;

       *** glibc detected *** ./test: munmap_chunk(): invalid pointer: 0x088eb00c ***
  • 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-06-04T18:02:11+00:00Added an answer on June 4, 2026 at 6:02 pm

    Can you help me understanding why? I have worst behavior if I don’t use the shared pointer but a plain vanilla pointer

    Because your code has an Undefined Behavior.

    CMyClass *p = new CMyCLass[10];
    ///....do stuff
    delete []p;
          ^^^^ <-------------that is Missing!
    

    You need to use delete [].

    1. If you allocated using new you must use delete.
    2. If you allocated using new [] you must use delete []

    Doing otherwise leads to Undefined Behavior. And one its Undefined Behavior then your program might show any behavior.It might crash or work or whatever(literally) so all safe bets are off.

    How to use custom deallocation with shared_ptr?

    For shared_ptr you should use your own custom deletor function to deallocate appropriately. In case of array allocation using new [], you need to use delete []. Something like:

    template
    class CustomDeleter
    {
    public:
        void operator () (T* d) const
        {
            delete [] d;
        }
    };
    
    int main ()
    {
        std::shared_ptr array(new Myclass[256], CustomDeleter());
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Being resonably new to using GTK+, im not fully aware of all its functionality.
I fully appreciate the atomicity that the Threading.Interlocked class provides; I don't understand, though,
I'm going to create some command line tools that make use of some large
I'm fully aware of the impact of flagging a thread as a daemon thread
I'm not fully getting all i want from Google analytics so I'm making my
Does anybody know a fully featured refactoring tool for C++ that works reliably with
Please not that I fully understand this is a dumb ass idea, but its
I would like to create a custom WPF control that inherits from comboBox. So
I am well aware of PCI Compliance so don't need an earful about storing
I am trying to use examples that are fun/interesting to me in order to

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.