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

The Archive Base Latest Questions

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

Suppose you have an array of objects in C++ and you remove one of

  • 0

Suppose you have an array of objects in C++ and you remove one of the objects from the array. When is that object’s destructor called? Since C++ does not have automatic garbage collecting, it would seem strange if the destructor was called right after being removed from the array. But does the object stay allocated until the array goes out of scope? That seems like it would lead to memory leaks.

Edit:

Instead of “remove” how about setting one of the elements of the array to a different object.

Also, what does happen when an element is removed from a std::vector?

  • 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-25T03:08:21+00:00Added an answer on May 25, 2026 at 3:08 am

    When you declare a C++ array, it constructs all of the objects on the stack, and they stay there until the array itself is deleted:

    std::string ArrayOfStrings[10]; //There are now ten strings here
    

    Objects can’t be “removed” from this kind of array. When ArrayOfStrings goes out of scope, it automatically deletes all the strings in the array. If you edit an object in this array, that object itself is changed. You actually changed the string itself, not some pointer or reference.

    If you have an array of pointers, then C++ constructs all of the pointers, but you have to manage what they point at yourself:

    std::string* ArrayOfPointersToStrings[10];
    

    You can manually delete strings that the pointers in the array point to, but you have to do all of that yourself. When ArrayOfPointersToStrings goes out of scope, it WILL NOT delete anything the strings point at. If you edit a pointer without deleting it first, that’s a memory leak, and that’s bad.

    If you have a std::vector, C++ constructs only some objects depending on how you construct it:

    std::vector<std::string> VectorOfStrings(10);
    

    This creates a dynamic array of strings. It is possible to remove objects from a vector, in which case the vector instantly deletes it and rearranges the rest for you. If you edit an object in this array, that object itself is changed. You actually changed the string itself, not some pointer or reference.

    To clarify comments:

    char *myLiteral = "APPLE";
    //"APPLE" is an immutable string, myLiteral points at it.
    std::string s= myLiteral; 
    std::string t = myLiteral;
    //s and t each makes a _seperate_ _mutable_ copy of "APPLE"
    s[3] = '?'; 
    //s is now "APP?E", because we just changed the L.  NO COPY WAS DONE. 
    std::cout << t[3];
    //displays "L", because t's copy was never changed.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have the Java code: Object arr = Array.newInstance(Array.class, 5); Would that run?
Is there a way to generically remove an object from an array? (maybe not
Suppose that we have previously instantiated three objects A, B, C from class D
Suppose I have an array of a objects of user defined class. Wanted to
Suppose I have an array of nodes (objects). I need to create a duplicate
Suppose I have an array that mimics a database table. Each array element represents
I have an object array that is being passed and then split up into
A have an array of Objects and I'd like to remove the first element
Suppose I have an object that looks like: public class Obj { String foo;
I'm trying to fetch objects from core data that are not in a given

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.