Q:

How to pass the address of "new" array member to vector

I have a simple question about new array ; in this code I tried to push the address of new array member into vector of pointers, but I was unable to call the function of this pointers through vector iterator in case if I use the commented code below:
#include <iostream>
#include <vector>
#include <fstream>

std::ofstream trace("test.out") ;

class Counted {
  int id ;
  static int count ;
public:
  Counted():id(count++) {trace << "Counted Object Constructed, ID = "
                   <<id << std::endl;}
  ~Counted(){trace<< "Counted Object destroyed..." << std::endl;}
  int get_id()const {return id;}

};

int Counted::count = 0;

int main() {
  std::vector<Counted*> Counted_vector;
  std::vector<Counted*>:: iterator counted_it ;
  const int buff_size = 5 ;
  //Counted *c = new Counted[buff_size] ;

  /*for(int i = 0; i < buff_size; i++){
    Counted_vector.push_back(&c[i]) ;    // not useful
    }*/

  for(int i = 0; i < buff_size; i++){
    Counted_vector.push_back(new Counted) ;
    }

  counted_it = Counted_vector.begin();
  while (counted_it <= Counted_vector.end()){
    if(*counted_it){
    trace << "*(counted_it)->get_id() = " << (*counted_it)->get_id()
          <<std::endl ;
    delete *counted_it ;}
    else break;
       counted_it++;
  }

}

output:
Counted Object Constructed, ID = 0
Counted Object Constructed, ID = 1
Counted Object Constructed, ID = 2
Counted Object Constructed, ID = 3
Counted Object Constructed, ID = 4
*(counted_it)->get_id() = 0
Counted Object destroyed...
*(counted_it)->get_id() = 1
Counted Object destroyed...
*(counted_it)->get_id() = 2
Counted Object destroyed...
*(counted_it)->get_id() = 3
Counted Object destroyed...
*(counted_it)->get_id() = 4
Counted Object destroyed...

if commented code used:
Counted Object Constructed, ID = 0
Counted Object Constructed, ID = 1
Counted Object Constructed, ID = 2
Counted Object Constructed, ID = 3
Counted Object Constructed, ID = 4
*(counted_it)->get_id() = 0
Counted Object destroyed...

A:

You should not do this:
delete *counted_it ;

If the memory was allocated by Counted *c = new Counted[buff_size] ;, the right way to deallocate the memory should be delete[] c;, you can't delete the element seperately.

