C++/errors
Expert: vijayan - 9/28/2008
Questionwhat is difference between compile time error and run time errors???????????
AnswerA 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