Exforsys
+ Reply to Thread
Results 1 to 5 of 5

Memory leaks in C program

This is a discussion on Memory leaks in C program within the C and C++ forums, part of the Programming Talk category; In my C program I use both "new" and "malloc" keywords, I want to know about memory leaks, What are ...

  1. #1
    Charith is offline Junior Member Array
    Join Date
    Jan 2006
    Answers
    11

    Memory leaks in C program

    In my C program I use both "new" and "malloc" keywords, I want to know about memory leaks, What are the consideration when I use "new" and "malloc" keywords. Should I take any extra care to fix memory leaks? Please help me learn


    Charith


  2. #2
    admin's Avatar
    admin is offline Administrator Array
    Join Date
    Mar 2005
    Location
    New York, USA
    Answers
    58
    There are various scenarios in which memory leaks can occur and taken care of in C program. I have specified some of them below:

    Memory leaks can occur during a realloc. That is suppose if a programmer uses the same pointer as the pointer used in original memory allocation for the realloc pointer and if suppose the realloc fails then the pointer would be set to NULL which would make the previously allocated memory also having no pointer for it. This is one of the situations of memory leak. To handle this situation what one can do is use a temporary pointer for realloc so that even if realloc fails, it would not cause big hazard as the pointer on the original memory space is kept as such.

    Since any memory space that loses its pointer or which is not free'd is called as memory leak it is always good to use free and the syntax of using this is

    void free(void *p);

    In the above free is a keyword which takes the pointer for freeing as argument.


  3. #3
    Charith is offline Junior Member Array
    Join Date
    Jan 2006
    Answers
    11
    Hello Admin

    Thanks a lot for posting answer to my question
    I am very happy

    Thanks again
    Charith


  4. #4
    caradoc is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    122
    It was really a very useful post on Memory leaks. Thanks to the admin for such a wonderful posts.


  5. #5
    zsk_00 is offline Member Array
    Join Date
    May 2006
    Answers
    37
    nice post, clears up some confusion


    •    Sponsored Ads



Latest Article

Network Security Risk Assessment and Measurement

Read More...