由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 输入参数的检查应该归caller还是callee?
相关主题
what's wrong with this C++ code?int F::*x = &F::x是什么意思?
how to get runtime caller function?c++ 里用到pointer 的地方我们尽可能用smart pointer吗?
Why do I need to use "plain" pointer?不如各位高手挑个专题讲讲C++11吧
is smart_ptr really that good?Go adopts JavaScript’s idea of semicolon insertion
pointer overflowC++一问
C++ Q05: pointer to constant variableC++ OO approach to use multi-dim array for HPC
C++的exception大家常用吗?C++ question
C++ Q93 - Q95 (转载)最expressive的三大语言
相关话题的讨论汇总
话题: c++话题: caller话题: callee话题: pointer话题: 检查
进入Programming版参与讨论
1 (共1页)
q****x
发帖数: 7404
1
如下代码,检查空指针和清空集合的责任应该是caller还是callee?按garbage in
garbage out的原则,这两个语句都可以省掉,caller自己负责。但这样好吗?
大家倾向哪种风格?理由是?
void GetNames(set* names) {
if (names == nullptr) return;
name.clear();
for (const string& name = stream.get()) {
names.insert(name);
}
return;
}
w**z
发帖数: 8232
2
callee 我是一定会check, 保护你的程序。至于是否清空,看你怎么定义你的接口。

