由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - A C++ exception question
相关主题
lex/yacc如何reset buffer?How to write a VB Macro to convert text to hyperlink in Excel
请教一个用matlab tcpip函数的问题急! Python 如何从文件读取数据(整数) ~~在线等
fprintf in C\C++Help! Read random number of lines in a input file.
Path with non-ascii character 问一个小程序python得
New C++ programmer, need to ask a I/O file read questionostream& operator << (ostream& s, int cnt) error
a C/C++ fopen mode questiondelete files in windows
C++的smart pointer注定是个二流的东西还是awk牛B
请问怎样在C/C++里取联机的windows phone 里的图片? (转载)你们吵着要javascript的: How to delete the last line in the file in node.js?
相关话题的讨论汇总
话题: exception话题: c++话题: thrown话题: int话题: catch
进入Programming版参与讨论
1 (共1页)
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.

1 (共1页)
进入Programming版参与讨论
相关主题
你们吵着要javascript的: How to delete the last line in the file in node.js?New C++ programmer, need to ask a I/O file read question
help with PHP programming!!! (转载)a C/C++ fopen mode question
C语言程序静态库和动态库的创建及其应用C++的smart pointer注定是个二流的东西
请问如何恢复正常的IO?请问怎样在C/C++里取联机的windows phone 里的图片? (转载)
lex/yacc如何reset buffer?How to write a VB Macro to convert text to hyperlink in Excel
请教一个用matlab tcpip函数的问题急! Python 如何从文件读取数据(整数) ~~在线等
fprintf in C\C++Help! Read random number of lines in a input file.
Path with non-ascii character 问一个小程序python得
相关话题的讨论汇总
话题: exception话题: c++话题: thrown话题: int话题: catch