Thread: practicing memory management

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    228

    practicing memory management

    So I have a declare two structures like so

    Code:
    struct X
    {
       struct Y *y;
    };
    
    struct Y
    {
        char *A
        char *B
    };
    in main this what I have done

    Code:
    int main()
    {
        int size = 100;
        struct X **x = createX(size);
    
       if(x == NULL || (*x)->y == NULL)
       {
          printf("Critical Error! ran out of memory.");
          return EXIT_FAILURE;
       }
    
        freeMem(x);
    }
    I call createX so I can allocate some memory.

    Code:
    Struct X ** createX(int size)
    {
      struct X **x = malloc(size * sizeof(struct X *));
    
      if(x == NULL) return NULL;
    
      (*x)->y = malloc(size * sizeOf((*x)->y));
    
      if((*x)->y == NULL)
      {
         free(x); //We free because we allocated n pointers
         return NULL;
      }
     
      int i;
    
      for(i = 0; i < 10; i++)    
      {
            x[i] = malloc(sizeof(struct X));
    
            if(x[i] == NULL) // If we dont't have enough memory for the pointers to point to a struct X
            {
                free((*x)->y);
                free(x);
                return NULL;
            }
      }
    
    
        return x;
    }
    Call freeMem to deallocate

    Code:
        void freeMem(Struct X **x)
        {
            int i;
    
            for(i = 0; i < 10; i++)
            {
                if(x[i] != NULL) // we dont want to free(NULL)
               {
                   free(x[i]); // free allocated structure
               }
            }
    
    
            if(players != NULL)
            {
               free((*x)->y); // free the pointer to struct Y
               free(x); // free up all the pointers
            }
         }
    That's it. Is there anything I did wrong or forgot to do or even added stuff that I didn't need to?
    Last edited by deathslice; 02-26-2016 at 04:43 PM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Your data structure doesn't make sense.
    You create an array of X*'s.
    Then, attached to the first element's y pointer member, you create an array of Y's.
    Then you set the first 10 of the size (100) elements of x to the address of a single struct each. Note that this overwrites the first element's y pointer and leaks the memory it pointed to.

    Instead of this abstracted presentation, why not show us what you really have in mind and make a little demo program displayed in a single set of code tags for easy viewing/copying/pasting/running.

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    228
    Are you sure it is overwriting it? the first thing I did was create an array of pointers to a struct called X(Note that this doesn't allocate memory for the structure pointer y inside the struct X and the array of pointers have nothing to point to at moment). Then we allocate memory for the struct pointer y. Then we allocate 10 structs and have an array point to them. That is the gist of what createX is doing. Also, I did not really do this with a demo in mind. The point of this was to ask if I was allocating and deallocating memory correctly as that is something you do quite often in c when you are working with pointers.

    Edit: opps now I notice that the size is 100 and the conditional of each for loop is index < 10 when it should have been index < 100 my mistake for both for loops.
    Last edited by deathslice; 02-26-2016 at 07:53 PM.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Yes, it's leaking memory.
    (*x)->y is the same as x[0]->y and you're overwriting x[0].
    The data structure makes no sense in any case.

  5. #5
    Registered User
    Join Date
    May 2015
    Posts
    228
    So would it change anything if I just move that to the end of the function? would it did not overwrite it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory management
    By tempster09 in forum C Programming
    Replies: 2
    Last Post: 12-26-2009, 01:11 PM
  2. Memory management - How/why/when
    By micke_b in forum C Programming
    Replies: 29
    Last Post: 11-07-2007, 12:26 PM
  3. Memory management
    By CompiledMonkey in forum C++ Programming
    Replies: 9
    Last Post: 12-19-2003, 11:41 AM
  4. Memory Management
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-01-2003, 09:01 PM
  5. Memory Management
    By rasdfasfd in forum C Programming
    Replies: 2
    Last Post: 12-03-2002, 08:57 PM