Objects inside vectors and unallocating contents?

Hi again,

Just a query about memory allocation in Vectors.

I've got a vector of pointers to objects like so:


std::vector<Account*> accounts;

void AccountHandler::addAccount(Account &item)
{
accounts.push_back(&item);
}


and adding like so:


accountMgr->addAccount(*new TaxAccount());


now i'm wondering because i'm creating a pointer there what happens if i want to delete it useing 'delete'... memory management practices etc? Is this a good idea or not?

Thanks.

Comments

  • : Hi again,
    :
    : Just a query about memory allocation in Vectors.
    :
    : I've got a vector of pointers to objects like so:
    :
    :
    : std::vector<Account*> accounts;
    :
    : void AccountHandler::addAccount(Account &item)
    : {
    : accounts.push_back(&item);
    : }
    :

    :
    : and adding like so:
    :
    :
    : accountMgr->addAccount(*new TaxAccount());
    :

    :
    : now i'm wondering because i'm creating a pointer there what happens if i want to delete it useing 'delete'... memory management practices etc? Is this a good idea or not?
    :
    : Thanks.
    :


    A few syntax issues, remove the text in [red]red[/red]:

    void AccountHandler::addAccount(Account &item)
    {
    accounts.push_back([red]&[/red]item);
    }

    accountMgr->addAccount([red]*[/red]new TaxAccount[red]()[/red]);

    And it doesn't seem like a good idea, no. It will be tricky to clean up the variables since you don't have an external pointer to it. But also because the vector class itself may allocate and delete its members. If that happens the the pointer you allocated dynamicly, you will get a memory leak.

    This would work fine though:

    TaxAccount* tax = new TaxAccount;
    accountMgr->addAccount(tax);
    ...
    delete tax;
  • And it doesn't seem like a good idea, no. It will be tricky to clean up the variables since you don't have an external pointer to it. But also because the vector class itself may allocate and delete its members. If that happens the the pointer you allocated dynamicly, you will get a memory leak.
    :
    : This would work fine though:
    :
    : TaxAccount* tax = new TaxAccount;
    : accountMgr->addAccount(tax);
    : ...
    : delete tax;
    :

    Hi,

    Thanks for the fast reply. after consulting another thread I've changed it to this:


    void AccountHandler::addAccount(Account* const item)
    {
    accounts.push_back(item);
    }

    Is this OK?

    Thanks for the hint on the delete'ing of pointers.
  • : And it doesn't seem like a good idea, no. It will be tricky to clean up the variables since you don't have an external pointer to it. But also because the vector class itself may allocate and delete its members. If that happens the the pointer you allocated dynamicly, you will get a memory leak.
    : :
    : : This would work fine though:
    : :
    : : TaxAccount* tax = new TaxAccount;
    : : accountMgr->addAccount(tax);
    : : ...
    : : delete tax;
    : :
    :
    : Hi,
    :
    : Thanks for the fast reply. after consulting another thread I've changed it to this:
    :
    :
    : void AccountHandler::addAccount(Account* const item)
    : {
    : accounts.push_back(item);
    : }
    :

    : Is this OK?
    :
    : Thanks for the hint on the delete'ing of pointers.
    :


    Looks ok, yes.
  • : Thanks for the hint on the delete'ing of pointers.
    Instead of manually deleting pointers, you might like to use a smart pointer. For a std::vector, you can use the boost::ptr_array or use the boost::shared_ptr (www.codepedia.com/1/CppSharedPtr ).

    See ya,
    bilderbikkel

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories