Jeyamaran
Jeyamaran

Reputation: 783

core dumped in assert

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)

Upvotes: 8

Views: 12685

Answers (5)

jfly
jfly

Reputation: 7990

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.

Upvotes: 4

Kumaragouda
Kumaragouda

Reputation: 126

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.

Upvotes: 11

nitish712
nitish712

Reputation: 19774

assert is a macro which is basically used to check the conditions at run-time. If the condition fails, your program is aborted at that point. Since the program has terminated without running completely, one may want to check the program state at that point. The core dumped indicates a snap-shot of the memory, register contents. The system usually dumps this file in some particular directory.

In your code, the assert has failed for the last condition and hence the abortion and the core dump.

Upvotes: 0

Jeegar Patel
Jeegar Patel

Reputation: 27230

when i run your code i got below output

Config value is : 21
Config value is : 27
a.out: test.c:4: print_number: Assertion `myConfig > 20' failed.
Aborted

see your 3rd member of array is 15 which is less than 20 so it failed assert function so it will give your assert message and abort.

I do not see any core dump here.

Upvotes: 0

Rahul R Dhobi
Rahul R Dhobi

Reputation: 5816

Because in last iteration your myConfig is 15 and assert is checking for 15>20 which is false,and your program got abort and core dumped if core dump is configured in your system. refer http://ptolemy.eecs.berkeley.edu/~johnr/tutorials/assertions.html for more information.

Upvotes: 0

Related Questions