【在 q****x 的大作中提到】
: 如下代码,检查空指针和清空集合的责任应该是caller还是callee?按garbage in
: garbage out的原则,这两个语句都可以省掉,caller自己负责。但这样好吗?
: 大家倾向哪种风格?理由是?
: void GetNames(set* names) {
: if (names == nullptr) return;
: name.clear();
: for (const string& name = stream.get()) {
: names.insert(name);
: }
: return;

p***o
发帖数: 1252
3
For performance, use assert. For robustness, use exception.
Ignore it as your code does only if the spec requests that explicitly.

【在 q****x 的大作中提到】
: 如下代码,检查空指针和清空集合的责任应该是caller还是callee?按garbage in
: garbage out的原则,这两个语句都可以省掉,caller自己负责。但这样好吗?
: 大家倾向哪种风格?理由是?
: void GetNames(set* names) {
: if (names == nullptr) return;
: name.clear();
: for (const string& name = stream.get()) {
: names.insert(name);
: }
: return;

q****x
发帖数: 7404
4
你说了三种检查策略。但这里的问题是该不该检查。

【在 p***o 的大作中提到】
: For performance, use assert. For robustness, use exception.
: Ignore it as your code does only if the spec requests that explicitly.

a*****g
发帖数: 19398
5
对。

【在 p***o 的大作中提到】
: For performance, use assert. For robustness, use exception.
: Ignore it as your code does only if the spec requests that explicitly.

d****i
发帖数: 4809
6
我认为不用在callee里面检查,C++里面这种传指针进函数的用的太多了,要是每个都
像你这么检查的话overhead太大,code也看着丑陋。应该由caller来检查。

【在 q****x 的大作中提到】
: 如下代码,检查空指针和清空集合的责任应该是caller还是callee?按garbage in
: garbage out的原则,这两个语句都可以省掉,caller自己负责。但这样好吗?
: 大家倾向哪种风格?理由是?
: void GetNames(set* names) {
: if (names == nullptr) return;
: name.clear();
: for (const string& name = stream.get()) {
: names.insert(name);
: }
: return;

g*********e
发帖数: 14401
7
debugger build用assert检查,release build不检查
d****n
发帖数: 12461
8
看是公共接口还是私有接口。私有的一般都不检查,留下debug想象空间。公共的做好
定义内的检查,留下记录。

【在 q****x 的大作中提到】
: 如下代码,检查空指针和清空集合的责任应该是caller还是callee?按garbage in
: garbage out的原则,这两个语句都可以省掉,caller自己负责。但这样好吗?
: 大家倾向哪种风格?理由是?
: void GetNames(set* names) {
: if (names == nullptr) return;
: name.clear();
: for (const string& name = stream.get()) {
: names.insert(name);
: }
: return;

Y**G
发帖数: 1089
9
If you write public API, you should not trust caller. Internal api is
different story.

【在 q****x 的大作中提到】
: 如下代码,检查空指针和清空集合的责任应该是caller还是callee?按garbage in
: garbage out的原则,这两个语句都可以省掉,caller自己负责。但这样好吗?
: 大家倾向哪种风格?理由是?
: void GetNames(set* names) {
: if (names == nullptr) return;
: name.clear();
: for (const string& name = stream.get()) {
: names.insert(name);
: }
: return;

p***o
发帖数: 1252
10
啥时候都应该啊,谁写code都有睡着的时候,指望所有的caller都负责是不现实的。

【在 q****x 的大作中提到】
: 你说了三种检查策略。但这里的问题是该不该检查。
相关主题
C++ Q05: pointer to constant variableint F::*x = &F::x是什么意思?
C++的exception大家常用吗?c++ 里用到pointer 的地方我们尽可能用smart pointer吗?
C++ Q93 - Q95 (转载)不如各位高手挑个专题讲讲C++11吧
进入Programming版参与讨论
p***o
发帖数: 1252
11
That's why you need to use assert. And now you can even use prefast
for static analysis in VC++.

【在 d****i 的大作中提到】
: 我认为不用在callee里面检查,C++里面这种传指针进函数的用的太多了,要是每个都
: 像你这么检查的话overhead太大,code也看着丑陋。应该由caller来检查。

b*******s
发帖数: 5216
12
这是个相当好的问题

【在 q****x 的大作中提到】
: 如下代码,检查空指针和清空集合的责任应该是caller还是callee?按garbage in
: garbage out的原则,这两个语句都可以省掉,caller自己负责。但这样好吗?
: 大家倾向哪种风格?理由是?
: void GetNames(set* names) {
: if (names == nullptr) return;
: name.clear();
: for (const string& name = stream.get()) {
: names.insert(name);
: }
: return;

k**********g
发帖数: 989
13
Try to write code that will eliminate or avoid some of the bugs in the first
place.
For example, if the argument is required (in this case), don't pass in a
pointer. Use a C++ reference.
void GetNames ( set < string > & names)
{
names.clear();
for (...)
{
names.insert(...);
}
}
If use of pointer argument is necessary, then it is necessary to perform
null pointer check in most cases.
The reason is that, in other languages (Java, C#, anything else), a null
pointer will be caught, and will generate a stack trace. In C++, the
consequences are truly undefined.
http://xkcd.com/292/
Only in the case of a C++ class's private method can the null pointer check
be skipped, and only if someone can methodically prove that the null pointer
check cannot happen.
Most C++ code should have logging. Compared to other languages, the design
of C++ logging must take into account the memory corruption issue. This
means C++ logging should either be (1) flushing more frequently, because
memory corruption could destroy the last moment of logs that lead up to the
crash. (2) or, send the log data over a shared memory map to a second
process, which will persist the data.
Anyway, C++ is different, like a surface-to-air missile.
g*****y
发帖数: 7271
14
I do the check even in release build since most of them are super
cheap.

【在 p***o 的大作中提到】
: For performance, use assert. For robustness, use exception.
: Ignore it as your code does only if the spec requests that explicitly.

q****x
发帖数: 7404
15
nullptr check probably is always necessary, as it's critical and cheap.
container clear, on the other hand, is debatable. in this sample code,
actually i think skipping clear is even a bug.

【在 g*****y 的大作中提到】
: I do the check even in release build since most of them are super
: cheap.

1 (共1页)
进入Programming版参与讨论
相关主题
最expressive的三大语言pointer overflow
c++ define 一问C++ Q05: pointer to constant variable
有什么工具可以检查内存泄漏么C++的exception大家常用吗?
一个C#使用C++.NET类库的问题C++ Q93 - Q95 (转载)
what's wrong with this C++ code?int F::*x = &F::x是什么意思?
how to get runtime caller function?c++ 里用到pointer 的地方我们尽可能用smart pointer吗?
Why do I need to use "plain" pointer?不如各位高手挑个专题讲讲C++11吧
is smart_ptr really that good?Go adopts JavaScript’s idea of semicolon insertion
相关话题的讨论汇总
话题: c++话题: caller话题: callee话题: pointer话题: 检查