0

this is my first time using dynamic memory allocation and I don't know how to check for memory leaks in my code

Just in general, how can I check for memory leaks in visual studio? I don't know how to track the heap and stack and so I'm mostly shooting in the dark here.

I should mention I used windows 10 and from what I know Valgrind doesn't offer support to W10

Dictionary* createDic(Dictionary* dics, int* size) {
    Dictionary* temp = NULL;
    //some extra code for the variables below which arent al that important for the question
    temp = malloc(++*size * sizeof(Dictionary));
    if (temp==NULL)
    {
        printf("\nThe creation of the dictionary has failed!");
        *size+=-1;
        freeArray(splitReciever,count);
        return dics;
    }
    for (int i = 0; i < (*size - 1); i++)
    {
        temp[i] = dics[i];
    }
    temp[*size - 1].languages = splitReciever;
    temp[*size - 1].numOfLanguages = count;
    temp[*size - 1].wordList = NULL;
    dics = temp;
    return dics;
}

I was also wondering if this code would work, again, without causing memory leaks?

Dictionary* createDic(Dictionary* dics, int* size) {
    Dictionary* temp = NULL;
    //some extra code for the variables below which arent al that important for the question
    temp = realloc(dics , ++*size * sizeof(Dictionary));
    if (temp==NULL)
    {
        printf("\nThe creation of the dictionary has failed!");
        *size+=-1;
        freeArray(splitReciever,count);
        return dics;
    }
    temp[*size - 1].languages = splitReciever;
    temp[*size - 1].numOfLanguages = count;
    temp[*size - 1].wordList = NULL;
    dics = temp;
    return dics;
}
someone
  • 19
  • 3

1 Answers1

0

To check for memory leaks tools like valgrind can be used – while these cannot give you guarantees of (in-)existence of memory leaks or for finding any location of a memory leak – there are false positives and negatives possible – their results still provide pretty valuable hints for where you should check your code again.

Monitoring memory consumption can give you further hints as well; if it raises beyond reasonable limits then you likely have a memory leak.

To your concrete example:

The first variant might produce a memory leak, but does not necessarily:

Dictionary d1 = ...;
Dictionary d2 = createDic(d1, &n);

// now you could use both of d1 and d2
// however risk of double deletion if re-allocation failed;
// need to compare d1 and d2 for equality before freeing them

Dictionary d3 = ...;    // assume this is the sole pointer to d3
d3 = createDic(d3, &n); // if re-allocation succeeded last reference to
                        // old dictionary is lost -> memory leak

The second variant avoids this, but comes with another drawback:

Dictionary d1 = ...;
Dictionary d2 = createDic(d1, &n);

In case of success, the pointer d1 gets invalidated, reading it results in undefined behaviour. So you'd have to update it to the new value as well.

In the sum this second variant should impose less trouble, so I'd prefer it. By the way: It's equivalent to first variant if you modify that one to delete the old dictionary just before returning in case of success...

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • so in the first variant if I were to do free(dics) and then dics = temp it would cause no issues? also, I use W10 and Valgrind isn't available on W10 from what I've seen – someone Dec 22 '21 at 12:02
  • @someone Yes, indeed. For valgrind: Try your favourite search engine with `valgrind windows`, you'll find alternatives like [here on SO](https://stackoverflow.com/questions/413477/is-there-a-good-valgrind-substitute-for-windows) or [valgrind with WSL](https://www.albertgao.xyz/2016/09/28/how-to-use-valgrind-on-windows/). – Aconcagua Dec 22 '21 at 14:40