Thread: bcc32 compiler warning when assigning null to pointer

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    51

    compiler warning when assigning null to pointer

    When i compile this code:

    Code:
    #include <iostream.h>
    
    int main()
    {
    unsigned short *pnt=new unsigned short;
     if (pnt==0)
     {
     cout<<"Not enough free memory available for pnt.";
     return 0;
     }
    *pnt=7;
    cout<<*pnt;
    delete pnt;
    pnt=0;		//HERE IS THE PROBLEM
    return 0;
    }
    I get a warning from my borland bcc32 5.5 compiler
    that says: 'pnt' is assigned a value that is never used in function main()

    I was told to take warnings seriously but the program works, and you should always assign pointers to point to null after calling delete on them. So...

    ...CAN I IGNORE THIS WARNING???
    Last edited by finnepower; 06-24-2002 at 09:56 AM.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    short answer: yes.

    borland's just letting you know that you might have forgotten a step because you never use that variable again. but in reality you're just being cautious.

    if you should ever alter that piece of code and make a mistake, you would know immediately if your pointer was a null pointer because they automatically trigger error messages. i would use NULL instead of zero, though. it makes the code look better, imho.

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Actually you dont need to assign the pointer pnt to null (in this case) because the program terminates anyway after it.

    I would think that after you assign pnt to 0 it expect you to use is later in your program and because you donīt the compiler issues a warning.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I was told to take warnings seriously
    Indeed you should if you don't know whether or not the warning is simply blowing smoke.

    >you should always assign pointers to point to null after calling delete on them
    This is a good habit to get into, but you've found one of the flaws in securing your code. Some compilers will warn you unnecessarily about unreferenced values. So if you have one of these compilers, you have to choose between the security of NULLing out a pointer and clean compiles in case real problems exist. Of course, in some cases there's no need to NULL out the pointer if the program is going to terminate shortly and the pointer will not be used again.

    >...CAN I IGNORE THIS WARNING???
    Yes.

    >i would use NULL instead of zero, though. it makes the code look better, imho.
    Perhaps, but because of C++'s stricter type checking, you tend to have fewer problems by using the integer constant 0.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    51
    Thanks y'all for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM