Thread: Am I freeing the memory in the correct way?

  1. #1
    Registered User
    Join Date
    Apr 2014
    Location
    Sofia, Bulgaria, Bulgaria
    Posts
    5

    Am I freeing the memory in the correct way?

    I am making a program for managing stocks (for fun and to learn)
    Here's some bits of the code you need to know in order to help me:
    Code:
    struct stockNode {
        int ID;
        struct stockNode *next;
    };
    typedef struct stockNode stock;
    
    int memLoc(void **var, int size)
    {
        if (NULL == (*var = malloc(size)))
        {
            printf("\nError (sui). Unable to allocate memory.");
            system("pause");
            return -1;
        }
        else return 1;
    }
    This is a function, with which the program finds the given stock by ID. head is already allocated in the previous function and passed on by address.
    Code:
    void menu_findStock(stock **head)
    {
        stock *curr; memLoc(&(curr), sizeof(stock));
    
        curr = *head;
        ...
        curr = NULL;
        free(curr);
    }
    As you can see, I allocate memory for curr in the function and then I assign curr = *head, which assigns the pointer curr to point to the address of *head as I believe. The thing is, if I do free(curr) at this point, I lose *head and all its values. Why?
    So I tried setting curr = NULL, but is using free(curr) after that of any use?
    Thanks.
    Last edited by Ripedox; 08-25-2014 at 03:38 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > which assigns the pointer curr to point to the address of *head as I believe.
    Yes, and it also loses the memory you allocated in the previous line.

    > memLoc(&(curr), sizeof(stock));
    Why did you ignore the return result?

    > So I tried setting curr = NULL, but is using free(curr) after that of any use?
    Not really, freeing NULL does nothing.

    > The thing is, if I do free(curr) at this point, I lose *head and all its values. Why?
    Because I guess it would have been better to do
    *head = curr;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2014
    Location
    Sofia, Bulgaria, Bulgaria
    Posts
    5
    I ignore memLoc's result as it's unfinished and I don't need to know if I am running out of memory for the current example.

    *head = curr; won't do it as I am using *head for my last list item so I can use *head -> next to go trough the whole list in search of the given ID. I only use *head = curr when I am creating a new stock.

    When I do "stock *curr;", don't I need to allocate memory for this pointer? As in the memory that the pointer occupies when it's created (&curr). And then free it? I thought that memLoc was doing that for me, but from what I see it's just telling the pointer where to point.

    Technically, what I am asking is, imagine if a machine has about 8 memory places (integer sized). I can't just do "stock *curr;" without checking if there's memory for the pointer to be created, can I?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > void menu_findStock(stock **head)
    The answer would seem to be that you don't need to allocate anything for a 'find' function.

    As in
    Code:
    curr = *head;
    while ( curr ) {
      if ( something ) {
      }
      curr = curr->next;
    }
    You don't allocate anything, you don't free anything either, the list remains unchanged.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2014
    Location
    Sofia, Bulgaria, Bulgaria
    Posts
    5
    Quote Originally Posted by Salem View Post
    You don't allocate anything, you don't free anything either, the list remains unchanged.
    Hey again. I asked my teacher and he said that I don't need to allocate memory for pointers. Why? Because they take up waaay too little memory, as they are integers, and if allocating a pointer would cause a crash the OS would've crashed long ago.

    Thanks for the help, now I am still wondering ,out of curiousity, is it possible to allocate memory for a pointer. Again, not for the place where the pointer points, but for the pointer itself as in the integer that acts like coordinates for the computer to navigate.
    Last edited by Ripedox; 08-29-2014 at 01:22 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ripedox
    I asked my teacher and he said that I don't need to allocate memory for pointers. Why? Because they take up waaay too little memory, as they are integers, and if allocating a pointer would cause a crash the OS would've crashed long ago.
    I think you misunderstood your teacher. You don't need to allocate memory for pointers separately here because when you dynamically allocate memory for a stockNode object, you already have the int member and pointer member allocated as members of the object. If you declare a pointer variable within a function, then memory is allocated for that pointer variable itself for you; you don't need to (and shouldn't) say, call malloc, but nonetheless memory was allocated.

    It is not true that you do not need to allocate memory for pointers "because they take up way too little memory". If you have not allocated memory for an object (whether dynamically with malloc or otherwise), then it does not exist and you cannot use it, and this includes pointers. Saying "if allocating a pointer would cause a crash the OS would've crashed long ago" makes sense if you understand it to mean "you can go ahead and declare a pointer as there will probably be enough memory for it"; it doesn't make sense to support the argument that you do not need to allocate memory for pointers.

    Quote Originally Posted by Ripedox
    is it possible to allocate memory for a pointer. Again, not for the place where the pointer points, but for the pointer itself as in the integer that acts like coordinates for the computer to navigate.
    Yes. You have already encountered this just by declaring a pointer within a function, but you are perhaps talking about dynamic alloaction with malloc. For example:
    Code:
    int **p = malloc(sizeof(*p));
    Here, we allocate memory for a pointer of type int*, and have p point to it.
    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. Freeing memory
    By Soel in forum C Programming
    Replies: 10
    Last Post: 12-16-2010, 06:31 AM
  2. Dynamic memory and realloc(), freeing memory
    By C_Sparky in forum C Programming
    Replies: 6
    Last Post: 10-06-2010, 07:55 PM
  3. Freeing memory
    By C_ntua in forum C Programming
    Replies: 17
    Last Post: 06-29-2008, 04:42 AM
  4. Freeing memory
    By bikr692002 in forum C++ Programming
    Replies: 6
    Last Post: 04-09-2006, 04:58 PM
  5. freeing memory
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 04-27-2003, 08:51 PM

Tags for this Thread