Puntero colgante
Un puntero que apunta a una ubicación de memoria que se ha eliminado (o liberado) se denomina puntero colgante. Hay tres formas diferentes en las que el puntero actúa como puntero colgante
C++
// Deallocating a memory pointed by ptr causes // dangling pointer #include <cstdlib> #include <iostream> int main() { int* ptr = (int *)malloc(sizeof(int)); // After below free call, ptr becomes a // dangling pointer free(ptr); // No more a dangling pointer ptr = NULL; } // This code is contributed by sarajadhav12052009
C
// Deallocating a memory pointed by ptr causes // dangling pointer #include <stdlib.h> #include <stdio.h> int main() { int *ptr = (int *)malloc(sizeof(int)); // After below free call, ptr becomes a // dangling pointer free(ptr); // No more a dangling pointer ptr = NULL; }
C++
// The pointer pointing to local variable becomes // dangling when local variable is not static. #include <iostream> int* fun() { // x is local variable and goes out of // scope after an execution of fun() is // over. int x = 5; return &x; } // Driver Code int main() { int *p = fun(); fflush(stdin); // p points to something which is not // valid anymore std::cout << *p; return 0; } // This code is contributed by sarajadhav12052009
C
// The pointer pointing to local variable becomes // dangling when local variable is not static. #include<stdio.h> int *fun() { // x is local variable and goes out of // scope after an execution of fun() is // over. int x = 5; return &x; } // Driver Code int main() { int *p = fun(); fflush(stdin); // p points to something which is not // valid anymore printf("%d", *p); return 0; }
C++
// The pointer pointing to local variable doesn't // become dangling when local variable is static. #include <iostream> using namespace std; int *fun() { // x now has scope throughout the program static int x = 5; return &x; } int main() { int *p = fun(); fflush(stdin); // Not a dangling pointer as it points // to static variable. cout << *p; return 0; } // This code is contributed by sarajadhav12052009
C
// The pointer pointing to local variable doesn't // become dangling when local variable is static. #include <stdio.h> int *fun() { // x now has scope throughout the program static int x = 5; return &x; } int main() { int *p = fun(); fflush(stdin); // Not a dangling pointer as it points // to static variable. printf("%d",*p); }
CPP
#include<stdlib.h> int main() { int x = 4; float y = 5.5; //A void pointer void *ptr; ptr = &x; // (int*)ptr - does type casting of void // *((int*)ptr) dereferences the typecasted // void pointer variable. printf("Integer variable is = %d", *( (int*) ptr) ); // void pointer is now float ptr = &y; printf("\nFloat variable is= %f", *( (float*) ptr) ); return 0; }
C++
#include <iostream> using namespace std; int main() { // Null Pointer int *ptr = NULL; cout << "The value of ptr is " << ptr; return 0; } // This code is contributed by sarajadhav12052009
C
#include <stdio.h> int main() { // Null Pointer int *ptr = NULL; printf("The value of ptr is %p", ptr); return 0; }
CPP
int main() { int *p; /* wild pointer */ int x = 10; // p is not a wild pointer now p = &x; return 0; }
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA