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

The Archive Base Latest Questions

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

please help to figure this out. I have some leaking code, and I don’t

  • 0

please help to figure this out.
I have some leaking code, and I don’t know how to handle it

vector <ItemClass> items( 10 );
items[1] = ItemClass( "DVD Player", 560 );
items[5] = * new ItemClass( "Blu Ray Player", 900 );

How should I free memory for items[5] ?
I’m getting error on my attempts of freeing memory

delete &items[5];

delete [] &items[5];

I even tried something like

ItemClass * delItem = &items[5];
items[5] = item4;
delete delItem;

I’m getting “corruption of the heap” in VS2010 Ultimate

  • 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-30T19:19:08+00:00Added an answer on May 30, 2026 at 7:19 pm

    There is a strange use and mix up of storing new-allocated objects in vectors in your code. Usually you should handle lists all in the same way. Thus my examples below will be more explicit, and independent from each other which should help you understand the differences.

    Use delete for objects only. See code below.

    Use delete [] for “native” arrays only, not for vector or similar container classes or objects inside them.

    I will not give an example for arrays though, since arrays might be more confusing.

    Default example stack:

    vector <ItemClass> items( 10 );
    
    // does not need to be deleted because item is on the stack
    ItemClass item("device1", "10"); 
    items.push_back(item);
    

    Default example with heap allocation:

    vector <ItemClass*> items( 10 );
    
    ItemClass* pItem = new ItemClass("device2", "20");
    items.push_back(pItem);
    
    // delete all items inside vector
    for (int i = 0; i < items.size(); i++)
    {
        ItemClass* pToDelete = items[i];
        delete pToDelete;
        items.erase(i);
    }
    

    The following examples should be avoided and are for clarifying things up only! Use at own risk.

    Storing addresses of stack variables:

    vector <ItemClass*> items( 10 );
    
    // does not need to be deleted because item is on the stack
    ItemClass item("device3", "30"); 
    items.push_back( &item ); // storing a reference to item
    
    // No need to delete this item that points to something on the stack.
    // However you might not be able to tell items apart which have been 
    // created on the heap or the stack, so just dont do it.
    

    If you mix up with the example before and store references and pointers you must handle it yourself. My recommendation: Do not do this.

    Storing dereferenced items:

    vector <ItemClass> items( 10 );
    
    ItemClass* pItem = new ItemClass("device4", "40"); 
    items.push_back(*pItem);
    
    // must be deleted because allocated on the heap
    // Again you can not tell which item is allocated on stack or heap. Avoid this.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help me figure this out. I apologize if this is a duplicate question,
Can someone please help me figure out a way to achieve the following (see
Please help! I couldn't figure it out how to map the following situation: I
i've been trying to figure this out and i have absolutely no clue why
I just don't seem to be able to figure this out in my head.
I'm compiling PHP5.2.9 on Mac OS X 10.5.6. Need some help to figure this
Ok I am still having some issues with this code below. I have received
Please help! I'm still learning so this might be completely wrong. I have built
Please help me figure a single query that will transform the data below... |id
Please help! Background info I have a WPF application which accesses a SQL Server

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.