Guddu
Guddu

Reputation: 2447

What happens if you don't free dynamically allocated memory in a function?

I am learning how to write functions in C to accept an array and return a modified array.

In function testfunc (which is supposed to simply add 10 to each element of input array b) I am allocating memory for npts number of integers using malloc. But since I want to return this array using a pointer, I am not freeing this memory at the end of the function. Suppose I call this function 100 times like I am doing in the code, so what happens to the all the memory allocated during the code ? Is the amount of memory used by the code 100*10*4 bytes ? For a function which doesn't work on dynamic memory allocation, I think memory allocated to variables disappears when the function returns the final value and when it is called again, it again allocates memory and so on. But I am confused as to what happens in this case.

i cannot free the allocated memory inside the function since i need it to return the array to the main function, but also i need to call this function more than 100 times for different arrays, so if it keeps on allocating again and again, it will run out of memory

And is there a way to check how much memory a code is using ? (other than looking at Activity Monitor on Mac-OSX).

Thanks !

/* code to test returning array from functions */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int* testfunc(int *a,int npts);

int main(int argc, char* argv[])
{
    int *b;
    int a[10],i,j,npts=10;

    b=(int *) malloc(sizeof(int)*npts);

    for (j=0; j <100; j++)
    {
        printf("iteration number %d \n",j);
        for (i=0; i<npts; i++)
        {
            a[i]=i;
            printf("%d \n",a[i]);
        }

        b=testfunc(a,npts);

        printf("returned array \n");
        for (i=0; i<npts; i++)
        {
            printf("%d \n",b[i]);
        }
    }
    printf("the size of one integer is %d \n",sizeof(int));

    return 0;
}
int* testfunc(int *b,int npts)
{
    int *c;
    int i=0;

    c=(int *) malloc(sizeof(int)*npts);

    for (i=0; i<npts; i++)
    {
        c[i]=b[i]+10;
    }

    return c;
}

This is the possible solution to avoid allocating memory inside a function and being able to call the function multiple times

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void testfunc(int *c,int *d,int npts);

int main(int argc, char* argv[])
{
    int *a,*b;
    int i,j,npts=10;

    a=malloc(sizeof(int)*npts);
    b=malloc(sizeof(int)*npts);

    for (j=0; j <100; j++)
    {
        printf("iteration number %d \n",j);
        for (i=0; i<npts; i++)
        {
            a[i]=i;
            printf("%d \n",a[i]);
        }

        testfunc(a,b,npts);

        printf("returned array \n");
        for (i=0; i<npts; i++)
        {
            printf("%d \n",b[i]);
        }
    }
    printf("the size of one integer is %d \n",sizeof(int));

    free(a);
    free(b);

    return 0;
}
void testfunc(int *c,int *d,int npts)
{
    int i=0;

    for (i=0; i<npts; i++)
    {
        d[i]=c[i]+10;
    }
}

Upvotes: 0

Views: 8717

Answers (5)

It's main that have to free() the allocated space, but you have to be sure that you don't modified the value on the space during the function.

Upvotes: 1

sankar suriya
sankar suriya

Reputation: 38

hey objective of call by reference is to modify the input pointer and return back to caller. means modified values i.e address still with caller.

You can simply do modification without allocate memory in your function

Just correction in your second approach

void testfunc(int *c,int npts) { int i=0;

for (i=0; i<npts; i++)
{
    c[i] += 10; //YOU JUST INCREMENT VALUE by 10 
}

}

My approach to write function:

  1. no memory allocation
  2. no multiple arguments
  3. keep necessary input, output argument , return value
  4. here int *c, npts -- input argument, output [ your value modified in int *c location]

Upvotes: -1

rohit
rohit

Reputation: 228

If dynamically allocated memory is not freed, it results in a memory leak and system will run out of memory. This can lead to program crashing.

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881513

c=(int *) malloc(sizeof(int)*npts);
:
return c;

This passes back both the memory and the responsibility for managing it.

It has become the responsibility of the caller (main in this cae) to free it when finished.

Where your real problem lies is here in main:

b=(int *) malloc(sizeof(int)*npts);
:
for (some number of iterations)
    b=testfunc(a,npts);  // overwrites b

On that "overwrites" line, you actually have a memory leak because you lose access to the memory currently allocated for b (both originally and in the prior iteration of the loop).


And, as an aside, please don't cast the return value of malloc in C. It's not needed and can hide certain subtle errors that you really don't want to have to debug :-)

Upvotes: 3

LS_For_CMU
LS_For_CMU

Reputation: 23

Since you are using malloc, which means you are allocating memory from heap not stack.
You should firstly figure out the mechanism of heap and stack.In C,malloc will help programmer allocate memory from heap, the same as new in C++.
So even if the function return the final value, the memory allocated will not be freed.If you call the function 100 times, it will allocate memory for you 100 times.
And as for a tool, you can refer to Valgrind, which is a powerful tool to check whether there exists a memory error.

Upvotes: 2

Related Questions