user2815547
user2815547

Reputation:

Memory leak in the code?

I am trying to learn memory in C and wanted to know if there is a memory leak in the following function:

void someFunction(void)
{
    unsigned char i;
    for( i=0; i < upperbound; i++ ){
        // Do Something
    }
}

Do I need to unalloc for the unsigned char i?

Upvotes: 0

Views: 65

Answers (1)

Barmar
Barmar

Reputation: 781058

There's no memory leak in that code. Local variables are allocated on the stack, and deallocated automatically when the function exits. You only have to free data that was allocated with functions like malloc or realloc.

Note that some library functions return data that was allocated dynamically, and you may be required to deallocate it when you're done. If a function returns a pointer, or modifies a pointer argument you provide as an argument, you must check the documentation to see whether this is necessary.

Upvotes: 4

Related Questions