Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

core dumped in assert

Tags:

c

assert

coredump

Hi when i try to use assert function in my program it dump the code. Can anyone tell me what is the problem with my code.

#include <stdio.h>
#include <assert.h>
void print_number(int myConfig) {
    assert (myConfig > 20);
    printf("\nConfig value is : %d",myConfig);
}

int main ()
{
int configArr[]={21,27,15};
for(int i=0;i<=2;i++)
  print_number (configArr[i]);
return 0;
}

Output :

Config value is : 21
Config value is : 27Assertion failed: myConfig > 20, file assert.cpp, line 4
Abort (core dumped)
like image 639
Jeyamaran Avatar asked Jan 22 '14 06:01

Jeyamaran


2 Answers

There is nothing wrong with your code.

The assert macro checks for the validity of the assertions or assumptions. If the assertion results to be FALSE then the macro writes information about the call that failed on stderr and then calls abort(). abort() raises the SIGABRT signal and this results in an abnormal termination of the process.

In your code, during the third (well, 2nd technically!) iteration of the for loop, "myConfig > 20" fails as value of myConfig is 15 and hence the process terminates abnormally.

like image 121
Kumaragouda Avatar answered Oct 29 '22 15:10

Kumaragouda


Your code is OK. When your code executed, if the assert expression(15 > 20) is false, assert() writes information about the call that failed on stderr and then calls abort() which raises the SIGABRT signal, and a core dump may be generated depending on your system settings.

like image 20
jfly Avatar answered Oct 29 '22 17:10

jfly