NULL & int*

This is a discussion on NULL & int* within the C Programming forums, part of the General Programming Boards category; I may be being stupid, but can you guys confirm that the following tests are logically equivalent Code: char *sring; ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    NULL & int*

    I may be being stupid, but can you guys confirm that the following tests are logically equivalent
    Code:
    char *sring;
    ....
    if(string)
    {
       free(string);
    }
    ...
    if(string != NULL)
    {
       free(string);
    }
    also, is this approach for an array of integers valid:
    Code:
    int *numbers;
    
    number = malloc(sizeof(int) * 10);
    ....
    if(number)free(number);
    I lost a few brain cells over the weekend!!!

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    thanks salem.

    I usually check if the malloc has suceeded directly after.

    >>They are - but that's not what you should be testing.

    I usually cleanup at the end of a function.

    So really, the answer is to make sure that pointers are initialised to NULL at their declaration.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    makes sense...


    ..cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  2. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  4. Big Problem With Passing an Matrix to Main Function
    By Maragato in forum C Programming
    Replies: 4
    Last Post: 06-14-2004, 11:06 PM
  5. Really Need Help With My Opengl Camera!!!!
    By psychopath in forum Game Programming
    Replies: 13
    Last Post: 05-28-2004, 03:05 PM

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