C++/errors

Advertisement


Question
what is difference between compile time error and run time errors???????????

Answer
A compile-time error is an error that the compiler gives when compiling your code. For example, the following code gives a compile time error:

#include <iostream>
#include <cstring>

void foobar( char* cstr )
{
     std::strcpy( cstr, "hello world" ) ;
}

int main()
{
     foobar( 1234 ) ; // invalid conversion from 'int' to 'char*'
}

>g++43 -std=c++98 -Wall -pedantic -o error error.cc
error.cc: In function 'int main()':
error.cc:11: error: invalid conversion from 'int' to 'char*'

When you get a compile time error, no object file is created.

A run-time error is an error that occurs when you run your program. After you have compiled and linked your code into an executable image.
For example, the following code would compile, but would generate a run-time error when you run the program.

#include <iostream>
#include <cstring>

void foobar( char* cstr )
{
     std::strcpy( cstr, "hello world" ) ;
}

int main()
{
     foobar( "this is a literal string" ) ; // wrong!
}

>g++43 -std=c++98 -Wall -pedantic -o error error.cc
error.cc: In function 'int main()':
error.cc:11:  warning: deprecated conversion from string constant to 'char*'

if you ignore the warning and run the resultant program, you get a run-time error:

> ./error
Segmentation fault  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.