愚蠢无效** [英] stupid void**

查看:60
本文介绍了愚蠢无效**的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C和旧的C ++编译器中,你可以编写这样的代码


void a(void ** pp); //声明一个函数,该函数接收指向void的指针。

struct I {}; //一些结构。

I * i1,** i2; //某种我。


a(& i1); //这是有效的

a(i2); //这也有效。

a(i1); //错误:间接错误级别

a(& i2); //错误:不正确的间接级别


现在,c ++编译器不再喜欢void *了。这意味着我们绝对必须将静态转换为void **因为c ++编译器太高而强大,只能通过单独的间接检查等级让空虚**过去。


a( (无效**)及I1); //这是有效的

a((void **)i2); //这也有效。

a((void **)i1); //哎呀

a((void **)& i2); //哎呀


对于c ++帮我们避免错误。

间接检查很有用。

In C and in older C++ compilers, you could write code like this

void a(void** pp); // declare a function that takes a pointer to a pointer-to-void.
struct I {}; // some struct.
I *i1,**i2; // some kinds of i.

a(&i1); // this works
a(i2); // this also worked.
a(i1); // error: incorrect levels of indirection
a(&i2); // error: incorrect levels of indirection

Now, c++ compilers just don''t like void* anymore. meaning we absolutely have to static cast to a void** because c++ compiler are too damn high and mighty to let a void** slip past on a mere levels of indirection check.

a((void**)&i1); // this works
a((void**)i2); // this also worked.
a((void**)i1); // oops
a((void**)&i2); // oops

Yay for c++ helping us avoid bugs.
indirection checking was useful damnit.

推荐答案

Chris Becke写道:
Chris Becke wrote:

在C和旧的C ++编译器中,你可以编写这样的代码


void a(void ** pp); //声明一个函数,该函数接收指向void的指针。

struct I {}; //一些结构。

I * i1,** i2; //某种我。


a(& i1); //这是有效的

a(i2); //这也有效。

a(i1); //错误:间接错误级别

a(& i2); //错误:间接错误级别
In C and in older C++ compilers, you could write code like this

void a(void** pp); // declare a function that takes a pointer to a pointer-to-void.
struct I {}; // some struct.
I *i1,**i2; // some kinds of i.

a(&i1); // this works
a(i2); // this also worked.
a(i1); // error: incorrect levels of indirection
a(&i2); // error: incorrect levels of indirection



不,你不能。既不是C语言,也不是C ++语言。这些都没有工作。

显然,你一直在使用一些愚蠢的旧破坏编译器和一堆你自己的黑客攻击,并且出于某种原因误认为编译器'傻'

怪异的C和/或C ++语言特性。

No, you couldn''t. Neither in C nor in C++. None of these ever "worked".
Apparently, you''ve been using some silly old broken compiler and a bunch
of your own hacks, and for some reason mistook that compiler''s silly
quirks for C and/or C++ language features.


现在,c ++编译器就是不要喜欢void *了。这意味着我们绝对不会因为c ++编译器太高而且需要静态转换为空虚**因为c ++编译器太高了而且很难让一个空白**在仅仅的水平上滑过

间接检查。
Now, c++ compilers just don''t like void* anymore. meaning we absolutely
have to static cast to a void** because c++ compiler are too damn high
and mighty to let a void** slip past on a mere levels of
indirection check.



嗯?你似乎在谈论''无效**''。在这里你突然提到'b $ b'提到''无效*'',这与''无效**'很少有关。


BTW," ;静态转换为无效**?有问题的演员阵容无法通过''static_cast''执行
。如果你的编译器允许它,它可能意味着你继续坚持使用一些相当愚蠢的编译器。

Huh? You seemed to be talking about ''void**''. And here you suddenly
mention ''void*'', which has very little to do with ''void**''.

BTW, "static cast to void**"? The casts in question cannot be performed
by ''static_cast''. If your compiler allows it, it probably means that you
are continuing to stick with some rather silly compiler.


a((void **) )及I1); //这是有效的

a((void **)i2); //这也有效。

a((void **)i1); //哎呀

a((void **)& i2); //哎呀


对于c ++,我们可以帮助我们避免错误。

间接检查是有用的。
a((void**)&i1); // this works
a((void**)i2); // this also worked.
a((void**)i1); // oops
a((void**)&i2); // oops

Yay for c++ helping us avoid bugs.
indirection checking was useful damnit.



所有这四个电话都是错误,就像原来的四个电话一样。

是错误从某种意义上说,结果指针无论如何都不能立即用作''void **''指针。现在使用的编译器现在看起来更好,因为它拒绝编译无用的

