Void_ptr ++不起作用,但void_ptr + =

jak*_*inf 3 c++ pointers

我一直认为,指针递增/递减是这样的操作:

 new_ptr=old_ptr+sizeof(type)*count 
Run Code Online (Sandbox Code Playgroud)

所以使用int*它将是:

  old_ptr=0     //int *ptr=0
  count=1       //int count=1
  new_ptr=old_ptr+sizeof(int)*count = 0+4*1 = 0x0004  //ptr+=count; 
Run Code Online (Sandbox Code Playgroud)

并且void的大小= 0,因此使用+ =递增void_ptr不应该改变它.但我可能错了.其他的是++运算符,它会抛出错误.所以如果++抛出错误,为什么+ =也不抛出它?示例代码:

#include <stdio.h>
void tell(const char *s,int *i,void *v){
    printf("%s: \n int_ptr: %#x\n void_ptr: %#x\n",s,i,v);
}
int main(){
    int *int_ptr=0;
    void *void_ptr=0;
    tell("Before",int_ptr,void_ptr);
    int_ptr++;  //int_ptr=int_ptr+sizeof(int); = 0x 0004
    //void_ptr++;   //error: ISO C++ forbids incrementing a pointer of type 'void*'
    tell("After ++",int_ptr,void_ptr);
    int_ptr+=1; //int_ptr=int_ptr+sizeof(int)  =  0x0008
    void_ptr+=1;//void_ptr=void_ptr+sizeof(void)    WHY DOES THIS WORK AND ++ DOES NOT?!     = 0x0001 ?! should be 0x0000, because sizeof void = 0
    tell("After +=",int_ptr,void_ptr); //RESULT: void_ptr=0x1, so does that mean, that sizeof void is 1 and it is not 0
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

Before:
 int_ptr: 0
 void_ptr: 0
After ++:
 int_ptr: 0x4
 void_ptr: 0
After +=:
 int_ptr: 0x8
 void_ptr: 0x1
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下吗?

Ger*_*ald 5

无效指针的算法在C和C++中都是非法的.一些编译器具有允许它的扩展.

在这种情况下,GCC有一个指针算术扩展,允许对void指针和函数指针进行算术运算,这两者都是标准的非法指针.它将两者视为指向大小为1的类型.

基本上,这是一个兼容性攻击,你应该避免在新代码中.

为什么++运算符会抛出错误,当+ =没有时,我不确定.++曾经在GCC中使用此扩展.也许是这个版本的GCC中的一个错误,或者是对C++扩展的不一致应用.