answersLogoWhite

0

What are three disadvantages of using pointers in class?

Updated: 8/17/2019
User Avatar

Wiki User

9y ago

Best Answer

Although pointers have many advantages in low-level programming, they are disadvantageous in high-level programming and should be avoided wherever possible. The problems that exist within classes are largely the same as those that exist outside of classes.

A pointer is used to store a memory address and to allow indirect access to the value stored at that memory address. This makes it possible to pass memory addresses to functions, making call by reference possible in languages that only support pass by value semantics. However, a pointer is a primitive data type; it has no members. Therefore it does not encapsulate any information regarding the memory being pointed at other than its type. Thus we can make no assumptions with regards the amount of memory that is safely accessible via a pointer, other than the amount allocated to its type. But if the pointer refers to an array of types, we have no built-in methods of knowing exactly how many elements there are in that array, other than that if the pointer is non-zero then there must be at least one element. Therefore we can only use pointers to arrays where we can be absolutely certain of the number of elements that can be safely accessed through that pointer.

Pointers also have no built-in notion of ownership. The memory being pointed at may well belong to the pointer but it may belong to another object entirely. Ownership can also be passed from one pointer to another or can be shared amongst several pointers. But there's no way of telling which entity specifically owns the memory just from the pointer alone, thus we cannot make assumptions about ownership. This is most important when it comes to releasing memory, because when two or more pointers share the same memory, we could easily end up trying to release the same memory twice.

Both of these problems place the onus of responsibility upon the programmer, even when using pointers within classes. The biggest problem when using them within classes is when an object of the class "owns" the memory being pointed at. If we were to copy one object to another, we cannot simply copy the pointer we must also copy the memory being pointed at, otherwise both objects would end up sharing the same memory. This means the programmer cannot rely on the copy constructor or assignment operator generated by the compiler, they must be user-defined, as must the destructor which must release the memory when the object falls from scope.

However, classes also provide the solution to these problems by allowing the programmer to encapsulate a pointer along with all the missing information related to that pointer. In other words, he can create a "smart pointer"; an object that behaves exactly like an ordinary pointer but where ownership of the memory falls to the object itself. When the object falls from scope it automatically releases the memory. The programmer can also design smart pointers that allow memory to be shared more easily by maintaining a reference counter for each memory resource. The constructors increment the counter while the destructor decrements it. If the count is zero, the destructor releases the memory.

By utilising smart pointers within other classes, those class implementations are greatly simplified because the onus of responsibility shifts to the smart pointer classes themselves; the programmer can concentrate on using pointers rather than worrying about the memory management issues. With a well-designed system of smart pointer classes, there should little or no performance penalty over a manually managed system.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are three disadvantages of using pointers in class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Disadvantages using cyanmethemoglobin method?

three disadvantages cyanmethemoglobin


What are restricted pointers in C99?

Using this you specify that two pointers can't point on the same address


List three disadvantages to using Blind carbon copy versus carbon copy in email?

Its not easy to make corrections on the copies


How to write a C program to find largest 2 numbers using pointers?

program to find maximum of two numbers using pointers


Why are header files not required when using far and near pointers?

In C programming, header files are required. It doesn't matter if you are using near pointers, far pointers, both, or neither -- you still need header files. There is no connection between the necessity of header files and the pointers' size.


Is it mandatory to use the construtors in a class in c plus plus?

No. If you do not provide a default constructor, the compiler will provide a default constructor that simply allocates memory for the class, but it will not initialize the members of the class. If you do not provide a copy constructor, then the compiler will provide a copy constructor that allocates memory for the class, and then copies the member's data from class to class. This is bad if the class contains pointers, because only the pointer will be copied - the objects to which the pointers point will not be copied - and you could wind up deleting an object and then using it after deletion, with potentially devastating consequences. So, yes, it is mandatory, from a good practices point of view, and just plain mandatory when the class has pointers, to always provide a default constructor and a copy constructor, along with the appropriate destructor.


What is multimedia explain three advantages and three disadvantages of using multimedia applications?

various classes of multimedia system


What are the applications of pointers in using c language?

Accessing data by address. Some data-structures, like lists and trees, are usually implemented using pointers.


What are the pointers in computer programming using c?

addresses


What are disadvantages of using car?

What are disadvantages of using car?


Can you get EA Sport to make a PS3 version of NBA live using 95 roster?

To make easy three pointers on the games on NBA live 06 for PS2


Can you write an algorithm to find the greatest of three number using class?

Yes. But why?