原始代码。这应该是让你想到你正在做什么的事情。相反,你决定在它上面拍一个演员来扫除地毯下的错误。好吧,难怪你从中得到的是一堆

一堆哎呀!


-

祝你好运,

Andrey Tarasevich

All these four calls are "bugs", just like the original four calls were.
The are "bugs" in a sense that that the resultant pointer cannot be
immediately used as a ''void**'' pointer anyway. The compiler you are
using now looks better because it refuses to compile the useless
original code. This should have been something to make you think about
what you are doing. Instead, you decided to slap a cast on it to sweep
the error under the carpet. Well, no wonder all you get from it is a
bunch of "oops"!

--
Best regards,
Andrey Tarasevich


Chris Becke写道:
Chris Becke wrote:

在C和较旧的C ++编译器,你可以编写这样的代码


void a(void ** pp); //声明一个函数,它接收指向

指针到void的指针。

struct I {}; //一些结构。

I * i1,** i2; //某种我。


a(& i1); //这是有效的

a(i2); //这也有效。

a(i1); //错误:间接错误级别

a(& i2); //错误:不正确的间接级别


现在,c ++编译器不再喜欢void *了。这意味着我们绝对不会因为c ++编译器太高而且需要静态转换为空虚**因为c ++编译器太高了而且很难让一个空白**在仅仅的水平上滑过间接

检查。


a((void **)& i1); //这是有效的

a((void **)i2); //这也有效。

a((void **)i1); //哎呀

a((void **)& i2); //哎呀


对于c ++,我们可以帮助我们避免错误。
In C and in older C++ compilers, you could write code like this

void a(void** pp); // declare a function that takes a pointer to a
pointer-to-void.
struct I {}; // some struct.
I *i1,**i2; // some kinds of i.

a(&i1); // this works
a(i2); // this also worked.
a(i1); // error: incorrect levels of indirection
a(&i2); // error: incorrect levels of indirection

Now, c++ compilers just don''t like void* anymore. meaning we absolutely
have to static cast to a void** because c++ compiler are too damn high
and mighty to let a void** slip past on a mere levels of indirection
check.

a((void**)&i1); // this works
a((void**)i2); // this also worked.
a((void**)i1); // oops
a((void**)&i2); // oops

Yay for c++ helping us avoid bugs.



是的,确实,禁止它可以消除错误。如果T **隐式转换为
到void **,你可以做


int * p;

void ** pp =& p; //错误,但你在争论这是允许的

char c;

* pp =& c; //如果允许,则相当于

// reinterpret_cast< void *&(p)=& c

* p = 1234; // oops,p指向一个字母


没有任何演员。另外,如果标准要求编译器允许

这个,它必须对所有指针类型使用相同的表示,而不是当前它可以使用更少的指针类型指向类型指针的位

,对齐要求比char'更严格。

Yes, indeed, disallowing it eliminates bugs. If T** implicitly converted
to void**, you could do

int* p;
void** pp = &p; // error, but you''re arguing that this be allowed
char c;
*pp = &c; // if allowed, would be equivalent to
// reinterpret_cast<void*&(p) = &c
*p = 1234; // oops, p points to a char

without any casting. Also, if the standard required a compiler to allow
this, it would have to use the same representation for all pointer types,
rather than as currently where it can use fewer bits for pointers to types
with alignment requirements more strict than char''s.


11月12日,10:21 * pm," ; Chris Becke < chris.be ... @ gmail.comwrote:
On Nov 12, 10:21*pm, "Chris Becke" <chris.be...@gmail.comwrote:

在C和旧的C ++编译器中,你可以编写这样的代码


* void a(void ** pp); //声明一个函数,该函数接收指向void的指针。

* struct I {}; //某些结构。

* I * i1,** i2; //某种我的。


* a(& i1); * //这是有效的

* a(i2); * * //这也有效。

* a(i1); * * //错误:间接错误级别

* a(& i2); * //错误:间接错误级别
In C and in older C++ compilers, you could write code like this

* void a(void** pp); // declare a function that takes a pointer to a pointer-to-void.
* struct I {}; // some struct.
* I *i1,**i2; // some kinds of i.

* a(&i1); *// this works
* a(i2); * * // this also worked.
* a(i1); * * // error: incorrect levels of indirection
* a(&i2); *// error: incorrect levels of indirection



只需备份安德烈所说的内容;这些都没有在任何标准版本的C或C ++中使用过。
。无效**

只能指向一个空白*,而不是其他任何东西。

Just backing up what Andrey said; none of these ever
worked in any standard version of C or C++. A void**
can only point to exactly a void* , not anything else.


这篇关于愚蠢无效**的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