what is memory leak?

Answer Posted / roshan.p.r.

Consider the following example..

int *p=new int[10];

here ten bytes of memory is allocated and the address is
stored in p. the only way to access this memory is via p.

now suppose if u allocate a new memory to p like

p=new int[20];

the previous address(10 bytes is lost we cant access it
again)because the only way that we had to access it was
pointer p and now a new address(20 bytes) is stored in p.

So we say there is a memory leak.

the best way to handle this is.

int *p=new int[10];
[]delete p;
p=0;
p=new int[20];

so in the above case we have first released the memory
pointed by p with delete function and then a new address is
assigned to it.

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the 4 types of unions?

609


can anyone suggest some site name..where i can get some good data structure puzzles???

1643


Explain a pre-processor and its advantages.

633


Explain the difference between #include "..." And #include <...> In c?

632


What is the significance of an algorithm to C programming?

596






write a C program:There is a mobile keypad with numbers 0-9 and alphabets on it. Take input 0f 7 keys and then form a word from the alphabets present on the keys.

14964


In c programming write a program that will print 10 multiples of 3 except 15,18,21 using looping

977


What is pointers in c?

658


Write a program to compute the similarity between two strings - The program should get the two strings as input - Then it will output one single number which is the percentage of similarity between the two strings

2251


In c programming, explain how do you insert quote characters (? And ?) Into the output screen?

764


The % symbol has a special use in a printf statement. How would you place this character as part of the output on the screen?

769


process by which one bit patten in to another by bit wise operation is? (a) masking, (b) pruning, (c) biting, (d) chopping,

1892


What is difference between structure and union with example?

596


how logic is used

1502


What are the application of c?

649