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 9116749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:44:56+00:00 2026-06-17T04:44:56+00:00

While doing programming i am using assert as well as NULL pointer validation. But

  • 0

While doing programming i am using assert as well as NULL pointer validation.

But as i know assert will be useful only in DEBUG mode.

My question is suppose i have a internal pointer which i am sure cant be NULL example function returning a pointer(but the pointer is not a member of class) in such cases i can use assert

test* ptr = fun(); // return a pointer of type test
assert(ptr);

//do some operation

or NULL pointer validation

test* ptr = fun(); // return a pointer of type test
assert(ptr);
if (NULL != ptr)
{
    //do some operation
}

Here which code practice is good.As per my understating it will be second one.
Because i have faced some situations where the value of ptr returns NULL due to some abnormal cases that we cant even think of.

But do we have any other better options?

  • 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-17T04:44:57+00:00Added an answer on June 17, 2026 at 4:44 am

    The real solution depends on the semantic of the function fun.

    If returning NULL is semantically invalid, then I think fun should throw an appropriate exception (such as std::logic_error1) instead of returning NULL, and you could use assert on the call site to ensure that fun is working fine, and if it is not working fine, then abort the program. In this way, the bug in fun doesn’t propagate to the rest of the program, as it is caught immediately.

    However, if returning NULL from fun is semantically valid, then you should check the return value on the call-site using if and assert is not really needed in this case, because you will use if anyway.

    1. Or you could use std::runtime_error or std::domain_error.

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

Sidebar

Related Questions

I wanted to know how to flush the socket streams while doing socket programming
While doing some refactoring I've found that I'm quite often using a pair or
While doing performance testing on windows, i usually use perfmon. But when i put
I'm new to programming and using SQL Server Compact and I'm doing this personal
I am new to opengl programming, but I am doing something very basic, and
I've been doing object-oriented programming for most of the past 7 years, using Java
I am using CI for my web-site. While programming CMS for the same I
While doing concurrent programming I need to tell the compiler/optimizer that it may not
I've been doing some simple web programming using python, and I have a basic
While doing variadic template programming in C++11 on GCC, once in a while I

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.