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

#define PRINT(int) printf("int = %d ",int) main() {< BR> intx,y,z; x=03;y=02;z=01; PRINT(x^x); z<<=3;PRINT(x); y>>=3;PRINT(y); }

724


What is a pragma?

672


What is the difference between a string and an array?

711


What is a scope resolution operator in c?

754


Multiply an Integer Number by 2 Without Using Multiplication Operator

326






Can a file other than a .h file be included with #include?

688


When is a void pointer used?

682


What is restrict keyword in c?

647


What does it mean when a pointer is used in an if statement?

606


Explain #pragma statements.

606


What is #define?

578


What is meant by realloc()?

680


Can we declare variable anywhere in c?

539


Is there a way to have non-constant case labels (i.e. Ranges or arbitrary expressions)?

585


What are the advantages of using macro in c language?

596