天道酬勤,学无止境

pointer-conversion

C 函数指针类型兼容性(C function pointer type compatibility)

问题 编写一个与函数回调一起使用的库,我经常将函数指针类型转换(和调用)到具有相同调用约定和相同签名的类型,但有一个例外:它们的参数指向不同的类型(所有数据),或空指针。 最近,我发现它可能不那么安全,根据这个:https://stackoverflow.com/a/14044244/3079266 基本上按照我的理解,如果参数的类型是兼容的,那说明函数指针的类型也是兼容的,应该没有问题。 现在,我有3个问题。 第一:这是否意味着,由于指向不同类型的指针在技术上不兼容,我正在做的事情会导致未定义的行为? 第二:我在哪里(在什么架构上)可以摆脱它? 其中包括 Windows x86 或 x64 版本吗? 第三:我在哪里不能逃脱它? 回答1 是的,这本身就是 UB 您可能可以在未优化的非调试版本中摆脱这种情况。 优化器可能会利用指针类型来确定两个指针是否可以别名,如果您对实际类型撒谎,该逻辑可能会失败。 调试版本当然可以直接检查是否存在类型不匹配。 Windows 不是编译器,所以这不是一个明智的问题。 常见的 Windows 编译器会优化构建,尤其是 ICC 具有高级优化功能。

2021-09-17 06:36:37    分类:技术分享    c   pointers   function-pointers   undefined-behavior   pointer-conversion

C function pointer type compatibility

Writing a library that works with function callbacks, I've frequently type-casted (and called) function pointers to types with the same calling convention and same signatures, but with one exception: they had parameters pointing to different types (all data), or void pointers. Recently, I discovered that it might not be that safe, according to this: https://stackoverflow.com/a/14044244/3079266 Basically, as I understood it, if the types of the arguments are compatible, that means the function pointer types are also compatible, and there should be no problem. Now, I've got 3 questions. First

2021-09-05 22:29:04    分类:问答    c   pointers   function-pointers   undefined-behavior   pointer-conversion

通过引用传递常量指针(passing const pointer by reference)

问题 我很困惑为什么以下代码无法编译 int foo(const float* &a) { return 0; } int main() { float* a; foo(a); return 0; } 编译器给出错误为: 错误:来自“float*”类型的表达式的“const float*&”类型的引用初始化无效 但是当我尝试在 foo 中不通过引用传递时,它编译得很好。 我认为无论我是否通过引用传递,它都应该显示相同的行为。 谢谢, 回答1 因为它不是类型安全的。 考虑: const float f = 2.0; int foo(const float* &a) { a = &f; return 0; } int main() { float* a; foo(a); *a = 7.0; return 0; } 任何非const引用或指针在指向类型中都必须是不变的,因为非const指针或引用支持读取(协变操作)和写入(逆变操作)。 const必须首先从最大的间接级别添加。 这会起作用: int foo(float* const &a) { return 0; } int main() { float* a; foo(a); return 0; }

2021-06-03 14:42:22    分类:技术分享    c++   reference   constants   implicit-conversion   pointer-conversion

passing const pointer by reference

I am confused that why following code is not able to compile int foo(const float* &a) { return 0; } int main() { float* a; foo(a); return 0; } Compiler give error as: error: invalid initialization of reference of type 'const float*&' from expression of type 'float*' but when I try to pass without by reference in foo, it is compiling fine. I think it should show same behavior whether I pass by reference or not. Thanks,

2021-05-17 19:13:10    分类:问答    c++   reference   constants   implicit-conversion   pointer-conversion

Is it legal to implement inheritance in C by casting pointers between one struct that is a subset of another rather than first member?

Now I know I can implement inheritance by casting the pointer to a struct to the type of the first member of this struct. However, purely as a learning experience, I started wondering whether it is possible to implement inheritance in a slightly different way. Is this code legal? #include <stdio.h> #include <stdlib.h> struct base { double some; char space_for_subclasses[]; }; struct derived { double some; int value; }; int main(void) { struct base *b = malloc(sizeof(struct derived)); b->some = 123.456; struct derived *d = (struct derived*)(b); d->value = 4; struct base *bb = (struct base*)(d)

2021-04-28 09:05:50    分类:问答    c   inheritance   language-lawyer   flexible-array-member   pointer-conversion

Conversion from Derived** to Base**

I was reading this and unfortunately could not understand in depth why the compiler does not allow conversion from Derived** to Base**. Also I have seen this which gives no more info than the parashift.com's link. EDIT: Let us analyze this code line by line: Car car; Car* carPtr = &car; Car** carPtrPtr = &carPtr; //MyComment: Until now there is no problem! Vehicle** vehiclePtrPtr = carPtrPtr; // This is an error in C++ //MyComment: Here compiler gives me an error! And I try to understand why. //MyComment: Let us consider that it was allowed. So what?? Let's go ahead! NuclearSubmarine sub

2021-03-31 17:46:39    分类:问答    c++   inheritance   pointers   c++-faq   pointer-conversion