Thread: bad style?

  1. #1
    Registered User char's Avatar
    Join Date
    Apr 2002
    Posts
    31

    bad style?

    I just wonder if freeing memory that has been allocated by another function is considered bad style.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int *fun (void);
    
    int main (void)
    {
      int *ptr;
    
      ptr = fun ();
    
      free (ptr);
    
      return 0;
    }
    
    int *fun (void)
    {
      int *ptr;
    
      ptr = malloc (sizeof (int));
    
      return ptr;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: bad style?

    Originally posted by char
    I just wonder if freeing memory that has been allocated by another function is considered bad style.
    Not in my opinion. A lot of the time you setup functions specifically for creating and destroying objects.

    Code:
    node * CreateNode();
    node * DestroyNode(node *);
    Just don't make things obscure (don't hide this type of activity in the depths of an unrelated function!).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User char's Avatar
    Join Date
    Apr 2002
    Posts
    31
    Thank you, Hammer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating C style Strings Question
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2009, 03:19 PM
  2. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  3. very bad delay in picking up WM_MOUSEMOVE
    By hanhao in forum C++ Programming
    Replies: 7
    Last Post: 05-28-2004, 11:02 AM
  4. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 07:31 AM
  5. coding style
    By ActionMan in forum Linux Programming
    Replies: 1
    Last Post: 10-03-2001, 07:36 AM