开发者

allocating memory with same identifier

#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.)


what will happen to prev memory allocated.

Will be lost forever. That is what is called "memory leak".

and as we are creating variables with same identifier why we are not getting any errors

That's not a compilation error. The compiler doesn't follow your logic, only your syntax. Overwriting values is perfectly fine with the compiler.


The previos memory allocated will be leaked, since its not freed and its address gets lost to the program. You don't get any errors because you are not creating variables with the same identifier, you are allocating different chunks of memory and storing the pointer to them into the same temp variable.

Consider reading some basic C programming material as you seem to be confusing variables with memory.


The compiler will in most cases not detect logical errors, such as this, where the programmer allocates memory but never frees it.

This is what we call a "memory leak".


Here temp is a pointer not an identifier.
So temp POINTS to the memory allocated my malloc.

So when you allocate new memory and use the SAME pointer to point to the newly allocated memory, it NO LONGER points the the old allocated memory block and it is lost forever. So there you have a memory leak.

If you no longer need the memory pointed to by temp after the end of the current iteration of the loop, use following code instead.

#include<stdio.h>
#include<stdlib.h>
int main()
{
    char *temp;
    while(1)
    {
        temp= malloc(sizeof(char)*10);
        /*some code*/

        /* free the memory pointed by temp */
        free(temp);
    }
}

This way you do not have a memory leak as the memory pointed to my temp is freed at the end of the iteration before a new block is allocated at the start of the next loop.

Update:
Also you can use the same memory block pointed to by temp throughout your loop. No need to reallocate in each iteration.
And if you do not want to retain any old memory you can simply use memset, like this

#include<stdio.h>
#include<stdlib.h>
int main()
{
    /* allocate memopry only once */
    char *temp = malloc(sizeof(char)*10);

    while(1){
        /* zero out the memory so no old data is left */
        memset((void*)temp, 0, sizeof(char)*10);

        /* some code that uses temp */
    }

    /* free the allocated memory */
    free(temp);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