dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: deletion of new pointer

  1. #1
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    deletion of new pointer

    I have created a new pointer in free store but i did not delete it will memory given to it will not destroyed.

  2. #2
    Join Date
    Aug 2009
    Posts
    440

    Re: deletion of new pointer

    Um...is there a question? I will just say that every new must be paired with a corresponding delete, otherwise you have a memory leak.

  3. #3
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Post Re: deletion of new pointer

    Quote Originally Posted by Alterah View Post
    Um...is there a question? I will just say that every new must be paired with a corresponding delete, otherwise you have a memory leak.
    consider this simple code

    #include<iostream>
    using namespace std;
    int main()
    {
    int *pointer=new int[1000];
    for(int i=0;i<1000;i++)
    pointer[i]=i;
    cout<<"values in specifed area\n";
    for(int i=0;i<1000;i++)
    cout<<"pointer["<<i<<"] "<<pointer[i]<<endl;
    delete [] pointer;
    return 0;
    }

    what if i not call delete in third last line definitely it will cause memory leak.
    Now my program ends is memory given to int*pointer =new int[100]; will be destroyed like ordinary variable or not
    This is my question i can't explain it better than this.
    Last edited by vkash; May 23rd, 2011 at 06:41 AM. Reason: change delete pointer; to delete [] pointer;

  4. #4
    Join Date
    Aug 2009
    Posts
    440

    Re: deletion of new pointer

    As soon as the program ends, the operating system will do cleanup; however, in the meantime, you have a memory leak in this case. This is because for arrays you need to use delete[].

  5. #5
    Join Date
    Aug 2008
    Posts
    902

    Re: deletion of new pointer

    This is my question i can't explain it better than this.
    Well, you never once ended a sentence with a question mark, so you came off as making a declarative statement, not as asking something.

    See the difference?

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: deletion of new pointer

    Quote Originally Posted by vkash View Post
    consider this simple code
    Code:
    	int *pointer=new int[1000];
            //...
    	delete pointer;
    Wrong form of delete.
    Code:
    delete [] pointer;
    If you use new[], you use delete[], not delete.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Re: deletion of new pointer

    Quote Originally Posted by Paul McKenzie View Post
    Wrong form of delete.
    Code:
    delete [] pointer;
    If you use new[], you use delete[], not delete.

    Regards,

    Paul McKenzie
    sorry for that mistake i know that but accidently i did that
    TELL ME WHETHER THE MEMORY GIVEN TO POINTER WILL BE DELETED OR NOT ( when program ends)?

  8. #8
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: deletion of new pointer

    As soon as the application ends, the entire space the application used is cleaned. So yes, your memoryleaks are cleaned, but you should really manage your memory yourself. Memoryleaks are simply bad coding.

  9. #9
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Resolved Re: deletion of new pointer

    Quote Originally Posted by Skizmo View Post
    As soon as the application ends, the entire space the application used is cleaned. So yes, your memoryleaks are cleaned, but you should really manage your memory yourself. Memoryleaks are simply bad coding.
    Thanks dude i was fearing that is the memory leak occurring in my general study programs cause slow down of my computers performance. Now i am sure that this is not reason of computer slow down

  10. #10
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: deletion of new pointer

    Quote Originally Posted by vkash View Post
    Thanks dude i was fearing that is the memory leak occurring in my general study programs cause slow down of my computers performance. Now i am sure that this is not reason of computer slow down
    Actually, it could. If your programs hogs a lot of memory because it is leaking all over and you don't have a lot of RAM in your computer, then other programs could be paged to virtual memory. Afterwards, your computer might seem to be slower.

    That doesn't really change anything, though. If your program has memory leaks, then it is not correct and you should fix it. Simple as that.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: deletion of new pointer

    Quote Originally Posted by vkash View Post
    Thanks dude i was fearing that is the memory leak...
    What if the slowdown is caused by a virus? That should put more fear into you than a simple memory leak.

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Re: deletion of new pointer

    Quote Originally Posted by D_Drmmr View Post
    Actually, it could. If your programs hogs a lot of memory because it is leaking all over and you don't have a lot of RAM in your computer, then other programs could be paged to virtual memory. Afterwards, your computer might seem to be slower.

    That doesn't really change anything, though. If your program has memory leaks, then it is not correct and you should fix it. Simple as that.
    OK OK
    I think a system restart will solve this problem am i correct?
    If it is not then i am in trouble.

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: deletion of new pointer

    Quote Originally Posted by vkash View Post
    OK OK
    I think a system restart will solve this problem am i correct?
    If it is not then i am in trouble.
    A system restart won't do anything with regards to memory leaks (unless those leaks are in the system software, but that's not your problem).

    If there is paging going on, it should manifest only as a temporary slowdown until the programs in use are returned to main memory.

  14. #14
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Re: deletion of new pointer

    Quote Originally Posted by Lindley View Post
    A system restart won't do anything with regards to memory leaks (unless those leaks are in the system software, but that's not your problem).

    If there is paging going on, it should manifest only as a temporary slowdown until the programs in use are returned to main memory.
    After all answer for this question is mixed one i have als posted this on yahoo answer there people say
    http://answers.yahoo.com/question/in...2183145AArNKAv
    here some guy say that it is possible that they remain in ram still after shutdowns and restart and end of program but some say all end when function return But in case of my simple educational programs there is nothing to worry.
    I will delete new pointer in all my future programs.
    Thanks to all of you for replying answers for their response

  15. #15
    Join Date
    Jun 2008
    Posts
    592

    Re: deletion of new pointer

    Quote Originally Posted by vkash
    here some guy say that it is possible that they remain in ram still after shutdowns and restart and end of program
    I believe you are not understanding what they are saying.

    I am assuming you're using protected mode. This is where paging to your hard drive can occur.

    Once your program terminates, you will no longer have a memory leak since the os will relinquish memory used by that program.

    Now if your program leaks memory a lot or simply uses a lot of memory, it will cause your memory usage to increase and will start to page things to your hard drive . Of course paging depends on what os you use.

    So memory leaks and heavy memory programs are bad for other programs that need memory too.
    Last edited by Joeman; May 23rd, 2011 at 01:48 PM.
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)