Re: Array Container


[ Follow Ups ] [ Post Followup ] [ CS2704 Web Discussion Board ] [ FAQ ]

Posted by Daniel Longest on March 19, 2001 at 02:59:36:

In Reply to: Re: Array Container posted by Amit Nithian on March 17, 2001 at 23:31:50:

I'm going only on what I can infer from the question and your reply but it seems like the idea is that the array should allocate an initial array of pointers to the templated type and that everytime an insert is to be done, the appropriate pointer should allocate memory for the new value, is this what is being said? If that is true, then the destructor for array should take care of calling delete for each of those pointers, as well as deleting the array of pointers itself. I believe normally, the standard library containers assume that if you store dynamic data in a container, then you have the responsibilty for deleting that data. If you do not, it will leak but the library clearly puts the responsibilty on any users of the containers. You could also use an auto pointer class (but not the class in is not designed for this because it doesnt' have I believe the necessary copy constuctor). If I have muddied the water, I apologize, I am just trying to understand what was said.

Daniel

: : I already had the array class designed, when McQuain threw a curve ball on friday in class. I have always thought of containers as truly containing the data they hold. Whether this is logical or physical containment is inconsequential, the point is that a container should be responsible for the data it holds (hence it should both create and destroy it). The other day McQuain introduced this concept as one of association between the container and the data. Though this does make some sense now that I think about it, it introduces some implementation complexity which McQuain did not mention in class. For instance, he said that any data left in the container at the point of the container's destruction should be destroyed by the container. His argument for this was good (i.e., "there is a good change that if it doesn't there will be a memory leak"), however are we going to restrict the container class so that it cannot be passed by value? If it were passed by value the formal parameter would destroy all of the contained data when the function returns, leaving the actual parameter with a whole container full of dangling pointers. Perhaps the container should not be responsible for destroying any of its data, and this should be left as the users responsibility (but then we are giving up the benefit of the constructor/destructor sequence and we might as well be writing in C). This idea seems to introduce an extreme amount of complexity on the user of the container class since he/she must understand these issues and refrain from using pass by value, etc. Also when a multiplicity of container objects are in the same scope, each containing links to the same data, a simple remove operation becomes quite a nightmare (as each container must in turn remove the data, or risk a dangling pointer). In short, this idea makes sense when McQuain says it in class, but it seems to me to be impractical. Not to mention that it introduces some subtle encapsulation violations (how can the container guarantee a sorted sequence when the user of the container may have direct links (pointers) to the data that the container is organizing?, etc, etc). Perhaps I am missing something obvious here, but this assumption that a container is merely associated with its data seems to lead to a number of quite serious flaws (at least one of which has been ground into our heads many times as a test question, i.e. "the shallow copy problem"). Is this the correct way to model containment? (sorry to write a book here, but these questions are keeping me from completing my program design)
: :

: I was wondering parts of what he said. You have to implement the deep copy constructor to allow the user to pass the Array by value. I thought though that it would ease implemenation by making the Array own the Book/Author instances instead of making them associations because where would the deletion of these go, in the destructor of the Database class?

: It confused me in general when McQuain said that it is the user's responsibility to clean up objects (namely the Data element of the Node example in class Friday).

: - Amit Nithian




Follow Ups:



Post a Followup

Name:
E-Mail:

Subject:

Comments:

Optional Link URL:
Link Title:
Optional Image URL:


[ Follow Ups ] [ Post Followup ] [ CS2704 Web Discussion Board ] [ FAQ ]