Suppose that a stack ADT was designed which returned a pointer

mahmed3
mahmed3 used Ask the Experts™
on
Suppose that a stack ADT was designed which returned a pointer to the top element rather than a copy of the element. Why is this not data abstraction?
can you use an example to show the problem with this design.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The very definition of data abstraction is, that the interface must be completely independent of the implementation. Sticking to it enables you to rip out the internal of a class and stuff in a new and improved motor, without affecting any code using the class!

The stack is called an ADT exactly because it is an Abstract Data Type - it has an explanation, a set of legal states, attributes and methods for manipulation, but it is NOT just a class in C++ (or any other language for that matter). It is an abstraction, an idea, an ideal structure.

Any implementation returning pointers or references to internal (private) variables violates data abstraction as the interface now contains traces of the implementation. Either it reveals something about the stability of internal variables or simply that the class was designed by a not-so-stable programmer.

Example:

A trivial problem with Top returning a pointer is that the const can be cast away thereby allowing opportunist programmers unrestricted access to private variables. Never a good idea.

More advanced, suppose you create a stack class, where the constructor takes some size parameter indicating the likely maximal number of elements that will be stored in the stack. This parameter is used to allocate exactly that amount of internal memory for the object to use. In case more elements are needed, the stack is reallocated to make room for more. Fetching a pointer to the top element and then pushing a lot of new elements onto the stack will force a reallocation invalidating the obtained pointer. Burn, baby, burn!

     Stack stack(8);             // Create a stack with room for 8 integers.
     stack.Push(100);
     int *pTop=stack.Top();  // Fetch a pointer to the top element.
     /* Push more elements onto (size > 8) causing reallocation... */
     /* Do whatever... */
     int top=*pTop;          // ERROR: pTop is now invalid because of the reallocation!

Instead a copy should be returned, since the copy may exist beyond the lifetime of the internal variable; its stability is completely controlled by the user that requested it. Using constant references will not repair this treacherous bug, though this technique is often applied to improve performance. Of course implementations exist which ensure the validity of the pointers and references but such guarantees do themselves violate the data abstraction principle as they too show detail of the implementation.

If it is vital that a copy is not created, a more complex scheme using markers or iterators (like in the standard library) along with rules concerning validity must be supplied.


Commented:
This question didn't show any activity for more than 21 days. I will ask Community Support to close it unless you finalize it yourself within 7 days.
You can always request to keep this question open. But remember, experts can only help if you provide feedback to their comments.
Unless there is objection or further activity,  I will suggest to accept

    "constructor"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
========
Werner
Force accepted

** Mindphaser - Community Support Moderator **

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial