Difference between dangling pointer and memory leak











up vote
73
down vote

favorite
63












I don't understand the difference between a dangling pointer and a memory leak. How are these two terms related?










share|improve this question




























    up vote
    73
    down vote

    favorite
    63












    I don't understand the difference between a dangling pointer and a memory leak. How are these two terms related?










    share|improve this question


























      up vote
      73
      down vote

      favorite
      63









      up vote
      73
      down vote

      favorite
      63






      63





      I don't understand the difference between a dangling pointer and a memory leak. How are these two terms related?










      share|improve this question















      I don't understand the difference between a dangling pointer and a memory leak. How are these two terms related?







      c






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 30 '12 at 9:45









      Graham Borland

      48.1k16114162




      48.1k16114162










      asked Oct 30 '12 at 4:48









      dead programmer

      1,71262350




      1,71262350
























          8 Answers
          8






          active

          oldest

          votes

















          up vote
          132
          down vote













          A dangling pointer points to memory that has already been freed. The storage is no longer allocated. Trying to access it might cause a Segmentation fault.



          Common way to end up with a dangling pointer:



          char* func()
          {
          char str[10];
          strcpy(str,"Hello!");
          return(str);
          }
          //returned pointer points to str which has gone out of scope.


          You are returning an address which was a local variable, which would have gone out of scope by the time control was returned to the calling function. (Undefined behaviour)



          Another common dangling pointer example is an access of a memory location via pointer, after free has been explicitly called on that memory.



          int *c = malloc(sizeof(int));
          free(c);
          *c = 3; //writing to freed location!




          A memory leak is memory which hasn't been freed, there is no way to access (or free it) now, as there are no ways to get to it anymore. (E.g. a pointer which was the only reference to a memory location dynamically allocated (and not freed) which points somewhere else now.)



          void func(){
          char *ch;
          ch = (char*) malloc(10);
          }
          //ch not valid outside, no way to access malloc-ed memory


          Char-ptr ch is a local variable that goes out of scope at the end of the function, leaking the dynamically allocated 10 bytes.






          share|improve this answer























          • This article might also be helpful stackoverflow.com/questions/127386/…
            – bkausbk
            Jul 19 '13 at 7:43


















          up vote
          21
          down vote













          You can think of these as the opposites of one another.



          When you free an area of memory, but still keep a pointer to it, that pointer is dangling:



          char *c = malloc(16);
          free(c);
          c[1] = 'a'; //invalid access through dangling pointer!


          When you lose the pointer, but keep the memory allocated, you have a memory leak:



          void myfunc()
          {
          char *c = malloc(16);
          } //after myfunc returns, the the memory pointed to by c is not freed: leak!





          share|improve this answer




























            up vote
            13
            down vote













            A dangling pointer is one that has a value (not NULL) which refers to some memory which is not valid for the type of object you expect. For example if you set a pointer to an object then overwrote that memory with something else unrelated or freed the memory if it was dynamically allocated.



            A memory leak is when you dynamically allocate memory from the heap but never free it, possibly because you lost all references to it.



            They are related in that they are both situations relating to mismanaged pointers, especially regarding dynamically allocated memory. In one situation (dangling pointer) you have likely freed the memory but tried to reference it afterwards; in the other (memory leak), you have forgotten to free the memory entirely!






            share|improve this answer




























              up vote
              6
              down vote













              Dangling Pointer



              If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.



              #include<stdio.h>

              int *call();

              void main(){

              int *ptr;
              ptr=call();

              fflush(stdin);
              printf("%d",*ptr);

              }

              int * call(){

              int x=25;
              ++x;
              return &x;
              }


              Output: Garbage value




              Note: In some compiler you may get warning message returning address
              of local variable or temporary




              Explanation: variable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.



              Solution of this problem: Make the variable x is as static variable.
              In other word we can say a pointer whose pointing object has been deleted is called dangling pointer.



              Memory Leak



              In computer science, a memory leak occurs when a computer program incorrectly manages memory allocations.
              As per simple we have allocated the memory and not Free other language term say not release it call memory leak it is fatal to application and unexpected crash.






              share|improve this answer






























                up vote
                3
                down vote













                Pointer helps to create user defined scope to a variable, which is called Dynamic variable. Dynamic Variable can be single variable or group of variable of same type (array) or group of variable of different types (struct). Default local variable scope starts when control enters into a function and ends when control comes out of that function. Default global vairable scope starts at program execution and ends once program finishes.



                But scope of a dynamic variable which holds by a pointer can start and end at any point in a program execution, which has to be decided by a programmer. Dangling and memory leak comes into picture only if a programmer doesnt handle the end of scope.



                Memory leak will occur if a programmer, doesnt write the code (free of pointer) for end of scope for dynamic variables. Any way once program exits complete process memory will be freed, at that time this leaked memory also will get freed. But it will cause a very serious problem for a process which is running long time.



                Once scope of dynamic variable comes to end(freed), NULL should be assigned to pointer variable. Otherwise if the code wrongly accesses it undefined behaviour will happen. So dangling pointer is nothing but a pointer which is pointing a dynamic variable whose scope is already finished.






                share|improve this answer




























                  up vote
                  3
                  down vote













                  Memory leak: When there is a memory area in a heap but no variable in the stack pointing to that memory.



                  char *myarea=(char *)malloc(10);

                  char *newarea=(char *)malloc(10);

                  myarea=newarea;


                  Dangling pointer: When a pointer variable in a stack but no memory in heap.



                  char *p =NULL;


                  A dangling pointer trying to dereference without allocating space will result in a segmentation fault.






                  share|improve this answer



















                  • 3




                    your example for dangling pointer is not really a dangling pointer but a NULL pointer. The correct example would be to dynamically assign memory to pointer, say using malloc(), and then free() that memory, this makes it a dangling pointer. NOTE: after freeing we haven't assigned it to NULL thus pointer still points to same memory address which makes it a dangling pointer. Now, if you try to access that memory using the same pointer (i.e. dereference the pointer) you may end of getting segmentation fault.
                    – sactiw
                    Sep 16 '14 at 5:54








                  • 1




                    @sactiw absolutely right
                    – Prince Vijay Pratap
                    Sep 20 '15 at 0:43


















                  up vote
                  0
                  down vote













                  A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer.



                  #include <stdlib.h>
                  #include <stdio.h>
                  void main()
                  {
                  int *ptr = (int *)malloc(sizeof(int));
                  // After below free call, ptr becomes a
                  // dangling pointer
                  free(ptr);
                  }


                  for more information click HERE






                  share|improve this answer




























                    up vote
                    0
                    down vote














                    A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer.
                    There are three different ways where Pointer acts as dangling pointer.




                    1. De-allocation of memory

                    2. Function Call

                    3. Variable goes out of scope




                    —— from https://www.geeksforgeeks.org/dangling-void-null-wild-pointers/






                    share|improve this answer























                      Your Answer






                      StackExchange.ifUsing("editor", function () {
                      StackExchange.using("externalEditor", function () {
                      StackExchange.using("snippets", function () {
                      StackExchange.snippets.init();
                      });
                      });
                      }, "code-snippets");

                      StackExchange.ready(function() {
                      var channelOptions = {
                      tags: "".split(" "),
                      id: "1"
                      };
                      initTagRenderer("".split(" "), "".split(" "), channelOptions);

                      StackExchange.using("externalEditor", function() {
                      // Have to fire editor after snippets, if snippets enabled
                      if (StackExchange.settings.snippets.snippetsEnabled) {
                      StackExchange.using("snippets", function() {
                      createEditor();
                      });
                      }
                      else {
                      createEditor();
                      }
                      });

                      function createEditor() {
                      StackExchange.prepareEditor({
                      heartbeatType: 'answer',
                      convertImagesToLinks: true,
                      noModals: true,
                      showLowRepImageUploadWarning: true,
                      reputationToPostImages: 10,
                      bindNavPrevention: true,
                      postfix: "",
                      imageUploader: {
                      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                      allowUrls: true
                      },
                      onDemand: true,
                      discardSelector: ".discard-answer"
                      ,immediatelyShowMarkdownHelp:true
                      });


                      }
                      });














                       

                      draft saved


                      draft discarded


















                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13132798%2fdifference-between-dangling-pointer-and-memory-leak%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown

























                      8 Answers
                      8






                      active

                      oldest

                      votes








                      8 Answers
                      8






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes








                      up vote
                      132
                      down vote













                      A dangling pointer points to memory that has already been freed. The storage is no longer allocated. Trying to access it might cause a Segmentation fault.



                      Common way to end up with a dangling pointer:



                      char* func()
                      {
                      char str[10];
                      strcpy(str,"Hello!");
                      return(str);
                      }
                      //returned pointer points to str which has gone out of scope.


                      You are returning an address which was a local variable, which would have gone out of scope by the time control was returned to the calling function. (Undefined behaviour)



                      Another common dangling pointer example is an access of a memory location via pointer, after free has been explicitly called on that memory.



                      int *c = malloc(sizeof(int));
                      free(c);
                      *c = 3; //writing to freed location!




                      A memory leak is memory which hasn't been freed, there is no way to access (or free it) now, as there are no ways to get to it anymore. (E.g. a pointer which was the only reference to a memory location dynamically allocated (and not freed) which points somewhere else now.)



                      void func(){
                      char *ch;
                      ch = (char*) malloc(10);
                      }
                      //ch not valid outside, no way to access malloc-ed memory


                      Char-ptr ch is a local variable that goes out of scope at the end of the function, leaking the dynamically allocated 10 bytes.






                      share|improve this answer























                      • This article might also be helpful stackoverflow.com/questions/127386/…
                        – bkausbk
                        Jul 19 '13 at 7:43















                      up vote
                      132
                      down vote













                      A dangling pointer points to memory that has already been freed. The storage is no longer allocated. Trying to access it might cause a Segmentation fault.



                      Common way to end up with a dangling pointer:



                      char* func()
                      {
                      char str[10];
                      strcpy(str,"Hello!");
                      return(str);
                      }
                      //returned pointer points to str which has gone out of scope.


                      You are returning an address which was a local variable, which would have gone out of scope by the time control was returned to the calling function. (Undefined behaviour)



                      Another common dangling pointer example is an access of a memory location via pointer, after free has been explicitly called on that memory.



                      int *c = malloc(sizeof(int));
                      free(c);
                      *c = 3; //writing to freed location!




                      A memory leak is memory which hasn't been freed, there is no way to access (or free it) now, as there are no ways to get to it anymore. (E.g. a pointer which was the only reference to a memory location dynamically allocated (and not freed) which points somewhere else now.)



                      void func(){
                      char *ch;
                      ch = (char*) malloc(10);
                      }
                      //ch not valid outside, no way to access malloc-ed memory


                      Char-ptr ch is a local variable that goes out of scope at the end of the function, leaking the dynamically allocated 10 bytes.






                      share|improve this answer























                      • This article might also be helpful stackoverflow.com/questions/127386/…
                        – bkausbk
                        Jul 19 '13 at 7:43













                      up vote
                      132
                      down vote










                      up vote
                      132
                      down vote









                      A dangling pointer points to memory that has already been freed. The storage is no longer allocated. Trying to access it might cause a Segmentation fault.



                      Common way to end up with a dangling pointer:



                      char* func()
                      {
                      char str[10];
                      strcpy(str,"Hello!");
                      return(str);
                      }
                      //returned pointer points to str which has gone out of scope.


                      You are returning an address which was a local variable, which would have gone out of scope by the time control was returned to the calling function. (Undefined behaviour)



                      Another common dangling pointer example is an access of a memory location via pointer, after free has been explicitly called on that memory.



                      int *c = malloc(sizeof(int));
                      free(c);
                      *c = 3; //writing to freed location!




                      A memory leak is memory which hasn't been freed, there is no way to access (or free it) now, as there are no ways to get to it anymore. (E.g. a pointer which was the only reference to a memory location dynamically allocated (and not freed) which points somewhere else now.)



                      void func(){
                      char *ch;
                      ch = (char*) malloc(10);
                      }
                      //ch not valid outside, no way to access malloc-ed memory


                      Char-ptr ch is a local variable that goes out of scope at the end of the function, leaking the dynamically allocated 10 bytes.






                      share|improve this answer














                      A dangling pointer points to memory that has already been freed. The storage is no longer allocated. Trying to access it might cause a Segmentation fault.



                      Common way to end up with a dangling pointer:



                      char* func()
                      {
                      char str[10];
                      strcpy(str,"Hello!");
                      return(str);
                      }
                      //returned pointer points to str which has gone out of scope.


                      You are returning an address which was a local variable, which would have gone out of scope by the time control was returned to the calling function. (Undefined behaviour)



                      Another common dangling pointer example is an access of a memory location via pointer, after free has been explicitly called on that memory.



                      int *c = malloc(sizeof(int));
                      free(c);
                      *c = 3; //writing to freed location!




                      A memory leak is memory which hasn't been freed, there is no way to access (or free it) now, as there are no ways to get to it anymore. (E.g. a pointer which was the only reference to a memory location dynamically allocated (and not freed) which points somewhere else now.)



                      void func(){
                      char *ch;
                      ch = (char*) malloc(10);
                      }
                      //ch not valid outside, no way to access malloc-ed memory


                      Char-ptr ch is a local variable that goes out of scope at the end of the function, leaking the dynamically allocated 10 bytes.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 19 '13 at 7:53









                      glglgl

                      64.8k788163




                      64.8k788163










                      answered Oct 30 '12 at 4:49









                      Anirudh Ramanathan

                      38.4k1599152




                      38.4k1599152












                      • This article might also be helpful stackoverflow.com/questions/127386/…
                        – bkausbk
                        Jul 19 '13 at 7:43


















                      • This article might also be helpful stackoverflow.com/questions/127386/…
                        – bkausbk
                        Jul 19 '13 at 7:43
















                      This article might also be helpful stackoverflow.com/questions/127386/…
                      – bkausbk
                      Jul 19 '13 at 7:43




                      This article might also be helpful stackoverflow.com/questions/127386/…
                      – bkausbk
                      Jul 19 '13 at 7:43












                      up vote
                      21
                      down vote













                      You can think of these as the opposites of one another.



                      When you free an area of memory, but still keep a pointer to it, that pointer is dangling:



                      char *c = malloc(16);
                      free(c);
                      c[1] = 'a'; //invalid access through dangling pointer!


                      When you lose the pointer, but keep the memory allocated, you have a memory leak:



                      void myfunc()
                      {
                      char *c = malloc(16);
                      } //after myfunc returns, the the memory pointed to by c is not freed: leak!





                      share|improve this answer

























                        up vote
                        21
                        down vote













                        You can think of these as the opposites of one another.



                        When you free an area of memory, but still keep a pointer to it, that pointer is dangling:



                        char *c = malloc(16);
                        free(c);
                        c[1] = 'a'; //invalid access through dangling pointer!


                        When you lose the pointer, but keep the memory allocated, you have a memory leak:



                        void myfunc()
                        {
                        char *c = malloc(16);
                        } //after myfunc returns, the the memory pointed to by c is not freed: leak!





                        share|improve this answer























                          up vote
                          21
                          down vote










                          up vote
                          21
                          down vote









                          You can think of these as the opposites of one another.



                          When you free an area of memory, but still keep a pointer to it, that pointer is dangling:



                          char *c = malloc(16);
                          free(c);
                          c[1] = 'a'; //invalid access through dangling pointer!


                          When you lose the pointer, but keep the memory allocated, you have a memory leak:



                          void myfunc()
                          {
                          char *c = malloc(16);
                          } //after myfunc returns, the the memory pointed to by c is not freed: leak!





                          share|improve this answer












                          You can think of these as the opposites of one another.



                          When you free an area of memory, but still keep a pointer to it, that pointer is dangling:



                          char *c = malloc(16);
                          free(c);
                          c[1] = 'a'; //invalid access through dangling pointer!


                          When you lose the pointer, but keep the memory allocated, you have a memory leak:



                          void myfunc()
                          {
                          char *c = malloc(16);
                          } //after myfunc returns, the the memory pointed to by c is not freed: leak!






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Oct 30 '12 at 4:54









                          Greg Inozemtsev

                          3,7921423




                          3,7921423






















                              up vote
                              13
                              down vote













                              A dangling pointer is one that has a value (not NULL) which refers to some memory which is not valid for the type of object you expect. For example if you set a pointer to an object then overwrote that memory with something else unrelated or freed the memory if it was dynamically allocated.



                              A memory leak is when you dynamically allocate memory from the heap but never free it, possibly because you lost all references to it.



                              They are related in that they are both situations relating to mismanaged pointers, especially regarding dynamically allocated memory. In one situation (dangling pointer) you have likely freed the memory but tried to reference it afterwards; in the other (memory leak), you have forgotten to free the memory entirely!






                              share|improve this answer

























                                up vote
                                13
                                down vote













                                A dangling pointer is one that has a value (not NULL) which refers to some memory which is not valid for the type of object you expect. For example if you set a pointer to an object then overwrote that memory with something else unrelated or freed the memory if it was dynamically allocated.



                                A memory leak is when you dynamically allocate memory from the heap but never free it, possibly because you lost all references to it.



                                They are related in that they are both situations relating to mismanaged pointers, especially regarding dynamically allocated memory. In one situation (dangling pointer) you have likely freed the memory but tried to reference it afterwards; in the other (memory leak), you have forgotten to free the memory entirely!






                                share|improve this answer























                                  up vote
                                  13
                                  down vote










                                  up vote
                                  13
                                  down vote









                                  A dangling pointer is one that has a value (not NULL) which refers to some memory which is not valid for the type of object you expect. For example if you set a pointer to an object then overwrote that memory with something else unrelated or freed the memory if it was dynamically allocated.



                                  A memory leak is when you dynamically allocate memory from the heap but never free it, possibly because you lost all references to it.



                                  They are related in that they are both situations relating to mismanaged pointers, especially regarding dynamically allocated memory. In one situation (dangling pointer) you have likely freed the memory but tried to reference it afterwards; in the other (memory leak), you have forgotten to free the memory entirely!






                                  share|improve this answer












                                  A dangling pointer is one that has a value (not NULL) which refers to some memory which is not valid for the type of object you expect. For example if you set a pointer to an object then overwrote that memory with something else unrelated or freed the memory if it was dynamically allocated.



                                  A memory leak is when you dynamically allocate memory from the heap but never free it, possibly because you lost all references to it.



                                  They are related in that they are both situations relating to mismanaged pointers, especially regarding dynamically allocated memory. In one situation (dangling pointer) you have likely freed the memory but tried to reference it afterwards; in the other (memory leak), you have forgotten to free the memory entirely!







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Oct 30 '12 at 4:50









                                  maerics

                                  102k28198246




                                  102k28198246






















                                      up vote
                                      6
                                      down vote













                                      Dangling Pointer



                                      If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.



                                      #include<stdio.h>

                                      int *call();

                                      void main(){

                                      int *ptr;
                                      ptr=call();

                                      fflush(stdin);
                                      printf("%d",*ptr);

                                      }

                                      int * call(){

                                      int x=25;
                                      ++x;
                                      return &x;
                                      }


                                      Output: Garbage value




                                      Note: In some compiler you may get warning message returning address
                                      of local variable or temporary




                                      Explanation: variable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.



                                      Solution of this problem: Make the variable x is as static variable.
                                      In other word we can say a pointer whose pointing object has been deleted is called dangling pointer.



                                      Memory Leak



                                      In computer science, a memory leak occurs when a computer program incorrectly manages memory allocations.
                                      As per simple we have allocated the memory and not Free other language term say not release it call memory leak it is fatal to application and unexpected crash.






                                      share|improve this answer



























                                        up vote
                                        6
                                        down vote













                                        Dangling Pointer



                                        If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.



                                        #include<stdio.h>

                                        int *call();

                                        void main(){

                                        int *ptr;
                                        ptr=call();

                                        fflush(stdin);
                                        printf("%d",*ptr);

                                        }

                                        int * call(){

                                        int x=25;
                                        ++x;
                                        return &x;
                                        }


                                        Output: Garbage value




                                        Note: In some compiler you may get warning message returning address
                                        of local variable or temporary




                                        Explanation: variable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.



                                        Solution of this problem: Make the variable x is as static variable.
                                        In other word we can say a pointer whose pointing object has been deleted is called dangling pointer.



                                        Memory Leak



                                        In computer science, a memory leak occurs when a computer program incorrectly manages memory allocations.
                                        As per simple we have allocated the memory and not Free other language term say not release it call memory leak it is fatal to application and unexpected crash.






                                        share|improve this answer

























                                          up vote
                                          6
                                          down vote










                                          up vote
                                          6
                                          down vote









                                          Dangling Pointer



                                          If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.



                                          #include<stdio.h>

                                          int *call();

                                          void main(){

                                          int *ptr;
                                          ptr=call();

                                          fflush(stdin);
                                          printf("%d",*ptr);

                                          }

                                          int * call(){

                                          int x=25;
                                          ++x;
                                          return &x;
                                          }


                                          Output: Garbage value




                                          Note: In some compiler you may get warning message returning address
                                          of local variable or temporary




                                          Explanation: variable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.



                                          Solution of this problem: Make the variable x is as static variable.
                                          In other word we can say a pointer whose pointing object has been deleted is called dangling pointer.



                                          Memory Leak



                                          In computer science, a memory leak occurs when a computer program incorrectly manages memory allocations.
                                          As per simple we have allocated the memory and not Free other language term say not release it call memory leak it is fatal to application and unexpected crash.






                                          share|improve this answer














                                          Dangling Pointer



                                          If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.



                                          #include<stdio.h>

                                          int *call();

                                          void main(){

                                          int *ptr;
                                          ptr=call();

                                          fflush(stdin);
                                          printf("%d",*ptr);

                                          }

                                          int * call(){

                                          int x=25;
                                          ++x;
                                          return &x;
                                          }


                                          Output: Garbage value




                                          Note: In some compiler you may get warning message returning address
                                          of local variable or temporary




                                          Explanation: variable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.



                                          Solution of this problem: Make the variable x is as static variable.
                                          In other word we can say a pointer whose pointing object has been deleted is called dangling pointer.



                                          Memory Leak



                                          In computer science, a memory leak occurs when a computer program incorrectly manages memory allocations.
                                          As per simple we have allocated the memory and not Free other language term say not release it call memory leak it is fatal to application and unexpected crash.







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Mar 31 '16 at 8:12









                                          byJeevan

                                          2,33212144




                                          2,33212144










                                          answered Jul 19 '13 at 6:32









                                          PeterParker

                                          30548




                                          30548






















                                              up vote
                                              3
                                              down vote













                                              Pointer helps to create user defined scope to a variable, which is called Dynamic variable. Dynamic Variable can be single variable or group of variable of same type (array) or group of variable of different types (struct). Default local variable scope starts when control enters into a function and ends when control comes out of that function. Default global vairable scope starts at program execution and ends once program finishes.



                                              But scope of a dynamic variable which holds by a pointer can start and end at any point in a program execution, which has to be decided by a programmer. Dangling and memory leak comes into picture only if a programmer doesnt handle the end of scope.



                                              Memory leak will occur if a programmer, doesnt write the code (free of pointer) for end of scope for dynamic variables. Any way once program exits complete process memory will be freed, at that time this leaked memory also will get freed. But it will cause a very serious problem for a process which is running long time.



                                              Once scope of dynamic variable comes to end(freed), NULL should be assigned to pointer variable. Otherwise if the code wrongly accesses it undefined behaviour will happen. So dangling pointer is nothing but a pointer which is pointing a dynamic variable whose scope is already finished.






                                              share|improve this answer

























                                                up vote
                                                3
                                                down vote













                                                Pointer helps to create user defined scope to a variable, which is called Dynamic variable. Dynamic Variable can be single variable or group of variable of same type (array) or group of variable of different types (struct). Default local variable scope starts when control enters into a function and ends when control comes out of that function. Default global vairable scope starts at program execution and ends once program finishes.



                                                But scope of a dynamic variable which holds by a pointer can start and end at any point in a program execution, which has to be decided by a programmer. Dangling and memory leak comes into picture only if a programmer doesnt handle the end of scope.



                                                Memory leak will occur if a programmer, doesnt write the code (free of pointer) for end of scope for dynamic variables. Any way once program exits complete process memory will be freed, at that time this leaked memory also will get freed. But it will cause a very serious problem for a process which is running long time.



                                                Once scope of dynamic variable comes to end(freed), NULL should be assigned to pointer variable. Otherwise if the code wrongly accesses it undefined behaviour will happen. So dangling pointer is nothing but a pointer which is pointing a dynamic variable whose scope is already finished.






                                                share|improve this answer























                                                  up vote
                                                  3
                                                  down vote










                                                  up vote
                                                  3
                                                  down vote









                                                  Pointer helps to create user defined scope to a variable, which is called Dynamic variable. Dynamic Variable can be single variable or group of variable of same type (array) or group of variable of different types (struct). Default local variable scope starts when control enters into a function and ends when control comes out of that function. Default global vairable scope starts at program execution and ends once program finishes.



                                                  But scope of a dynamic variable which holds by a pointer can start and end at any point in a program execution, which has to be decided by a programmer. Dangling and memory leak comes into picture only if a programmer doesnt handle the end of scope.



                                                  Memory leak will occur if a programmer, doesnt write the code (free of pointer) for end of scope for dynamic variables. Any way once program exits complete process memory will be freed, at that time this leaked memory also will get freed. But it will cause a very serious problem for a process which is running long time.



                                                  Once scope of dynamic variable comes to end(freed), NULL should be assigned to pointer variable. Otherwise if the code wrongly accesses it undefined behaviour will happen. So dangling pointer is nothing but a pointer which is pointing a dynamic variable whose scope is already finished.






                                                  share|improve this answer












                                                  Pointer helps to create user defined scope to a variable, which is called Dynamic variable. Dynamic Variable can be single variable or group of variable of same type (array) or group of variable of different types (struct). Default local variable scope starts when control enters into a function and ends when control comes out of that function. Default global vairable scope starts at program execution and ends once program finishes.



                                                  But scope of a dynamic variable which holds by a pointer can start and end at any point in a program execution, which has to be decided by a programmer. Dangling and memory leak comes into picture only if a programmer doesnt handle the end of scope.



                                                  Memory leak will occur if a programmer, doesnt write the code (free of pointer) for end of scope for dynamic variables. Any way once program exits complete process memory will be freed, at that time this leaked memory also will get freed. But it will cause a very serious problem for a process which is running long time.



                                                  Once scope of dynamic variable comes to end(freed), NULL should be assigned to pointer variable. Otherwise if the code wrongly accesses it undefined behaviour will happen. So dangling pointer is nothing but a pointer which is pointing a dynamic variable whose scope is already finished.







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Jul 19 '13 at 6:56









                                                  rashok

                                                  5,957115475




                                                  5,957115475






















                                                      up vote
                                                      3
                                                      down vote













                                                      Memory leak: When there is a memory area in a heap but no variable in the stack pointing to that memory.



                                                      char *myarea=(char *)malloc(10);

                                                      char *newarea=(char *)malloc(10);

                                                      myarea=newarea;


                                                      Dangling pointer: When a pointer variable in a stack but no memory in heap.



                                                      char *p =NULL;


                                                      A dangling pointer trying to dereference without allocating space will result in a segmentation fault.






                                                      share|improve this answer



















                                                      • 3




                                                        your example for dangling pointer is not really a dangling pointer but a NULL pointer. The correct example would be to dynamically assign memory to pointer, say using malloc(), and then free() that memory, this makes it a dangling pointer. NOTE: after freeing we haven't assigned it to NULL thus pointer still points to same memory address which makes it a dangling pointer. Now, if you try to access that memory using the same pointer (i.e. dereference the pointer) you may end of getting segmentation fault.
                                                        – sactiw
                                                        Sep 16 '14 at 5:54








                                                      • 1




                                                        @sactiw absolutely right
                                                        – Prince Vijay Pratap
                                                        Sep 20 '15 at 0:43















                                                      up vote
                                                      3
                                                      down vote













                                                      Memory leak: When there is a memory area in a heap but no variable in the stack pointing to that memory.



                                                      char *myarea=(char *)malloc(10);

                                                      char *newarea=(char *)malloc(10);

                                                      myarea=newarea;


                                                      Dangling pointer: When a pointer variable in a stack but no memory in heap.



                                                      char *p =NULL;


                                                      A dangling pointer trying to dereference without allocating space will result in a segmentation fault.






                                                      share|improve this answer



















                                                      • 3




                                                        your example for dangling pointer is not really a dangling pointer but a NULL pointer. The correct example would be to dynamically assign memory to pointer, say using malloc(), and then free() that memory, this makes it a dangling pointer. NOTE: after freeing we haven't assigned it to NULL thus pointer still points to same memory address which makes it a dangling pointer. Now, if you try to access that memory using the same pointer (i.e. dereference the pointer) you may end of getting segmentation fault.
                                                        – sactiw
                                                        Sep 16 '14 at 5:54








                                                      • 1




                                                        @sactiw absolutely right
                                                        – Prince Vijay Pratap
                                                        Sep 20 '15 at 0:43













                                                      up vote
                                                      3
                                                      down vote










                                                      up vote
                                                      3
                                                      down vote









                                                      Memory leak: When there is a memory area in a heap but no variable in the stack pointing to that memory.



                                                      char *myarea=(char *)malloc(10);

                                                      char *newarea=(char *)malloc(10);

                                                      myarea=newarea;


                                                      Dangling pointer: When a pointer variable in a stack but no memory in heap.



                                                      char *p =NULL;


                                                      A dangling pointer trying to dereference without allocating space will result in a segmentation fault.






                                                      share|improve this answer














                                                      Memory leak: When there is a memory area in a heap but no variable in the stack pointing to that memory.



                                                      char *myarea=(char *)malloc(10);

                                                      char *newarea=(char *)malloc(10);

                                                      myarea=newarea;


                                                      Dangling pointer: When a pointer variable in a stack but no memory in heap.



                                                      char *p =NULL;


                                                      A dangling pointer trying to dereference without allocating space will result in a segmentation fault.







                                                      share|improve this answer














                                                      share|improve this answer



                                                      share|improve this answer








                                                      edited Jul 28 '14 at 19:12









                                                      drs

                                                      2,66422449




                                                      2,66422449










                                                      answered Jul 28 '14 at 19:06









                                                      user2264571

                                                      5511




                                                      5511








                                                      • 3




                                                        your example for dangling pointer is not really a dangling pointer but a NULL pointer. The correct example would be to dynamically assign memory to pointer, say using malloc(), and then free() that memory, this makes it a dangling pointer. NOTE: after freeing we haven't assigned it to NULL thus pointer still points to same memory address which makes it a dangling pointer. Now, if you try to access that memory using the same pointer (i.e. dereference the pointer) you may end of getting segmentation fault.
                                                        – sactiw
                                                        Sep 16 '14 at 5:54








                                                      • 1




                                                        @sactiw absolutely right
                                                        – Prince Vijay Pratap
                                                        Sep 20 '15 at 0:43














                                                      • 3




                                                        your example for dangling pointer is not really a dangling pointer but a NULL pointer. The correct example would be to dynamically assign memory to pointer, say using malloc(), and then free() that memory, this makes it a dangling pointer. NOTE: after freeing we haven't assigned it to NULL thus pointer still points to same memory address which makes it a dangling pointer. Now, if you try to access that memory using the same pointer (i.e. dereference the pointer) you may end of getting segmentation fault.
                                                        – sactiw
                                                        Sep 16 '14 at 5:54








                                                      • 1




                                                        @sactiw absolutely right
                                                        – Prince Vijay Pratap
                                                        Sep 20 '15 at 0:43








                                                      3




                                                      3




                                                      your example for dangling pointer is not really a dangling pointer but a NULL pointer. The correct example would be to dynamically assign memory to pointer, say using malloc(), and then free() that memory, this makes it a dangling pointer. NOTE: after freeing we haven't assigned it to NULL thus pointer still points to same memory address which makes it a dangling pointer. Now, if you try to access that memory using the same pointer (i.e. dereference the pointer) you may end of getting segmentation fault.
                                                      – sactiw
                                                      Sep 16 '14 at 5:54






                                                      your example for dangling pointer is not really a dangling pointer but a NULL pointer. The correct example would be to dynamically assign memory to pointer, say using malloc(), and then free() that memory, this makes it a dangling pointer. NOTE: after freeing we haven't assigned it to NULL thus pointer still points to same memory address which makes it a dangling pointer. Now, if you try to access that memory using the same pointer (i.e. dereference the pointer) you may end of getting segmentation fault.
                                                      – sactiw
                                                      Sep 16 '14 at 5:54






                                                      1




                                                      1




                                                      @sactiw absolutely right
                                                      – Prince Vijay Pratap
                                                      Sep 20 '15 at 0:43




                                                      @sactiw absolutely right
                                                      – Prince Vijay Pratap
                                                      Sep 20 '15 at 0:43










                                                      up vote
                                                      0
                                                      down vote













                                                      A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer.



                                                      #include <stdlib.h>
                                                      #include <stdio.h>
                                                      void main()
                                                      {
                                                      int *ptr = (int *)malloc(sizeof(int));
                                                      // After below free call, ptr becomes a
                                                      // dangling pointer
                                                      free(ptr);
                                                      }


                                                      for more information click HERE






                                                      share|improve this answer

























                                                        up vote
                                                        0
                                                        down vote













                                                        A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer.



                                                        #include <stdlib.h>
                                                        #include <stdio.h>
                                                        void main()
                                                        {
                                                        int *ptr = (int *)malloc(sizeof(int));
                                                        // After below free call, ptr becomes a
                                                        // dangling pointer
                                                        free(ptr);
                                                        }


                                                        for more information click HERE






                                                        share|improve this answer























                                                          up vote
                                                          0
                                                          down vote










                                                          up vote
                                                          0
                                                          down vote









                                                          A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer.



                                                          #include <stdlib.h>
                                                          #include <stdio.h>
                                                          void main()
                                                          {
                                                          int *ptr = (int *)malloc(sizeof(int));
                                                          // After below free call, ptr becomes a
                                                          // dangling pointer
                                                          free(ptr);
                                                          }


                                                          for more information click HERE






                                                          share|improve this answer












                                                          A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer.



                                                          #include <stdlib.h>
                                                          #include <stdio.h>
                                                          void main()
                                                          {
                                                          int *ptr = (int *)malloc(sizeof(int));
                                                          // After below free call, ptr becomes a
                                                          // dangling pointer
                                                          free(ptr);
                                                          }


                                                          for more information click HERE







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Jul 3 at 13:59









                                                          Niravdas

                                                          566




                                                          566






















                                                              up vote
                                                              0
                                                              down vote














                                                              A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer.
                                                              There are three different ways where Pointer acts as dangling pointer.




                                                              1. De-allocation of memory

                                                              2. Function Call

                                                              3. Variable goes out of scope




                                                              —— from https://www.geeksforgeeks.org/dangling-void-null-wild-pointers/






                                                              share|improve this answer



























                                                                up vote
                                                                0
                                                                down vote














                                                                A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer.
                                                                There are three different ways where Pointer acts as dangling pointer.




                                                                1. De-allocation of memory

                                                                2. Function Call

                                                                3. Variable goes out of scope




                                                                —— from https://www.geeksforgeeks.org/dangling-void-null-wild-pointers/






                                                                share|improve this answer

























                                                                  up vote
                                                                  0
                                                                  down vote










                                                                  up vote
                                                                  0
                                                                  down vote










                                                                  A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer.
                                                                  There are three different ways where Pointer acts as dangling pointer.




                                                                  1. De-allocation of memory

                                                                  2. Function Call

                                                                  3. Variable goes out of scope




                                                                  —— from https://www.geeksforgeeks.org/dangling-void-null-wild-pointers/






                                                                  share|improve this answer















                                                                  A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer.
                                                                  There are three different ways where Pointer acts as dangling pointer.




                                                                  1. De-allocation of memory

                                                                  2. Function Call

                                                                  3. Variable goes out of scope




                                                                  —— from https://www.geeksforgeeks.org/dangling-void-null-wild-pointers/







                                                                  share|improve this answer














                                                                  share|improve this answer



                                                                  share|improve this answer








                                                                  edited 2 hours ago









                                                                  Pang

                                                                  6,8001563101




                                                                  6,8001563101










                                                                  answered Aug 2 at 11:30









                                                                  Lalit

                                                                  1




                                                                  1






























                                                                       

                                                                      draft saved


                                                                      draft discarded



















































                                                                       


                                                                      draft saved


                                                                      draft discarded














                                                                      StackExchange.ready(
                                                                      function () {
                                                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13132798%2fdifference-between-dangling-pointer-and-memory-leak%23new-answer', 'question_page');
                                                                      }
                                                                      );

                                                                      Post as a guest















                                                                      Required, but never shown





















































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown

































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown







                                                                      Popular posts from this blog

                                                                      Feedback on college project

                                                                      Futebolista

                                                                      Albești (Vaslui)