#include<stdio.h>
#include<stdlib.h>
int main()
{
char *temp;
while(1)
{
temp= malloc(sizeof(char)*10);
/*some code*/
}
}
My question is: I was allocating memory in every loop with same name. What will happen to previous memory allocated; and as we are creating variables with same identifier why we are not getting any errors?
PS: Don’t ask what I’m doing in this code. I was doing some other code and by mistake did it. Just re modified it and asking my problem .
It’s called a memory leak. You have no way of ever freeing the memory, so it’s lost to you.
Your process will keep growing in size until you run out of available memory (or, if it’s 64 bit, swap the box into oblivion at which point your local sysadmin will come over and trounce you. If it’s a production machine, you’re likely to find out what a performance improvement plan is from your boss and HR.)