Re: [C++] Vectors & Dynamic allocation
From: Anand Hariharan (desh_bakth_at_yahoo.com)
Date: 05/01/04
- Next message: Cerf: "Re: Runtime errors but no compiletime errors"
- Previous message: shonamorrison_at_optonline.net: "Princess Diana Nude, From Photo Archives"
- Next in thread: B. v Ingen Schenau: "Re: [C++] Vectors & Dynamic allocation"
- Maybe reply: B. v Ingen Schenau: "Re: [C++] Vectors & Dynamic allocation"
- Reply: Robert W Hand: "Re: [C++] Vectors & Dynamic allocation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 30 Apr 2004 16:52:51 -0700
Robert W Hand <rwhand@NOSPAMoperamail.com> wrote in message news:<bm44909io91ceu7kt4kjhforg3nmvjp8m8@4ax.com>...
> >> I agree that complexity requirements for
> >> std::vector::erase clearly prohibit deallocation of the elements.
> >
> >Am unsure what the complexity requirements might have to do with the
> >deallocation of elements.
>
> In the section of the Standard devoted to std::vector<T>::erase, there
> is a paragraph labeled "Complexity". It states that the destructor of
> T is called the number of times equal to the number of elements
> erased, but the assignment operator for T is called the number of
> times equal to the number of elements after the erased elements.
> There is no provision for deallocation. Furthermore, since iterators
> before the erasure remain valid, an allocation/deallocation step seems
> impossible. Deallocation/allocation would invalidate all the
> iterators.
>
Bob - It was only from the above para, it became clear to me that you
are talking about allocation/deallocation of vector (i.e.,
reallocation/resizing).
>From the context of your statement in your earlier post (at the top of
this post), I thought we were talking about deallocation of the memory
pointed to by the pointers contained in the vector (the example was
talking about vector<int *>). That was what I meant when I said ...
> >AFAICS, getting vector's erase to check if the elements need
> >deallocation etc mimics Garbage collection - a price a developer might
> >not be willing to pay. However, even if this were to be included, its
> >complexity would still be O(n), isn't it?
>
>From my above para, I wonder if it is clear that I was talking about
the memory held by the pointers to int (the elements of the vector)
rather than the vector itself.
> The complexity of erase depends upon the type of container. For
> vector, it seems to me that we are mixing different lengths of two
> different operations. Erasing the last element of a vector should be
> different than erasing the first element. In the case of the last
> element, only one element is erased and none copied. In the case of
> the first element, one element is erased and size()-1 are copied.
> Doesn't seem to fit into a neat O(0) complexity. Perhaps someone has
> a different idea?
(Very) loosely put, Complexity refers to the number of operations
required to perform an operation, expressed as a function of the input
size. Specifics / border-line/base cases are not separately accounted
for. E.g., "Complexity of sorting is O(n lg n)" does not account for
the characteristics of a specific instance of input. Of course, if
the input set is already sorted, a sorting algorithm could "sort" its
input in linear time.
wondering if this thread could sustain much longer,
- Anand
- Next message: Cerf: "Re: Runtime errors but no compiletime errors"
- Previous message: shonamorrison_at_optonline.net: "Princess Diana Nude, From Photo Archives"
- Next in thread: B. v Ingen Schenau: "Re: [C++] Vectors & Dynamic allocation"
- Maybe reply: B. v Ingen Schenau: "Re: [C++] Vectors & Dynamic allocation"
- Reply: Robert W Hand: "Re: [C++] Vectors & Dynamic allocation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|