bad style?

This is a discussion on bad style? within the C Programming forums, part of the General Programming Boards category; I just wonder if freeing memory that has been allocated by another function is considered bad style. Code: #include <stdio.h> ...

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21