+ Reply to Thread
Results 1 to 5 of 5

Thread: Memory Leak & Structure Padding

  1. #1
    Junior Member
    Join Date
    Feb 2006
    Location
    India
    Posts
    13
    Rep Power
    8


    hi friends

    Can you tell me


    1)What is Memory Leak?


    2)What is structure Padding?


    Friends can you give some tricky programs to solve, so that i can increase my programming skills....

  2. #2
    Super Moderator sk_kireeti's Avatar
    Join Date
    Feb 2006
    Posts
    2,279
    Rep Power
    65
    1)What is Memory Leak?
    Unused memory or unwanted memory that is reserved by the program is a memory leak.

    You can get more info on how to handle this situation in the following URL.
    http://www.troubleshooters.com/codecorn/memleak.htm

    Thnaks,
    Krishna


  3. #3
    Super Moderator sk_kireeti's Avatar
    Join Date
    Feb 2006
    Posts
    2,279
    Rep Power
    65
    Hi Vijay,

    Refer the following link. It has some interesting info on Structure padding.

    http://www.thescripts.com/forum/thread452200.html

    Hope it will help you understand the concept.

    Thanks,
    Krishna


  4. #4
    MNR
    Unregistered

    Smile Re: Memory Leak & Structure Padding

    I know Structure padding;

    take one simple example
    structure ex
    {
    int a;
    char b;
    };
    The size of structure ex is actually 5 but in c ( i dont know in dos or winodws compliler i am using Linux) it takes 8 bytes because sizeof pointer is 4 bytes . because of pointer traversing it takes multile of 4 bytes. if u want to remove structure padding use at a time of complisation
    cc -fpact-struct filename.c
    ie "-fpack-struct " it removes structure padding.. now it displays 5 .....


    Memory Leak..........

    A memory leak is a particular kind of unnecessary memory consumption by a computer program, where the program fails to release memory that is no longer needed. The term is somewhat of a misnomer, since memory is not leaked or lost from the computer, but rather no longer available for use.

    As is noted below, a memory leak has similar symptoms to a number of other problems, and generally can only be diagnosed by a programmer with access to the original program code; however many people are quick to describe any unwanted increase in memory usage as a memory leak, even if this is not strictly accurate.

    Here is a C function that deliberately leaks memory by losing the pointer to the allocated memory. Since the program loops forever calling the defective function, it will eventually fail when no more memory is available to the program.

    int f(void)
    {
    char* s;
    s = malloc(50); /* get memory */
    if (s==NULL) return 1; /* no memory available */
    else
    { /* memory available */
    return 0; /* memory leak - see note below */
    }
    /*
    * Memory was available and pointed to by s, but not saved.
    * After this function returns, the pointer is destroyed,
    * and the allocated memory becomes unreachable
    *
    * to "fix" this code, you would add the statment "free(s)" to
    * the else block before the "return 0" statement
    */
    }

    int main(void)
    {
    /* this is an infinite loop calling the above function */
    while (1) f(); /* This function call will fail to malloc sooner or later */
    return 0;
    }



    : ) MNR
    Last edited by Unregistered; 09-09-06 at 12:17 PM.

  5. #5
    Senior Member
    Join Date
    Aug 2006
    Location
    Hyderabad,India
    Age
    34
    Posts
    6,052
    Rep Power
    115

    Re: Memory Leak & Structure Padding

    thanks krishna

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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

Content Relevant URLs by vBSEO 3.5.1 PL1