BLOT:C++ 具有隐式转换,我正在寻找一种方法来防止它。
让我举一个例子,下面的代码片段:
#include <iostream>
int incrementByOne(int i) {
return ++i;
}
int main()
{
bool b = true;
std::cout << incrementByOne(b) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它将输出:2
我怎样才能防止这种隐式转换,并且即使在运行时也严格只采用 int 作为参数?
我能想到的一种方法是重载函数。所以新代码将如下所示:
#include <iostream>
int incrementByOne(int i) {
return ++i;
}
int incrementByOne(bool) {
std::cerr << "Please provide integer as argument" << std::endl;
exit(0);
}
int incrementByOne(char) {
std::cerr << "Please provide integer as argument" << std::endl;
exit(0);
}
int main()
{
bool b = true;
std::cout << incrementByOne(b) << …Run Code Online (Sandbox Code Playgroud) 今天我开始学习C,我陷入了函数指针的困境。这是我的代码:
\n主要.c:
\n\n#include <stdio.h>\n\nint sumOfElements(int *arr, int arr_elements);\n\nint main()\n{\n\n int (*ptr)(int,int) = NULL;\n ptr = sumOfElements;\n int a[] = {128, 64, 32, 16, 8, 4, 2, 1};\n printf("Total of price is: %d", ptr(a, 8));\n\n}\n\nint sumOfElements(int *arr, int arr_elements)\n{\n int k =0;\n int total;\n for(;k < arr_elements;k++)\n {\n total += arr[k];\n }\n return total;\n}\n\nRun Code Online (Sandbox Code Playgroud)\n我想做的是访问函数中数组的元素sumOfElements。当我正常调用它时,一切都会顺利。当我尝试使用 时function pointer,编译器之前会向我抛出一些警告,然后是Segmentation Fault错误:
main.c:17:9: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] \nmain.c:19:41: warning: passing argument 1 …Run Code Online (Sandbox Code Playgroud) c arrays function-pointers implicit-conversion function-declaration
我对二维数组的了解:
但这对我来说没有意义 A 值如何与 *A 相同,有人可以告诉我这是如何在内存中工作的,我知道这是正确的,但我无法向自己解释
c pointers pointer-arithmetic multidimensional-array implicit-conversion
我正在读一本关于 C 语言安全编码的书,我迷失的一点是强制转换和隐式转换之间的区别。请看一下下面的代码片段:
unsigned char a = 1;
unsigned char b = 2;
int c = b - a ; //presumably implicit conversion
printf("%u" , (int)a - b ); // presumably casting
Run Code Online (Sandbox Code Playgroud)
(int) a-b 我在 Google 上找到了答案,但它们都与 C# 甚至 C++ 有关,但不是普通的 C ,所以有人可以解释一下这样做和分配给另一个这样的变量有什么区别吗int c = a - b
这里究竟发生了什么?提前致谢 。
我写了这个函数:
int einmaleins(int zahl, int *arr){
int e;
for(int i = 1; i<11; i++){
e = zahl*i;
arr[i-1]=e;
}
return 0;
}
int main(){
int arr[10] = {0};
einmaleins(2, &arr[10]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是指针数组,但是当我启动程序时,我收到以下消息:*** 检测到堆栈粉碎 ***:终止
我真的不知道该怎么办。
我无法理解为什么编译器在分配不兼容的指针时不发出警告(?:),但在直接分配时发出警告。
在这种情况下编译器会发出警告:
\n\n\ntest.c:在函数 \xe2\x80\x98main\xe2\x80\x99 中:test.c:8:4:警告:分配给\n\xe2\x80\x98uint32_t *\xe2\x80\x99 {aka \xe2 \x80\x98unsigned int *\xe2\x80\x99} 来自不兼容的指针类型\n\xe2\x80\x98uint32_t **\xe2\x80\x99 {aka \xe2\x80\x98unsigned int **\xe2\x80\x99 } [-Win兼容指针类型]\n8 | a = 数组;\n| ^
\n
#include <stdint.h>\n#include <stdlib.h>\n\nint main(void)\n{\n uint32_t *array[10], *a;\n a = array;\n}\nRun Code Online (Sandbox Code Playgroud)\n以下情况不会发出警告:
\n#include <stdint.h>\n#include <stdlib.h>\n\nint main(void)\n{\n int b = 8;\n uint32_t *array[10], *a;\n\n a = (b >= 8) ? array : malloc(8);\n}\nRun Code Online (Sandbox Code Playgroud)\n环境:
\nGcc version 9.3.0\nUbuntu 20.04\ncompilation cmd: gcc test.c -o test.out\nRun Code Online (Sandbox Code Playgroud)\n c gcc conditional-operator void-pointers implicit-conversion
我试图将数组传递给方法。我尝试了以下方法:
\n我不明白为什么最后一个(#3)发出警告。&ARRAY_NAME 在 memset、memcpy 等中工作时没有警告。为什么它在自定义方法中存在问题?
\n警告信息:
\nfunctionTest.c:35:11: warning: passing argument 1 of \xe2\x80\x98func2\xe2\x80\x99 from incompatible pointer type [-Wincompatible-pointer-types]\n 35 | func2(&temp, ARRAY_SIZE);\n | ^~~~~\n | |\n | unsigned char (*)[200]\nfunctionTest.c:8:27: note: expected \xe2\x80\x98unsigned char *\xe2\x80\x99 but argument is of type \xe2\x80\x98unsigned char (*)[200]\xe2\x80\x99\n 8 | void func2(unsigned char* buf, int length)\nRun Code Online (Sandbox Code Playgroud)\n代码
\n#include <stdio.h>\n#include <stdint.h>\n#include <string.h>\n\n\n#define ARRAY_SIZE 200\n\nvoid …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
void f(auto& i, auto& j)
{
static_assert(/* SOMETHING */, "");
// function body here...
}
Run Code Online (Sandbox Code Playgroud)
我希望该/* SOMETHING */部件检查以下代码是否编译(考虑所有标准规则,如隐式转换规则):
i += j;
Run Code Online (Sandbox Code Playgroud)
我试过了:
sizeof(std::declval<decltype(i)>() += std::declval<decltype(j)>());
Run Code Online (Sandbox Code Playgroud)
但它失败了.
这样做的正确方法是什么?
编辑:我了解SFINAE和约束模板参数.这不是问题的主题.主题是static_assert在测试表达式的正确性时如何使失败.
我想比较四个整数,看看它们是否相等。于是写了下面的,
int a = 1, b = 2, c = 3, d = 4;
if (a != b != c != d)
{
//do something
}
Run Code Online (Sandbox Code Playgroud)
这显然没有显示错误。但是,事实上,给出了错误的答案。有人可以解释一下吗?
我只是好奇相同的类型转换格式适用于 char、int 以及其他许多类型,但为什么它不适用于字符串,即(string) 'c'屏幕后面出了什么问题?
#include <iostream>
using namespace std;
int main(){
char a = 'a';
cout << (char) 99 <<endl;
cout << (int) 'c'<<endl;
cout<< (string) 'c' + "++" <<endl; // why this does not work???
cout<< string (1, 'c') + "++" <<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) c ×6
c++ ×4
arrays ×3
pointers ×3
c++11 ×2
boolean ×1
c++14 ×1
c++17 ×1
casing ×1
casting ×1
char ×1
compile-time ×1
gcc ×1
gcc-warning ×1
if-statement ×1
inequality ×1
string ×1