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

  • SEARCH
  • Home
  • 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 8306287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:18:06+00:00 2026-06-08T18:18:06+00:00

So I ran into a bug today where a NULL was passed into a

  • 0

So I ran into a bug today where a NULL was passed into a constructor’s argument list and this caused the application to break. Its odd that the compiler did not prohibit this from happening. Since the argument list changed, the problem was not noticed until now. See the following code snippet:

This object takes 3 parameters, pay close attention to std::string&.

class Foo {
public: 
    std::string myName;
    unsigned int someVariable;
    void * ptr;

    Foo(const std::string&  name, void * aPtr, unsigned int variable);
    ~Foo();
}

Foo::Foo(const std::string&  name, void * aPtr, unsigned int variable) : myName(name), ptr(aPtr), someVariable(variable)
{
   // object constructed
}


int main(int argc, char* argv[])
{
   // construct an instance of Foo
   Foo foo(NULL /*whoops, passed in NULL when a string should be passed in*/,
           "foo", 
           0);   // program compiles as expected, A NULL pointer runtime error occurs when executed.
}

So basically, if you accidently switch your input values for your foo object the compiler does not do anything. No alarm is sounded and you are left scratching your head what happened when the program crashes. I would think there should be a way to prevent this from occurring. Is there something that works around this issue? Is there something in the compiler that should be turned on?

  • 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-06-08T18:18:07+00:00Added an answer on June 8, 2026 at 6:18 pm

    Actually, it’s not really NULL that is being passed by reference.

    std::string has a converting constructor that takes a char const*. NULL is a null pointer constant, so it can be used where a char const* is expected, so a std::string object is constructed from this null pointer. This construction yields undefined behavior.

    One option for providing a better warning to the user would be to add another constructor that has a char const* parameter. This way you can easily add an assert in the constructor if null is passed in. It’s not a compile-time check, but it might be better than nothing if your run into this problem frequently (for what it’s worth, I can’t recall ever having run into this problem, so I’d argue that it’s not worth the effort).

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

Sidebar

Related Questions

I ran into an odd bug today when I was running some unit tests
I ran into this weird bug or something in android's 4.0.1 Ice Cream Sandwitch,
I ran into what seemed a mysterious bug this morning that I feel very
We ran into a bug in our product caused by the fact that for
So I ran into this exact problem: http://www.vistax64.com/powershell/273120-bug-when-using-namespace-parameter-new-webserviceproxy.html The gist of the issue is
I ran into this today and the only thing I can think is that
I ran across this bug three times today in one of our projects. Putting
Ran into an odd bug while trying to play around with some perlin noise
While troubleshooting a bug I ran into this. It seems to be due to
I recently ran into a bug in my code when using boost::bind . From

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.