以下程序具有未定义的行为:
#include <stdio.h>
int main(void)
{
unsigned int x = -100; // This is fine, becomes UINT_MAX - 100
printf("%d\n", x); // This is undefined behavior.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
C99 7.19.6.1p8状态%d需要一个int参数.
C99 7.19.6.1p9声明"如果任何参数不是相应转换规范的正确类型,则行为 未定义."
但是,gcc -Wformat(包含在内-Wall)不会抱怨上述程序,为什么?这是一个错误,还是故意遗漏?
从gcc手册页:
-Wformat
Run Code Online (Sandbox Code Playgroud)
检查电话"printf"和"scanf"等,以确保提供参数指定适当的格式字符串类型,并在格式字符串指定的转换意义
我已经看到了关于此的其他问题,但没有一个完全解释它.编译器处理以下两种情况的正确方法是什么?我用gcc 4.7.1(使用-std = c ++ 0x),VS2010和VS2012尝试了一下,得到了不同的结果:
例1:
struct BB
{
// generic cast
template<typename T>
operator T() const
{
return 0;
}
// string cast
operator std::string() const
{
return string("hello");
}
};
int main()
{
BB b;
string s = b;
}
Run Code Online (Sandbox Code Playgroud)
输出:
例2:
struct BB
{
// generic cast
template<typename T>
operator T() const
{
return 0;
}
// string cast
operator std::string() const
{
return string("hello");
}
};
int main()
{ …Run Code Online (Sandbox Code Playgroud) 在您考虑将其标记为重复之前,请完整阅读该问题.声明就像
int i=int();
大多数程序员会说这里有值初始化,我将初始化值.(0作为输出).但它也在C++ 98编译器上输出0作为输出.以下我在C++ 98实现上测试的程序并给出了0作为输出.
#include <iostream>
int main()
{
int i=int();
std::cout<<i;
}
Run Code Online (Sandbox Code Playgroud)
不要说我在上面的C++ 98程序中初始化了值,因为在C++ 03中引入了值初始化.那我怎么在这里初始化?它真的是构造函数调用吗?int()看起来像构造函数调用.原始类型也有C++中的默认构造函数,正如Bjarne stroustrup在他的C++编程语言和TC++ PL中所说的那样.C++编程语言Bjarne stroustrup:
10.4.2内置类型也有默认构造函数
另请阅读同一本书第6.2.8节.
以下链接还表示内置类型在C++中具有默认构造函数.
1)http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=15
2)http://www.geeksforgeeks.org/c-default-constructor-built-in-types/
那么我真的可以说它是整数类型的构造函数调用吗?
函数中毒在C++中是非常有用的技术.
一般来说,它指的是使函数不可用,例如,如果你想禁止在程序中使用动态分配,你可能会"毒害" malloc函数,因此无法使用它."中毒"标识符意味着"中毒"后对标识符的任何引用都是硬编译器错误
例如(在此处查看现场演示)
#include <iostream>
#include <cstdlib>
#pragma GCC poison malloc
int main()
{
int* p=(int*)malloc(sizeof(int)); // compiler error use of poisoned function malloc
*p=3;
std::cout<<*p<<'\n';
free(p);
}
Run Code Online (Sandbox Code Playgroud)
我发现这种技术对于防止在C++中滥用保留字非常有用.
例如:
#include "test.h" // contains definition of some class T
#pragma GCC poison private
#define private public // oops compiler error use of poisoned identifier private in macro
int main()
{
// Instantiate T & use it members
}
Run Code Online (Sandbox Code Playgroud)
这也可以在C中用来防止使用C++关键字,因为C++有很多关键字而不是C&C使用C++特定关键字作为C中的标识符是完全有效的.
例如(在此处 …
考虑以下计划:
#include <iostream>
template <typename T>
void foo(const T* x) {
x();
}
void bar() { std::cout<<"bar() is called\n"; }
int main() {
foo(bar);
}
Run Code Online (Sandbox Code Playgroud)
它编译罚款clang++&VC++但g++给人以下编译器错误(见现场演示这里)
main.cpp: In function 'int main()':
main.cpp:10:9: error: no matching function for call to 'foo(void (&)())'
foo(bar);
^
main.cpp:3:6: note: candidate: template<class T> void foo(const T*)
void foo(const T* x) {
^~~
main.cpp:3:6: note: template argument deduction/substitution failed:
main.cpp:10:9: note: types 'const T' and 'void()' have incompatible …Run Code Online (Sandbox Code Playgroud) c++ const function-pointers language-lawyer template-argument-deduction
我尝试了下面的c程序,我希望得到编译时错误,但为什么编译器没有给出任何错误?
#include <stdio.h>
int main(void)
{
printf("%d\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么输出依赖于编译器?这是各种编译器的输出
Orwell Dev C++ IDE上的输出(使用gcc 4.8.1):0
Visual Studio 2010提供的Visual C++输出:0
CodeBlocks IDE(使用gcc 4.7.1):垃圾值
在线编译ideone.com:垃圾值
这里出了什么问题?
我知道函数原型在C++中是强制性的,如果函数是在main()函数之后定义的,但它在C中是可选的(但推荐).我最近编写了一个简单的程序,它执行了2个数字的加法,但错误地使用了点运算符代替传递参数时的逗号.
#include <stdio.h>
int main()
{
printf("sum is: %d",add(15.30)); // oops, uses dot instead of comma
}
int add(int a,int b)
{
return a+b;
}
Run Code Online (Sandbox Code Playgroud)
在上面的程序中,如果在add(int,int)函数之前定义了main()函数,那么程序肯定无法编译.这是因为调用函数时传递的参数少于所需的参数.
但是,为什么上面的程序编译运行正常 - 给出一些大的垃圾值作为输出?是什么原因?使用函数原型设计是否更好,以便编译器检测到类型不匹配以及与函数调用相关的任何其他错误?
这是未定义的行为吗?
我知道已经问过类似的问题,但我的问题是不同的,所以请不要在完全阅读之前将其标记为重复或其他内容.
Bjarne Stroustrup在他的"C++编程语言"一书中说过
10.4.2内置类型也有默认构造函数
另请阅读同一本书第6.2.8节.
以下链接还表示内置类型在C++中具有默认构造函数.
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=15
http://www.geeksforgeeks.org/c-default-constructor-built-in-types/
但是以下链接的答案表明内置类型没有构造函数.
另外,请阅读以下链接,这些链接说明int i=int();原因值初始化之类的语句基本上最终为零初始化.
以下是我有时问过的问题,即int a=int()在C++ 98中发生了什么,我得到了默认初始化的答案.
如果我没有错,那么在C++标准中没有提及或者C++标准没有说原始类型也有构造函数.那么,从Bjarne Stroustrup的书中,我可以说它在概念上是真的,他们有构造函数,但实际上没有像内置类型的构造函数这样的东西吗?他书中的文字真的有缺陷吗?或者,根据Bjarne Stroustrup的书,当它们被实际调用和使用时,它是否真的如此?
Stroustrup的C++编程语言第3版说,
仅当两个指针指向同一数组的元素时才定义指针的减法(尽管该语言没有确保情况的快速方法).从另一个指针中减去一个指针时,结果是两个指针之间的数组元素数(一个整数).可以向指针添加整数或从指针中减去整数; 在这两种情况下,结果都是指针值. 如果该值未指向与原始指针相同的数组或超出原始指针的元素,则使用该值的结果是未定义的.
例如:
void f ()
{
int v1 [10];
int v2 [10];
int i1 = &v1[5] - &v1[3]; // i1 = 2
int i2 = &v1[5] - &v2[3]; // result undefined
}
Run Code Online (Sandbox Code Playgroud)
我正在阅读维基百科上未指明的行为.它说
在C和C++中,如果指针指向同一对象的成员或同一数组的元素,则仅严格定义指向对象的指针.
例:
int main(void)
{
int a = 0;
int b = 0;
return &a < &b; /* unspecified behavior in C++, undefined in C */
}
Run Code Online (Sandbox Code Playgroud)
所以,我很困惑.哪一个是正确的?维基百科或Stroustrup的书?C++标准对此有何看法?
纠正我如果我误解了什么.
这就是事情.我最近在Qt框架上重写了我的OpenCV代码,并且代码在Visual Studio 2013上运行良好,但是当我在Qt上运行它时,发生了一些奇怪的事情.
为了简化问题,我编写了另一个代码来进行实验,并且预计问题仍然存在.
这是代码,
#include <iostream>
#include <highgui.hpp>
#include <core.hpp>
#include <cv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat view, viewGray;
vector<Point2f> pointBuf;
Size boardSize;
boardSize.width = 7; boardSize.height = 9;
view = imread("G:\\C++\\OpenCV\\OpenCV\\left1.jpg", 1);
cout << pointBuf.size() << endl;
cout << boardSize << endl;
cvtColor(view, viewGray, COLOR_BGR2GRAY);
bool found = findChessboardCorners(view, boardSize, pointBuf, \
CV_CALIB_CB_ADAPTIVE_THRESH | \
CV_CALIB_CB_FAST_CHECK | \
CV_CALIB_CB_NORMALIZE_IMAGE);
cout << pointBuf.size() << endl;
cout << found << endl;
namedWindow("show", CV_WINDOW_NORMAL);
imshow("show", view); …Run Code Online (Sandbox Code Playgroud) c++ ×7
c ×3
constructor ×2
gcc ×2
printf ×2
c++98 ×1
const ×1
gcc-warning ×1
opencv ×1
opencv3.0 ×1
operators ×1
pointers ×1
pragma ×1
qt ×1
template-argument-deduction ×1
visual-c++ ×1