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....
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....
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
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
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.
thanks krishna
There are currently 1 users browsing this thread. (0 members and 1 guests)