简体   繁体   English

ISO C ++禁止在转换为c ++的旧代码中增加类型为'void *'的指针

[英]ISO C++ forbids incrementing a pointer of type ‘void*’ , in legacy code converted to c++

i know its old problem , but what will be the easy solution to keep the old logic of c app (legacy) that now converted to c++ ? 我知道它的旧问题,但是要保持现在转换为C ++的C应用程序(旧版)的旧逻辑,简单的解决方案是什么?

in c its working : 在其工作:

void *p;
void *response = malloc(60 * 81);

p = response ;
p+=4;

in g++ gives : ISO C++ forbids incrementing a pointer of type 'void*' update: 在g ++中给出:ISO C ++禁止递增类型为'void *'的指针更新:
if i change it to char* im getting this error: 如果我将其更改为char *,则会收到此错误消息:

char *p;
char *response = malloc(60 * 81);

error: invalid conversion from ‘void*’ to ‘char*’

also does char* can hold other types (basic ones ) like short , int , bool ? char *是否还可以容纳其他类型(基本类型),例如short,int,bool? this is why it is used in this legacy code , to hold diffident types , 这就是为什么在此旧版代码中使用它来保存不同类型的原因,

The simplest would be to cast the void * to char * . 最简单的方法是将void * char *char * Since ISO C also forbids void* arithmetic, gcc treats it as a char* as an extension. 由于ISO C也禁止使用void*算术,因此gcc将其视为char*作为扩展名。 See this for more details: http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Pointer-Arith.html#Pointer-Arith 详情请参见: http : //gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Pointer-Arith.html#Pointer-Arith

It's only working in gcc. 它仅在gcc中工作。 Pointer arthimetic on void * is undefined. 无效*上的指针关节未定义。 Gcc treats it like a char * in that case. 在这种情况下,Gcc会将其视为char *。 So the best way to fix your legacy code is to carefully change all those pointers to char *. 因此,修复遗留代码的最佳方法是仔细更改所有指向char *的指针。

The easiest might be to look for compiler options to change this behavior. 最简单的方法可能是寻找编译器选项来更改此行为。

The best is probably to change the type to char * , since that seems to match usage and intent. 最好的办法是将类型更改为char * ,因为这似乎与用法和意图匹配。

Porting from gnu C to C++ is non trivial. 从gnu C移植到C ++并非易事。 Arithmetic on void* is not C, it is an extension. void*上的算术不是C,而是扩展。

Porting such things to C++ should be done more carefully, in particular if the C code was not too proper from the start. 将这些东西移植到C ++时应该更加谨慎,特别是如果C代码从一开始就不太合适的话。 That data has an "intended" type, so you should use that type in C++ and not yet another second guess like char . 该数据具有“预期”类型,因此您应该在C ++中使用该类型,而不要再像char那样进行第二次猜测。 Obviously this was not thought to be a C string, doing += 4 for C strings makes not much sense. 显然,这不被认为是C字符串,对C字符串执行+= 4并没有多大意义。 So there is the assumption that the base type has a size of 4 , probably from the rest of the code you can guess how this has to be interpreted. 因此,假设基本类型的大小为4 ,可能从代码的其余部分中可以猜出该如何解释。

Once you have the proper type, use new[] to allocate the array. 一旦具有正确的类型,请使用new[]分配数组。 Don't use malloc in C++ if you can avoid it. 如果可以避免,请勿在C ++中使用malloc

由于“指针的算术”,如果您写p += 4则意味着: p += ((sizeof(void *) * 4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ISO C++ 禁止指针和整数之间的比较 [-fpermissive]| [C] - ISO C++ forbids comparison between pointer and integer [-fpermissive]| [C] 错误] ISO C++ 禁止指针和整数之间的比较 [-fpermissive] - Error] ISO C++ forbids comparison between pointer and integer [-fpermissive] ISO C++ 禁止声明没有类型的“int32” - ISO C++ forbids declaration of 'int32' with no type 在 C/C++ 上增加 void 指针和运算符优先级 - Incrementing a void pointer and operator precedence on C/C++ ISO C++ 禁止在 Arduino c 串行通信中比较指针和整数 [-fpermissive] 错误 - ISO C++ forbids comparison between pointer and integer [-fpermissive] error in Arduino c serial communication ISO C ++禁止比较指针和整数:C错误中的文件处理 - ISO C++ forbids comparison between pointer and integer : File handling in C error ISO C ++禁止在devc ++中比较指针和整数[-fpermissive]错误 - ISO C++ forbids comparison between pointer and integer [-fpermissive] error in devc++ 调用scanf()会给出错误“ ISO C ++禁止将其强制转换为用作左值的非引用类型” - Calling scanf() gives error “ISO C++ forbids cast to non-reference type used as lvalue” ISO C ++禁止可变大小的数组(编译错误) - ISO C++ forbids variable-size array (compilation error) 取消引用C ++中的void指针 - Dereferencing the void pointer in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM