answersLogoWhite

0


Want this question answered?

Be notified when an answer is posted

Add your answer:

Earn +20 pts
Q: Memory mangment New and delete pointer to object link list?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Algorithm to delete?

The only way to delete objects in an object oriented programming language (unless they were created in heap memory) is for the object to go out of scope. If the object is declared in the heap, in c++ you would use delete[] ptr; or delete ptr; where ptr is a pointer to your object.


Trying to delete uninitialized pointer to object?

You cannot delete an uninitialized pointer, because there is no allocation for the object, and the pointer contains garbage. That includes the case where you attempted allocation and failed, but deletion is safe in that case because a NULL pointer is "safe" to delete, even though it does not point at anything.


How do you use delete function in inheritence in classes using c plus plus?

You use delete to release the memory occupied by an object that is being pointed to by the pointer you delete. Provided all base classes have a virtual destructor, it doesn't matter what the type of pointer you actually delete is, so long as it is a type of class from which the object is actually derived. The destructor from the most-inherited class (the object itself) will be called first, which calls the destructors of its immediate base classes, and theirs, and so on until the object is completely released from memory.Note that you must not delete pointers to object references -- they must be nullified (not deleted) when they are no longer required. The referenced object will be deleted automatically when it falls from scope.


What is called pointers-c plus plus?

Yes, C++ has pointers, which are references to memory locations. which are variables that store memory addresses, or NULL (zero). If the pointer is non-NULL, the pointer is said to dereference the object (or variable) residing at the stored memory address, which permits indirect access to that object so long as the object remains in scope.


Should you explicitly call a destructor in C programming?

Not to be pedantic, but you cannot call a destructor explicitly. Destructors are implicitly called when an object falls from scope or when you delete a pointer to an object. Any object created dynamically (with the new keyword) must be deleted as soon as you are finished with it, and before the pointer falls from scope. In this sense, you are explicitly calling the object's destructor, however it's really being called implicitly by you deleting the pointer. It's also good practice to explicitly NULL your pointer immediately after deleting the object it pointed to. An object reference is destroyed automatically when the reference falls from scope. If you have a pointer to that reference, do not delete the pointer, but do assign it to NULL as soon as possible to prevent any access to the deleted object. If you do delete a pointer to a reference that's still in scope, you will render the reference NULL and a NULL reference will render your program invalid.

Related questions

Algorithm to delete?

The only way to delete objects in an object oriented programming language (unless they were created in heap memory) is for the object to go out of scope. If the object is declared in the heap, in c++ you would use delete[] ptr; or delete ptr; where ptr is a pointer to your object.


Trying to delete uninitialized pointer to object?

You cannot delete an uninitialized pointer, because there is no allocation for the object, and the pointer contains garbage. That includes the case where you attempted allocation and failed, but deletion is safe in that case because a NULL pointer is "safe" to delete, even though it does not point at anything.


What is a memory pointer?

A pointer is a variable that stores a memory address and that allows indirect access to the object or value stored at that address.


What is dangling pointer reference in c plus plus?

A dangling pointer (we also use the terms stray pointer and wild pointer) is created whenever we call delete on a pointer and then try to use the pointer without reassigning it.We can also create dangling pointers inadvertently by calling a rogue function that returns a pointer to an object that is local to the function we are calling. The object will fall from scope when the function returns so the pointer is left dangling.Note that there is no such thing as a dangling pointer reference. Pointers and references are not the same. A reference is merely an alias to an object -- it consumes no memory beyond the object it refers to. Whereas a pointer is a variable that may contain the address of an object, but it requires additional memory to do so (4 bytes on 32-bit architecture). Pointers may be NULL, references can never be NULL. Pointers to valid objects require indirection, references do not. References are the preferred method of accessing an object's members, not least because they are easier to work with.


How do you use delete function in inheritence in classes using c plus plus?

You use delete to release the memory occupied by an object that is being pointed to by the pointer you delete. Provided all base classes have a virtual destructor, it doesn't matter what the type of pointer you actually delete is, so long as it is a type of class from which the object is actually derived. The destructor from the most-inherited class (the object itself) will be called first, which calls the destructors of its immediate base classes, and theirs, and so on until the object is completely released from memory.Note that you must not delete pointers to object references -- they must be nullified (not deleted) when they are no longer required. The referenced object will be deleted automatically when it falls from scope.


What is called pointers-c plus plus?

Yes, C++ has pointers, which are references to memory locations. which are variables that store memory addresses, or NULL (zero). If the pointer is non-NULL, the pointer is said to dereference the object (or variable) residing at the stored memory address, which permits indirect access to that object so long as the object remains in scope.


Is it possible to delete a const object in C plus plus?

Yes. However, making a const pointer seems rather pointless, as you will be able to allocate and deallocate memory for it, but you will be unable to change the contents.


Definition of dangling pointer object?

A dangling pointer occurs when objects have been deallocated or deleted from the system. They 'dangle' due to the pointer's values still remaining leaving a location to the non-existent object in the memory.


Should you explicitly call a destructor in C programming?

Not to be pedantic, but you cannot call a destructor explicitly. Destructors are implicitly called when an object falls from scope or when you delete a pointer to an object. Any object created dynamically (with the new keyword) must be deleted as soon as you are finished with it, and before the pointer falls from scope. In this sense, you are explicitly calling the object's destructor, however it's really being called implicitly by you deleting the pointer. It's also good practice to explicitly NULL your pointer immediately after deleting the object it pointed to. An object reference is destroyed automatically when the reference falls from scope. If you have a pointer to that reference, do not delete the pointer, but do assign it to NULL as soon as possible to prevent any access to the deleted object. If you do delete a pointer to a reference that's still in scope, you will render the reference NULL and a NULL reference will render your program invalid.


What is an address in C plus plus programming?

An address in C or C++ is the location in memory of an object or function. An address is the contents of a pointer, as opposed to the contents of the memory location pointed to by the pointer.


What is a bit copy?

A bit copy of an object is an exact, bit-by-bit, copy of that object. The default copy constructor generated by the compiler makes a bit copy. This is potentially a problem if the object contains pointers to other objects... A bit copy of a pointer copies the pointer, but not its data. This means that you have two pointers pointing at the same object in memory. If you delete one of them, the other becomes invalid, and this can (usually does) cause corruption. If an object contains a pointer, the object's copy constructor should provide for proper allocation and copying of any pointed to objects within that object.


What is pointer to an object in c plus plus?

A pointer is simply a variable that stores a memory address. Thus a pointer to an object is simply a variable that stores the memory address of an object. Since pointers are variables, they require memory of their own. Pointers may also be constant, which simply means you cannot change what they point to. Pointers can also be dereferenced to provide indirect access to the memory they point to -- hence they are known as pointers. However, unlike C, pointers are not the same as references. In C++, a reference is simply an alias for a memory address and requires no storage of its own.