这是一个C规范问题.
我们都知道这是合法的C,应该可以在任何平台上正常工作:
/* Stupid way to count the length of a number */
int count_len(int val) {
char buf[256];
return sprintf(buf, "%d", val);
}
Run Code Online (Sandbox Code Playgroud)
但这几乎可以保证崩溃:
/* Stupid way to count the length of a number */
int count_len(int val) {
char buf[256000000];
return sprintf(buf, "%d", val);
}
Run Code Online (Sandbox Code Playgroud)
不同之处在于后一个程序会破坏堆栈并且可能会崩溃.但是,纯粹在语义上,它与以前的程序没有任何不同.
根据C规范,后一个程序实际上是未定义的行为吗?如果是这样,它与前者的区别是什么?如果不是,那么C规范中的说法是否可以使符合要求的实现崩溃?
(如果这在C89/C99/C11/C++*之间有所不同,那么这也很有趣).
我已经安装libeigen3-dev以便使用Eigen 3编译程序.当我包含一个文件时,例如Eigen/Dense我在尝试运行时遇到此错误g++:
user@office-debian:~/Documents/prog$ g++ src/main.cpp -MMD -std=c++11
In file included from src/main.cpp:9:0:
src/tdefs.h:16:23: fatal error: Eigen/Dense: No such file or directory
compilation terminated.
Run Code Online (Sandbox Code Playgroud)
运行以下行正常工作:
g ++ -I/usr/include/eigen3/src/main.cpp -MMD -std = c ++ 11
不应该包含目录由GCC自动找到,因为我通过aptitude安装了Eigen包?为什么在安装库而不是Eigen时会自动发现boost和OpenGL?(请注意,eigen是一个仅限标头的库,但这不重要吗?)
运行g++ src/main.cpp -MMD -std=c++11 --verbose产生以下输出:
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.7.2-5' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc …Run Code Online (Sandbox Code Playgroud) 我想获得CPU温度.以下是我使用C++和WMI所做的事情.我正在读MSAcpi_ThermalZoneTemperature,但它始终是相同的,根本不是CPU温度.
有没有办法在不必编写驱动程序的情况下获得CPU的实际温度?或者我可以使用任何库吗?先感谢您.
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")
HRESULT GetCpuTemperature(LPLONG pTemperature)
{
if (pTemperature == NULL)
return E_INVALIDARG;
*pTemperature = -1;
HRESULT ci = CoInitialize(NULL);
HRESULT hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
if (SUCCEEDED(hr))
{
IWbemLocator *pLocator;
hr = CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLocator);
if (SUCCEEDED(hr))
{
IWbemServices *pServices;
BSTR ns = SysAllocString(L"root\\WMI");
hr = pLocator->ConnectServer(ns, NULL, NULL, NULL, 0, NULL, NULL, &pServices);
pLocator->Release();
SysFreeString(ns);
if (SUCCEEDED(hr)) …Run Code Online (Sandbox Code Playgroud) 我知道有很多这样的问题,但我找不到适合我的解决方案.
我试图制作简单的分数计算器,而不是可以添加或减去任意数量的函数,并将答案写为减少的分数.
示例:input = 3/2 + 4/8,output = 2
我正在尝试重载运算符以实现此目的.
所以在程序中,我正在尝试开发输入,其中包含由运算符'+'或' - '分隔的分数表达式.
表达式中的分数数是任意的.
以下6行中的每一行都是有效输入表达式的示例:
1/2 + 3/4
1/2 -5/7+3/5
355/113
3 /9-21/ -7
4/7-5/-8
-2/-3+7/5
Run Code Online (Sandbox Code Playgroud)
*我遇到的问题是,当我运行程序时,它有一个过载操作错误:错误:重载'operator <<'必须是二元运算符(有3个参数)*
/Users/Spicycurryman/Desktop/ECS40/hw1/fraction.cpp:61:22: error: overloaded 'operator<<' must be a binary operator (has 3 parameters)
ostream& Fraction::operator<<(ostream &os, Fraction& n)
^
/Users/Spicycurryman/Desktop/ECS40/hw1/fraction.cpp:80:22: error: overloaded 'operator>>' must be a binary operator (has 3 parameters)
istream& Fraction::operator>>(istream &os, Fraction& n)
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这是一个错误.
我的以下代码如下:
CPP文件
#include "Fraction.h"
Fraction::Fraction(int a, int b)
{
}
int Fraction::find_gcd (int n1, int …Run Code Online (Sandbox Code Playgroud) 我遇到了以下签名
double(&rotate_vec(double(&val)[4]))[4];
Run Code Online (Sandbox Code Playgroud)
在评论中,它"声称" 接受并返回四个元素的数组.我的第一反应是,这甚至看起来不是标准的c ++,但是这个编译:
double(&rotate_vec(double(&val)[4]))[4]
{
// ...
return val;
}
int main()
{
double ar[4] = { 1, 2, 3, 5 };
rotate_vec(ar);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在阅读Stroustrup的"The C++ Programming Language"时,我在p上看到了这句话.108:
"所使用的语法分析风格通常称为递归下降;它是一种流行且直接的自上而下的技术.在C++这样的语言中,函数调用相对便宜,它也很有效."
有人可以解释为什么C++函数调用很便宜吗?我会对一般性解释感兴趣,即如果可能的话,什么使得函数调用在任何语言中都很便宜.
C++ 14草案(N3936)在§3.2/ 3中陈述:
变量x的名称显示为潜在评估的表达式ex,除非将lvalue-to-rvalue转换(4.1)应用于x,否则将生成不调用任何非平凡函数的常量表达式(5.19),并且如果x是一个对象,ex是表达式e的潜在结果集合的元素,其中左值到右值的转换(4.1)应用于e,或者e是丢弃值表达式(第5节).
这对我没有任何意义:如果表达式e是丢弃值表达式取决于使用的上下文e.表达式语句(第6.2节)中使用的每个表达式都是一个丢弃值表达式.如果左值到右值转换被施加到e还依赖于上下文e中被使用.
而且,表达式在另一个表达式的潜在结果集合中意味着什么.人们需要表达式相等的概念才能确定集合的成员资格.但我们没有参考透明度,所以我看不出如何实现这一点.
为什么从C++ 11改为C++ 14?这怎么解释?就目前而言,它没有意义.
是boost::lexical_cast现在的冗余度,C++ 11引入stoi,stof和家人,或者是没有任何理由仍然使用它?(除了没有C++ 11编译器)它们是否提供完全相同的功能?
我有一个非常简单的类,像这样:
class MySimpleClass
{
public:
uint16_t m_myInt;
String m_myString;
String m_myOtherString;
MyEnum m_myEnumValue;
bool m_myBool;
};
Run Code Online (Sandbox Code Playgroud)
这个类是我无法更改的预编译库的一部分,它不提供构造函数.有什么方法可以初始化这个类而不必做这样的事情......
MySimpleClass msc;
msc.m_myInt = 1;
msc.m_myString = "foo";
msc.m_myOtherString = "bar";
msc.m_myEnumValue = ENUM_VALUE_YES;
msc.m_myBool = true;
Run Code Online (Sandbox Code Playgroud)
我并不反对这样做,但我很想知道是否有某种初始化语法可以做到这一点?
我在C++ 03工作,但C++ 11中的答案也会引起人们的兴趣.
当使用vim插件YouCompleteMe进行C++代码完成时,我偶然发现了一个问题.使用嵌套模板类可以使完成无法正常工作.
请考虑以下示例来重现行为:
#include <vector>
template<class T>
class foo {
public:
void Init();
private:
struct bar {
int foobar;
};
bar one_bar;
std::vector<foo<T>::bar> some_bars;
};
template<class T>
void foo<T>::Init(){
one_bar.foobar = 0; // completion as expected
some_bars.at(0).foobar = 0; // no completion neither for "at" nor for "foobar"
}
Run Code Online (Sandbox Code Playgroud)
"some_bars"的代码完成根本不起作用,而"one_bar"的行为与预期一致.
如何完成此代码的完成工作?这个问题是否与设置有关并且实际上应该有效还是YCM中的错误?
我的系统是基于debian的jessie/sid,vim版本7.4,来自GitHub的YCM最新版本.
编辑:YCMs bug跟踪器中报告了类似的问题:https : //github.com/Valloric/YouCompleteMe/issues/243 https://github.com/Valloric/YouCompleteMe/issues/530
似乎是铿锵而不是YCM中的错误.有人能证实吗?
编辑2:我在YCM问题跟踪器中打开了另一个问题. https://github.com/Valloric/YouCompleteMe/issues/1170
目的是获得有关clang中的错误信息的更多信息,最后在clang问题跟踪器中进行错误报告.
编辑3:我按照RedX的建议程序,在clang中输入我的代码以获得完成.Clang没有为代码中讨论的位置提供任何建议.这显然是YCM未能在vim中提出建议的原因,它与YCM或vim无关.
已提交clang问题跟踪器中的错误报告:http: //llvm.org/bugs/show_bug.cgi?id = 20973