Re: Vector and derived classes objects

From: ma740988 (ma740988_at_pegasus.cc.ucf.edu)
Date: 11/09/04


Date: 8 Nov 2004 16:12:54 -0800

Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news:<aiMjd.8329$Ae.4337@newsread1.dllstx09.us.to.verio.net>...
> ma740988 wrote:
> > [...]
> > Since I am using just vector of pointers, the copy-ctor or assign-op
> > wont be called for the objects the pointer points to, hence I dont see
> > any way an exception can be thrown there with my push_back.
>
> Huh? push_back beyond the capacity of the vector _requires_ that the
> storage for that vector is grown. That means reallocation. That also
> means the original storage has to stay around until all the copying
> has competed. If your vector occupies at least half the memory that
> your process is allowed to have, there is no way in hell push_back can
> succeed without throwing to tell you that you've run out of memory.

[...]

Victor, appreaciate your patience but this reminds me of a course in
pertubation techniques where it took me 3 texts and days later before
I saw the light hence bear with me once more. I'm not arguing a throw
of bad_alloc, I understand. So now consider:

class X
{
 std::string kdx;
public:
  X() { }
};

int main()
{
  X *ptr_x = new X();
  std::size_t Jdx = sizeof (ptr_x);
  std::cout << Jdx << std::endl;

  std:vector<X*> my_vec;
  my_vec.push_back(ptr_x);
  std::cout << my_vec.size() << std::endl;
  std::cout << my_vec.capacity() << std::endl;

  delete ptr_x;
}

The output
4
1
1

on my platform. Now my_vec is a single byte. That said, I envision
it would require for me then to store a 'slew' of pointers (beyond the
capacity of the vectory - whatever number that is) to my_vec for me to
run out of memory. Correct?



Relevant Pages

  • Re: Linked List & Dynamic Memory Allocation
    ... you have a memory leak. ... that storage will not be available any longer. ... You must call freefor every pointer you got via malloc. ... There are three serious errors you can make using dynamic allocation: ...
    (microsoft.public.vc.mfc)
  • Re: A question about identifiers
    ... object and pass the pointer around. ... In C you can't form a pointer to an object with register storage ... 'register' object in memory, you still can't take & of it. ... The defining characteristic of objects is storage. ...
    (comp.lang.c)
  • Pointer to memory
    ... How does on "point" a pointer at a specific memory address? ... I know I can move a pointer in the malloc'd storage by ... I need to jump from run location to another based on some of the ...
    (comp.lang.c)
  • Re: Objects
    ... i know that an object is a region of memory that can represent values. ... "An object is a named region of storage; an lvalue is an expression referring to an object." ... The struct becomes an object once it's defined. ... after the pointer to which its address was assigned. ...
    (comp.lang.c)
  • Re: do you ever found memory leak with STL?
    ... > and someone say that it's STL strategy. ... > but I dont know when will it release those memory. ... You're using a pointer to an element inside the vector. ...
    (comp.lang.cpp)