char *getSubstring(char *string, int position, int length) { char *pointer; int c; pointer = malloc(length+1); if (pointer == NULL) { printf("Unable to allocate memory.\n"); exit(EXIT_FAILURE); } for (c = 0 ; c < position -1 ; c++) string++; for (c = 0 ; c < length ; c++) { *(pointer+c) = *string; string++; } *(pointer+c) = '\0'; return pointer; } 

I was told that a memory leak occurs in this function because it dynamically allocates a substring buffer, but it is not released anywhere. Tell me how to fix a memory leak. I have to return the result, so you can’t call free(pointer) before.

Wikipedia: Memory Leak .

UPD . More precisely, when this function is called, a memory leak occurs.

This function is called inside another function. Here is a piece of code:

 char *func(int param) { /* Тут вычисляются значения Y, jm, len */ return getSubstring(Y, jm-len+1, len); } 

The same question - how to fix? Here, too, you can not clear the memory! Is this a problem with my program structure?

 i=0; while (other_dna != NULL) { /* ... */ myvar = func(/* ... */); /* используем myvar */ /* ... */ i++; } 
  • 2
    @Dezza Simpson, everything is correct, if the function returns an internal variable, then it would be erased in the nearest time. The task of freeing memory is not on this function, but on the code that calls this function. - etki
  • @Fike: Thank you. I had never known about memory leaks before. I added all the necessary questions. Tell us how to fix it - Dezza Simpson
  • @Dezza Simpson, func is just a wrapper and passes the line on, I can not say anything. - etki
  • @Fike: Added the right question - Dezza Simpson
  • while (other_dna! = NULL) {/ * ... / myvar = func (/ ... /); / use myvar / / ... * / free (myvar); // finished using - immediately cleared i ++; } - etki

2 answers 2

First, instead of a loop

 for (c = 0 ; c < position -1 ; c++) string++; 

you could just write string + = position - 1;

Secondly, I don’t like the fact that memory allocation takes place inside a function, and its release is outside. I would rewrite it like this:

 char* getSubstring(const char* str, int pos, size_t len, char* substr) { str += pos - 1; strncpy(substr, str, len); *(substr + len) = '\0'; return substr; } 

And the allocation and release of memory would transfer to an external function. In your case, func.

Thirdly, if you really need dynamic strings (think very well, maybe you can still replace them with static ones, even if you need to allocate a lot of memory at once?), Write your framework to work with them. For a sample, you can take a code from CuTest .

    @Dezza Simpson , there is no leak here. Just the calling code must take into account that the result returned from func() located in the memory allocated by malloc() .

    Usually such details must be present in the function description (even if you write for yourself).

    -

    If you approach the task, not as training, then instead of getSubstring() you can use the library (at least in * nix) function strndup (see man strndup )

    You will have

      char *func() { ... return strndup(Y + jm - len + 1, len); } 

    -

    In general, I would add that this (returning dynamic memory in one form or another and releasing it elsewhere) is a fairly common practice and you should not be afraid to use it (although a more traditional (academic) approach to design encourages the allocation / release of resources for level).

    Just a little attention to the details of the program and everything will be OK.