d****i 发帖数: 4809 | 1 We know that try catch block does the exception handling in C++. But what if
we don't use try catch block if an exception is thrown? For example
int foo()
{
int *p=new int[100];
int a, b;
// many codes here
FILE *fp;
fp=fopen("C:\myfile","r");
//many lines of code here
}
So when calling function calls foo(), what happens if file open failure
occurs in the middle? How about variable a, b, p? I was asked this question
during an interview, didn't know the right answer. | p***o 发帖数: 1252 | 2 Sounds like they are asking for C++ exception safety
http://www.boost.org/community/exception_safety.html
http://www2.research.att.com/~bs/except.pdf
if
【在 d****i 的大作中提到】 : We know that try catch block does the exception handling in C++. But what if : we don't use try catch block if an exception is thrown? For example : int foo() : { : int *p=new int[100]; : int a, b; : : // many codes here : FILE *fp; : fp=fopen("C:\myfile","r");
| t****t 发帖数: 6806 | 3 fopen() is from libc, it doesn't throw any exception. if open fails, it
returns NULL.
on the other hand, if an exception is thrown (whatever reason) and is not
catched by catch block, terminate() will be called, which will in turn call
terminate handler set by set_terminate(). the default handler will call
abort().
if an exception is thrown and is not listed in exception-specification,
unexpected() will be called, which will in turn call unexpected handler set
by set_unexpected(). the default ha
【在 d****i 的大作中提到】 : We know that try catch block does the exception handling in C++. But what if : we don't use try catch block if an exception is thrown? For example : int foo() : { : int *p=new int[100]; : int a, b; : : // many codes here : FILE *fp; : fp=fopen("C:\myfile","r");
| d****i 发帖数: 4809 | 4 Thanks a lot. So another question is: if an exception is thrown by some code
but no catch block is written to catch this exception, what will happen to
the stack variable a,b and heap variable p? When terminate() is called, is p
automatically deleted? What about a and b?
call
set
【在 t****t 的大作中提到】 : fopen() is from libc, it doesn't throw any exception. if open fails, it : returns NULL. : on the other hand, if an exception is thrown (whatever reason) and is not : catched by catch block, terminate() will be called, which will in turn call : terminate handler set by set_terminate(). the default handler will call : abort(). : if an exception is thrown and is not listed in exception-specification, : unexpected() will be called, which will in turn call unexpected handler set : by set_unexpected(). the default ha
| p***o 发帖数: 1252 | 5 面试的人想问p和fp在不用try/catch的情况下怎么释放 ...
call
set
【在 t****t 的大作中提到】 : fopen() is from libc, it doesn't throw any exception. if open fails, it : returns NULL. : on the other hand, if an exception is thrown (whatever reason) and is not : catched by catch block, terminate() will be called, which will in turn call : terminate handler set by set_terminate(). the default handler will call : abort(). : if an exception is thrown and is not listed in exception-specification, : unexpected() will be called, which will in turn call unexpected handler set : by set_unexpected(). the default ha
| K*****n 发帖数: 65 | 6 Exception or not, objects on stack will always get destroyed. Create a
CSelfRelease class, then let CSelfRelease objects release p and fp. | y******e 发帖数: 359 | 7 I think this is related to "stack unwinding". When exception is thrown, the
stack unwinding happens which makes all variables on the stack released. But
the memory pointed by the original int* p is not released since it's
allocated on the heap.
So there is memory leak of 100*4 bytes. | o****b 发帖数: 31 | 8 I think this is correct
the
But
【在 y******e 的大作中提到】 : I think this is related to "stack unwinding". When exception is thrown, the : stack unwinding happens which makes all variables on the stack released. But : the memory pointed by the original int* p is not released since it's : allocated on the heap. : So there is memory leak of 100*4 bytes.
|
|