C Programming
C++ Programming

What is a dangling pointer in C and C plus plus?


Top Answer
User Avatar
Wiki User
2012-06-10 15:53:09
2012-06-10 15:53:09

A dangling pointer is one that points to a memory location but the memory itself has been freed or released back to the system. The memory may still contain valid information, but the system can overwrite the data at any time so any attempt to access that memory via the dangling pointer could prove disastrous. As soon as memory is released, the pointer is invalid -- because the memory it points to is potentially invalid.

To prevent this, always nullify pointers (set them to point at memory address zero) when they are no longer required, immediately after releasing the memory they point to. There are occasion when this is not necessary, such as when releasing a member pointer in a class destructor, but if a pointer is re-used, it must be initialised before being accessed again.

User Avatar

Related Questions

dangling pointer is a pointer

Whenever memory that was in use, and was referred to by a pointer variable, is freed, and the pointer variable is not updated accordingly (setting it to NULL, for example), the pointer variable is considerred to be a dangling pointer reference.

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.

The pointer that points to a block of memory that does not exist is called a dazzling pointer or wild pointer

generic pointer is a pointer that can not be dereferenced i.e one type pointer can not be assigned to another type.

Yes, C++ supports function pointers.

a pointer that is not pointing to anything

Address of the current object.

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.

A pointer is an object that contains the address of another object, hence the term pointer, meaning that one object points to another.

http://rapidshare.com/files/154681843/C_Basic_Books_Must_download_it.rar These books contain the basics of C language with general answers , must read it !

The function ftell returns the position of the file pointer for a file.

A pointer is a variable that holds an address in memory. int * pPointer = new int; This code creates a pointer to an integer, and assigns it an address of an integer object on the heap.

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.

The this operator in C++ is a compiler generated pointer to the instance of an object, accessible from within a method of that object.

A double pointer is a pointer which points to another pointer which points to an object.For example:Foobar** foobar = NULL;This means that somewhere in memory, the class foobar has been created. The variable 'foobar' now points to a pointer to that memory. However, in this example, the pointer is created as 0.

A pointer in C++ is the same as a pointer in C -- it is a variable that is used to store a memory address and which allows indirect access to that memory address. When a pointer is not in use, it must be zeroed or nullified by assigning the NULL value, thus preventing indirect access to invalid memory.

You cannot point at a class. Pointers are defined by the class of object they can point at. An object is an instance of a class and the pointer points at the object, not the class.

An iterator is a type of pointer that is used to iterate or traverse an array or list of objects.

Yes. All string variables are pointers as are other arrays.

You access memory with a pointer or a reference to the memory. To allocate memory dynamically, use calloc or malloc (C or C++) or new (C++ only).

There are 7 C++ operators that connot be overloaded. They are... . select .* pointer select :: scope :> base ?: conditional # preprocessor ## preprocessor

An asterisk in C++, such as int *data, is what's known as a pointer. A pointer is like a regular variable, but instead of holding a value, a pointer holds the memory location of the value. It's a somewhat difficult concept, and you can learn more about it here: See related links section below...

Example: void foo( MyClass& object ){} // function with call by reference signature MyClass* p = new MyClass(); // instantiate a pointer to MyClass foo( *p ); // call by reference using the pointer

Copyright ยฉ 2020 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.