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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:16:32+00:00 2026-05-25T12:16:32+00:00

My assert macro is like this: #ifdef DEBUG #define ASSERT(x) ((void)(!(x) && assert_handler(#x, __FILE__,

  • 0

My assert macro is like this:

#ifdef DEBUG
#define ASSERT(x) ((void)(!(x) && assert_handler(#x, __FILE__, __LINE__) && (exit(-1), 1)))
#else 
#define ASSERT(x) ((void)sizeof(x))

I thought this was more or less bulletproof but I seem to be using it a lot in the context of asserting the return value of functions which are important for their side effects. If in my release build I end up compiling

ASSERT(fgets(buffer,sizeof(buffer)/sizeof(buffer[0]),file));

which would become

((void)sizeof(fgets(buffer,sizeof(buffer)/sizeof(buffer[0]),file)));

Is there a chance this will get completely optimized out? I am fairly certain that it won’t (I’m calling a function, fgets), but what exactly is the condition that assures it? Are there any operations with side effects which the optimizer might throw out?

  • 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-25T12:16:33+00:00Added an answer on May 25, 2026 at 12:16 pm

    The usual meaning of assert is to be optimized out, so it might be better to stick to those semantics and do

    #else 
    #define ASSERT(x)
    #endif
    

    If you insist on it not being optimized out, why not just do

    #else 
    #define ASSERT(x) ((void)(x))
    #endif
    

    ?

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

Sidebar

Related Questions

In C#, one can make a method such as this: [Conditional(DEBUG)] private void MyFunction()
I'm using a macro that might be dangerous: #define REMAINDER(v, size) ((v) & (size
I'm writing a Win32 service in C++. I have a custom Assert macro that
Any failed ASSERT statements on Windows cause the below debug message to appear and
From what I understand assert is a macro in C and supposedly if you
Consider this Python code: assert(a > 0) assert(b > 0) assert(a + b >
Having the code: [Test] public void ShouldDoSomethingMeaningFull() { Assert.Fail(); } I often need to
I'm new to programming & i found this code when i was going through
This is a part of a static assert trick. I can't understand how the
Why does this simple assert statement fail? From what I've read I should be

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.