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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:06:04+00:00 2026-05-26T18:06:04+00:00

How do I delete the memory that have been allocated for array models ,

  • 0

How do I delete the memory that have been allocated for array models, names? I tried every method but it always crashes when I run it.

int main()
{

    vector<Person*> people;

    const int PERSON_SZ = 4;
    char * names[] = {"Jim", "Fred", "Harry", "Linda"};
    int ages[] = { 23, 35, 52, 59 };

    for (int i = 0; i < PERSON_SZ; i++)
    {

        Person * temp = new Person(names[i], ages[i]);
        people.push_back(temp);
    }

    // A vector of Car pointers
    vector<Car*> cars;

    const int CAR_SZ = 3;
    char * models[] = { "Festiva", "Ferrarri", "Prius" };
    srand(time(0));
    for (int i = 0; i < CAR_SZ; i++)
    {

        Car * temp = new Car(models[i]);
        temp->set_driver(people[rand() % (people.size())]);
        temp->set_owner(people[rand() % (people.size())]);
        cars.push_back(temp);
    }



    for (int p = 0; p < people.size(); p++)
    {
        people[p]->increment_age();
    }

    for (int c = 0; c < cars.size(); c++)
    {
        cars[c]->print();
    }

    delete [] names;
    for ( int r = 0; r < CAR_SZ; ++r )
       {
           delete [] models[r];
       }
    return 0;
}
  • 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-26T18:06:05+00:00Added an answer on May 26, 2026 at 6:06 pm

    You didn’t allocate models and names using new, so you cannot delete them.

    Both arrays are allocated on the stack and are automatically deleted, when the function returns.

    The contents of the (string) arrays (i.e. the strings itselves) are stored in the global data segment and cannot be freed at all. This would also be dangerous because the compiler might use the same string constant at different places in the program.

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

Sidebar

Related Questions

I have been trying to minimize my memory footprint with UIImagePickerController , but I'm
I have been working on some legacy C++ code that uses variable length structures
i need to have reliable measurement of allocated memory in a linux process. I've
This is a question that's been nagging me for some time. I always thought
I have an odd question that im not sure has been asked/answered, and im
I have been looking into a memory leak issue for a while. I found
I have been told be a couple of tools that the following code is
I believe I have a double delete and some memory corruption happening somehow in
In C++ using delete to free memory obtained with malloc() doesn't necessarily cause a
Is it possible to delete some images from the browser (firefox2) memory using javascript?

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.