Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3751258
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:02:56+00:00 2026-05-19T09:02:56+00:00

Is the assignment and check for null explicitly required in the below snippet to

  • 0

Is the assignment and check for null explicitly required in the below snippet to achieve the same result. Also, is there anything wrong or can be improved here.

class Sample
{

private:
    char *bits;

public:

    Sample() 
    {
       bits = NULL; //should this be explicit?
    }


    ~Sample() 
    {
        if (bits != NULL) 
        {
           delete [] bits; //should this be explicit?
        }
        bits = NULL; //should this be explicit?
    }

};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T09:02:57+00:00Added an answer on May 19, 2026 at 9:02 am

    In the first case, where you do this in the constructor:

    Sample()
    {
        bits = NULL;
    }
    

    Whether or not you need this assignment depends on your code – if bits may not have any memory allocated to it at all, then the NULL assignment should be there because of the delete in the destructor. And really as a safety issue, you would probably want the NULL unless you document some contract the user has to obey, but here since you are initializing the pointer to NULL, I will assume that means bits could realistically be NULL or actually have new-allocated memory at some point.

    If you did not initialize the pointer, and it is never subsequently made ‘valid’ by being assigned something, and the object is destroyed, then bad stuff happens.

    Also, as a habit, prefer to use initialization lists when possible in constructors:

    Sample() 
        : bits(NULL)
    {
    
    }
    

    For the destructor, you do not need to check if a pointer is not NULL before deleting it. delete on a NULL pointer is completely safe. You also don’t need to reassign the bits pointer back to NULL, the object is going away.

    ~Sample()
    {
        delete [] bits;
    }
    

    Of course, if you delete an invalid pointer, then bad stuff will happen regardless.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way of doing parallel assignment in C++? Currently, the below compiles
Is variable assignment expensive compared to a null check? For example, is it worth
For a school assignment I have to write x86 assembly code, except I can't
I need to retrieve the PIN from a notepad file (below) and check it
I am doing an assignment for school and I'm not sure how to check
For my university assignment I have to design some basic managment system for sicknesses
For a database assignment I have to model a system for a school. Part
For my assignment I have been assigned the task of creating a DTD for
For my university assignment I have to make a networkable version of pacman. I
For a homework assignment I was given a Card class that has enumerated types

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.