JAN
JAN

Reputation: 21895

Any benefit of using assert instead of using a simple "if" ?

Given this code :

#include <stdio.h>
#include <assert.h>

void print_number(int* somePtr) {
  assert (somePtr!=NULL);
  printf ("%d\n",*somePtr);
}

int main ()
{
  int a=1234;
  int * b = NULL;
  int * c = NULL;

  b=&a;

  print_number (c);
  print_number (b);

  return 0;
}

I can do this instead :

#include <stdio.h>
#include <assert.h>

void print_number(int* somePtr) {
  if (somePtr != NULL)
       printf ("%d\n",*somePtr);
  // else do something 
}

int main ()
{
  int a=1234;
  int * b = NULL;
  int * c = NULL;

  b=&a;

  print_number (c);
  print_number (b);

  return 0;
}

So , what am I gaining by using assert ?

Regards

Upvotes: 15

Views: 16859

Answers (6)

PermanentGuest
PermanentGuest

Reputation: 5341

assert is to document your assumptions in the code. if statement is to handle different logical scenarios.

Now in your specific case, think from the point of view of the developer of the print_number() function.

For example when you write

void print_number(int* somePtr) {
  assert (somePtr!=NULL);
  printf ("%d\n",*somePtr);
}

you mean to say that,

In my print_number function I assume that always the pointer coming is not null. I would be very very surprised if this is null. I don't care to handle this scenario at all in my code.

But, if you write

void print_number(int* somePtr) {
  if (somePtr != NULL)
       printf ("%d\n",*somePtr);
  // else do something 
}

You seem to say that, in my print_number function, I expect people to pass a null pointer. And I know how to handle this situation and I do handle this with an else condition.

So, sometimes you will know how to handle certain situations and you want to do that. Then, use if. Sometimes, you assume that something will not happen and you don't care to handle it. You just express your surprise and stop your program execution there with assert.

Upvotes: 33

Harsh Gaur
Harsh Gaur

Reputation: 132

If Your (if) statement becomes True or False so compiler go for the next instructions. But in assert.h when your statement becomes false "Program Terminates immediately" with assertion message.

EXAMPLE :*

#include <assert.h> #include <stdio.h>

int main () {

    int a;
    printf("Enter an integer value: "); scanf("%d", &a); assert(a >= 10);
    printf("Integer entered is %d\n", a);
    return(0); }

Upvotes: 1

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361812

The difference is that assert is enabled only for debug build; it is not enabled for release build (i.e when NDEBUG is defined), which means in the release build there will be no check; as a result, your code will be little bit faster, compared to the one in which you use if condition which remains in the release build as well.

That means, assert is used to check common errors when you write the code, and catch them as soon as possible, in the development phase itself.

Upvotes: 14

tenfour
tenfour

Reputation: 36906

Lots of reasons:

  1. Asserts are usually removed for release builds.
  2. Asserts will report failure information to the client. if() does nothing by itself.
  3. Because asserts are usually macros, you can also get code information about the failing assertion.
  4. Assert is more semantically clear than if().

Upvotes: 10

marcinj
marcinj

Reputation: 50046

Assert will inform you that something wrong happend, possibly error to be fixed. In debug mode it will break and show callstack that will help you with fixing bug. So its a good practice to use. I would actually use if() and assert, because in Release your asserts should be turned off:

void print_number(int* somePtr) {
  assert(somePtr != NULL);
  if (somePtr != NULL)
       printf ("%d\n",*somePtr);
  // else do something 
}

in " // else do something " you might think of throwing exception or returning error code.

Upvotes: 3

SingerOfTheFall
SingerOfTheFall

Reputation: 29986

If assertion fails, you will see the output containing the failed assertion itself, plus the function and the line of the failed assert, something like:

test: main.cpp:9: int main(): Assertion `0==1' failed.

So, if your program crashes in runtime, you will see the exact reason and location of the crash. There's a big article about assertions in wiki.

Upvotes: 7

Related Questions