由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++问题: 指针变量是哪里产生的?
相关主题
问个C++中重复删除指针的问题A C++ runtime type casting question
C++ Q87: What is wrong with this swap function? (转载)c++ pointer conversion question
不如各位高手挑个专题讲讲C++11吧C++的很多library到底是趋于集中,还是各个公司独自开发,各立山头
作为返回值得实参是用指针还是引用比较好?difference between: char** p and char*p[] ??
C++ Q05: pointer to constant variableC++ pointer problem
请教两道linux面试题目问一个关于C++指针的问题
大家觉得C++复杂在哪里?纯虚函数问题
C++菜问: 怎么这样也可以?请教一个const pointer的问题
相关话题的讨论汇总
话题: null话题: pointer话题: function话题: donothing话题: integer
进入Programming版参与讨论
1 (共1页)
b*****d
发帖数: 7166
1
class myc
{
...
public:
...
void donothing() {cout<<"I do nothing!"< };
int main()
{
myc *p=0;
p->donothing();
}
output:
I do nothing!
经我测试,default constructor, copy constructor, = assignment operator 都没
有被调用。而且指针为0,换成NULL也一样。但是却可以使用类的函数。
请问:p指向的object(这里其实都没有,因为是0)是怎么产生的?
l*********s
发帖数: 5409
2
under the hood, member function is really a normal function with a hidden
parameter bound to "this".
you passed in an invalid pointer, but it does not cause problem because you
don't use instance data. o.w, you will trigger runtime error.
g*********e
发帖数: 14401
3
只有access member variable的时候才用到p指针
g*********e
发帖数: 14401
4
只有access member variable的时候才用到p指针
b*****d
发帖数: 7166
5
谢谢!
我的问题是p指针变量的值应该是个地址。一般情况是个object的地址。但这里是0. 那
p是怎么找到member function的地址的?是系统产生了一个不完全的object交给p?还
是p可以直接在member function列表里找需要的函数,虽然p=0?

you

【在 l*********s 的大作中提到】
: under the hood, member function is really a normal function with a hidden
: parameter bound to "this".
: you passed in an invalid pointer, but it does not cause problem because you
: don't use instance data. o.w, you will trigger runtime error.

l*********s
发帖数: 5409
6
2nd explanation is partially correct as object creation is not involved at
all. but there is no such process of member function lookup. Non-virtual
function address is determined at compiling time.

【在 b*****d 的大作中提到】
: 谢谢!
: 我的问题是p指针变量的值应该是个地址。一般情况是个object的地址。但这里是0. 那
: p是怎么找到member function的地址的?是系统产生了一个不完全的object交给p?还
: 是p可以直接在member function列表里找需要的函数,虽然p=0?
:
: you

x********q
发帖数: 108
7
可以这么理解,编译器是指上把这个方程编译成了类似
blahblah7myc8donothing9Ev(myc*)的C方程。
C++是静态语言,编译时就已经通过p的namespace,declared type/class和function
name找到了上述方
程,然后把p做参数压栈,call blahblah7myc8donothing9Ev而已。
所以,并非通过p找到了member function,而是通过p的类型找到的该方程。非virtual
function,每个类的object并不保存member function的地址。

【在 b*****d 的大作中提到】
: 谢谢!
: 我的问题是p指针变量的值应该是个地址。一般情况是个object的地址。但这里是0. 那
: p是怎么找到member function的地址的?是系统产生了一个不完全的object交给p?还
: 是p可以直接在member function列表里找需要的函数,虽然p=0?
:
: you

t***t
发帖数: 6066
8
I would say this compiler sucks. it should catch such error instead of
letting it pass.
t****t
发帖数: 6806
9
catching such error requires runtime check. most of these kind of error, are
"no diagnostic required" by standard.

【在 t***t 的大作中提到】
: I would say this compiler sucks. it should catch such error instead of
: letting it pass.

t***t
发帖数: 6066
10
for this case, no runtime check is needed.
compiler should give warning whenever an integer is used as address and
assigned to a pointer. which means, pointer can only be get from new or from
another pointer. IMO, developer should never do it.
so such code will always get warning and alert the developer.
or, can set the compiler stricter, give error instead of warning.
i don't see the need to assign integer to pointer unless for very low level
programming.

are

【在 t****t 的大作中提到】
: catching such error requires runtime check. most of these kind of error, are
: "no diagnostic required" by standard.

相关主题
请教两道linux面试题目A C++ runtime type casting question
大家觉得C++复杂在哪里?c++ pointer conversion question
C++菜问: 怎么这样也可以?C++的很多library到底是趋于集中,还是各个公司独自开发,各立山头
进入Programming版参与讨论
t****t
发帖数: 6806
11
you do know null pointer is traditionally expressed by number 0, don't you?
in fact, literal integer 0 is the only way to express null pointer. NULL is
simply a macro form of literal integer 0. you want a warning everytime null
pointer is used?
of course, in c++11, there is this new nullptr keyword.

from
level

【在 t***t 的大作中提到】
: for this case, no runtime check is needed.
: compiler should give warning whenever an integer is used as address and
: assigned to a pointer. which means, pointer can only be get from new or from
: another pointer. IMO, developer should never do it.
: so such code will always get warning and alert the developer.
: or, can set the compiler stricter, give error instead of warning.
: i don't see the need to assign integer to pointer unless for very low level
: programming.
:
: are

t***t
发帖数: 6066
12
this can be enforced by compiler.
if assign integer 0, give warning.
if assign by macro null, pass.
and actually this can only be done by compiler, not run time, because
pointer is integer at runtime. only compiler knows that it's integer or
pointer.

?
is
null

【在 t****t 的大作中提到】
: you do know null pointer is traditionally expressed by number 0, don't you?
: in fact, literal integer 0 is the only way to express null pointer. NULL is
: simply a macro form of literal integer 0. you want a warning everytime null
: pointer is used?
: of course, in c++11, there is this new nullptr keyword.
:
: from
: level

t****t
发帖数: 6806
13
basically you want to mix PP stage with compile stage.
i think this is generally a very bad idea, unless absolutely necessary. NULL
(not null) or 0 is simply the same token, i think there is more important t
hings to warn about, e.g. multi-value-change-between-sequence-point type of
error; this is implementable and worth the trouble.
distinguishing null or 0 is not worth it. besides there is this new nullptr
keyword now.

【在 t***t 的大作中提到】
: this can be enforced by compiler.
: if assign integer 0, give warning.
: if assign by macro null, pass.
: and actually this can only be done by compiler, not run time, because
: pointer is integer at runtime. only compiler knows that it's integer or
: pointer.
:
: ?
: is
: null

n******t
发帖数: 4406
14
as long as you know how things work, there is no problem for this kind of
usage. And if you do not know how things work, your C++ will definitely suck.

【在 t***t 的大作中提到】
: I would say this compiler sucks. it should catch such error instead of
: letting it pass.

f*******n
发帖数: 12623
15
Technically according to the standard, it's undefined behavior. Undefined
behavior means it can do anything, crash or not, or blow up your computer.
Whatever.
In practice, non-virtual function calls are looked up at compile time in all
compilers. The pointer is not used in determining which function to call.

【在 b*****d 的大作中提到】
: class myc
: {
: ...
: public:
: ...
: void donothing() {cout<<"I do nothing!"<: };
: int main()
: {
: myc *p=0;

b*******s
发帖数: 5216
16
C++的类的内存布局就是这样的,和实例无关的单独有一块地方,是这个类共有的

【在 b*****d 的大作中提到】
: 谢谢!
: 我的问题是p指针变量的值应该是个地址。一般情况是个object的地址。但这里是0. 那
: p是怎么找到member function的地址的?是系统产生了一个不完全的object交给p?还
: 是p可以直接在member function列表里找需要的函数,虽然p=0?
:
: you

d****n
发帖数: 1241
17

前段时间看见CLANG的邮件列表里有人讨论,也许不用多久,会加入到clang的
undefined behavior sanitizer里了。

【在 t****t 的大作中提到】
: basically you want to mix PP stage with compile stage.
: i think this is generally a very bad idea, unless absolutely necessary. NULL
: (not null) or 0 is simply the same token, i think there is more important t
: hings to warn about, e.g. multi-value-change-between-sequence-point type of
: error; this is implementable and worth the trouble.
: distinguishing null or 0 is not worth it. besides there is this new nullptr
: keyword now.

t****t
发帖数: 6806
18
gcc already has this warning option, although it is said not 100% accurate.

【在 d****n 的大作中提到】
:
: 前段时间看见CLANG的邮件列表里有人讨论,也许不用多久,会加入到clang的
: undefined behavior sanitizer里了。

x****u
发帖数: 44466
19
我感觉clang的sanitizer是一种技术上很成功,但实际应用可能带来灾难的东西。
C++代码追求的不是发现的bug数多,而是残留的bug少,很多santizer实际上是鼓励危
险方式编程,并且给予程序员虚假的安全感。

【在 t****t 的大作中提到】
: gcc already has this warning option, although it is said not 100% accurate.
d***a
发帖数: 13752
20
类似于你写了个如下的function和调用。
void donothing(myc *p)
{
cout << "I do nothing!" << endl;
}
int main()
{
myc *p = NULL;
donothing(p);
}

【在 b*****d 的大作中提到】
: 谢谢!
: 我的问题是p指针变量的值应该是个地址。一般情况是个object的地址。但这里是0. 那
: p是怎么找到member function的地址的?是系统产生了一个不完全的object交给p?还
: 是p可以直接在member function列表里找需要的函数,虽然p=0?
:
: you

1 (共1页)
进入Programming版参与讨论
相关主题
请教一个const pointer的问题C++ Q05: pointer to constant variable
C++(非VC++) 删除链表时如何对指针操作? 在线等回复!谢谢!请教两道linux面试题目
这段code有啥问题?大家觉得C++复杂在哪里?
Question about type conversion (转载)C++菜问: 怎么这样也可以?
问个C++中重复删除指针的问题A C++ runtime type casting question
C++ Q87: What is wrong with this swap function? (转载)c++ pointer conversion question
不如各位高手挑个专题讲讲C++11吧C++的很多library到底是趋于集中,还是各个公司独自开发,各立山头
作为返回值得实参是用指针还是引用比较好?difference between: char** p and char*p[] ??
相关话题的讨论汇总
话题: null话题: pointer话题: function话题: donothing话题: integer