Thread: Where are leaks?

  1. #1
    Registered User
    Join Date
    Dec 2020
    Posts
    3

    Where are leaks?

    Hello, i need help from more intelligent people than i am I have in my school project leaks, but i dont know where. When i run some piece of code i found leak here in allocation.

    Code:
    stStruct** myArray = NULL;
    
    
    void allocMyArray()
    {
        if (myArray == NULL) {
            myStruct = (stStruct**)malloc(sizeof(stStruct*)*dimense);
            for (int i = 0; i < dimense; i++) {
                myArray[i] = (stStruct*)malloc(sizeof(stStruct));
            }
        }
    }
    
    
    void deleteMyArray()
    {
        if (myArray != NULL) {
            for (int i = 0; i < dimense; i++) {
                free(myArray[i]);
            }
            free(myArray);
            myArray = NULL;
        }
    }

    Maybe is leak on another place .. :/
    I will be very happy for any help

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,231
    You should avoid global variables and clean up on failure. So, I might expect something like this:
    Code:
    stStruct **allocMyArray(size_t dimense)
    {
        stStruct **myArray = malloc(sizeof(myArray[0]) * dimense);
        if (!myArray) {
            return NULL;
        }
    
        for (size_t i = 0; i < dimense; i++) {
            myArray[i] = malloc(sizeof(*myArray[i]));
            if (!myArray[i]) {
                for (size_t j = 0; j < i; j++) {
                    free(myArray[j]);
                }
                return NULL;
            }
        }
        return myArray;
    }
    Having said that, do you really need a dynamic array of stStruct pointers, each of which points to a lone stStruct object rather than to the first object in a dynamic array of stStruct objects? This can be useful if you want to allow for "gaps" in the array such that an object can be removed from the array by setting the corresponding pointer to be a null pointer. But if not, it would be more efficient to simply create a dynamic array of stStruct objects rather than pointers.

    You might also consider creating a struct to bundle the dynamic array with its dimension, e.g.,
    Code:
    struct stStructArray
    {
        stStruct **data;
        size_t dimense;
    };
    This can help avoid mistakes where you have more than one such dynamic array, and then you mix up which dimension is for which dynamic array.
    Last edited by laserlight; 12-08-2020 at 05:27 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    167
    Quote Originally Posted by laserlight View Post
    You should avoid global variables and clean up on failure. So, I might expect something like this:
    Code:
    stStruct **allocMyArray(size_t dimense)
    {
        stStruct **myArray = malloc(sizeof(myArray[0]) * dimense);
        if (!myArray) {
            return NULL;
        }
    
        for (size_t i = 0; i < dimense; i++) {
            myArray[i] = malloc(sizeof(*myArray[i]));
            if (!myArray[i]) {
                for (size_t j = 0; j < i; j++) {
                    free(myArray[j]);
                }
                return NULL;
            }
        }
        return myArray;
    }
    Having said that, do you really need a dynamic array of stStruct pointers, each of which points to a lone stStruct object rather than to the first object in a dynamic array of stStruct objects? This can be useful if you want to allow for "gaps" in the array such that an object can be removed from the array by setting the corresponding pointer to be a null pointer. But if not, it would be more efficient to simply create a dynamic array of stStruct objects rather than pointers.

    You might also consider creating a struct to bundle the dynamic array with its dimension, e.g.,
    Code:
    struct stStructArray
    {
        stStruct **data;
        size_t dimense;
    };
    This can help avoid mistakes where you have more than one such dynamic array, and then you mix up which dimension is for which dynamic array.
    Just to prove the power of code reviews

    Code:
    stStruct **allocMyArray(size_t dimense)
    {
        stStruct **myArray = malloc(sizeof(myArray[0]) * dimense);
        if (!myArray) {
            return NULL;
        }
    
        for (size_t i = 0; i < dimense; i++) {
            myArray[i] = malloc(sizeof(*myArray[i]));
            if (!myArray[i]) {
                for (size_t j = 0; j < i; j++) {
                    free(myArray[j]);
                }
                free(myArray);  // <<< ADDED THIS
                return NULL;
            }
        }
        return myArray;
    }

  4. #4
    Registered User
    Join Date
    Dec 2020
    Posts
    3
    Thanks a lot, i repaired my code. But still have leaks :/ Last option where i think can be leak is that, i have struct which store 2 another strucs. How i should free this struc correct? I can add code, but there is lot, so if u can add some your example i will be glad.

    "example"
    Code:
    typedef struct a{
     int id;
     char name[50];
     char tel[12];
    }stA
    
    typedef struct b{
     int id;
     char str[50];
    }stB
    
    typedef struct c{
     int id;
     stA* myA;
     stB* myB;
    }stC

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,231
    That depends on how you allocate them.

    Have you changed your global variables into local variables? It will be easier to reason about your code that way.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2020
    Posts
    3
    We have in our assignment, so i cant change it
    Code:
    static stStruct** myArray = NULL;


    For two first struct i read data from file and load them to the separate linked list. I have in these two struct pointer on next node, that i build this linked list and i have static variable on actual node and first node. There is no problem. When i want get these two struct like a parameters to third struct. First i allocated memory only for third struct(how i showed in my allocated example). Then i call function, where i allocated memory for struct A and B and created new object, because i need stored these data after i dealocated linked list for these structs.

    I apologize for the way I am trying to explain, my english is not so good, but i trying do my best. I can add my code on github if u want, maybe it will be easier.
    Thank u good people

    edit:
    Maybe could you show me on my previous 3 struct, how would you do this array.
    I have 3 structs. A, B, C .. A and B i load from file. Then i allocated array for objects C for example size is 2. When i created i call function find A and B by ID and then i add into object C and this object add to array ..
    Last edited by ShadowDoc; 12-09-2020 at 06:01 PM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,231
    Here's what I would do if I were you:
    • For each component that you need dynamic memory allocation, create a function to allocate (and possibly initialise), and another function to free the allocated memory. You already have an example of this with allocMyArray and deleteMyArray.
    • If a component builds on smaller components that require dynamic memory allocation, the allocator function will not call malloc directly, but rather it will call the allocator function of the smaller components. Likewise, the deallocator function will not call free directly, but rather it will call the deallocator function of the smaller components.
    • Test that these functions work independently of the rest of your program, e.g., for each component write a tiny test program that just creates and destroys a component object. This is also where you can use your leak checker tool to check that the simplest use of such a pair of functions do indeed result in no memory leaks.
    • Combine the use of these components and their allocator/deallocator functions into your actual program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leaks
    By shikhardeep in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2011, 03:55 AM
  2. Memory Leaks!
    By td4nos in forum C Programming
    Replies: 5
    Last Post: 07-28-2009, 09:12 AM
  3. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM
  4. any leaks?
    By shadovv in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2005, 12:46 PM
  5. COM Memory Leaks
    By subdene in forum Windows Programming
    Replies: 0
    Last Post: 06-07-2004, 11:57 AM

Tags for this Thread