我一直在阅读一下,当你使用placement new时,你必须手动调用析构函数.
考虑下面的代码:
// Allocate memory ourself
char* pMemory = new char[ sizeof(MyClass)];
// Construct the object ourself
MyClass* pMyClass = new( pMemory ) MyClass();
// The destruction of object is our duty.
pMyClass->~MyClass();
Run Code Online (Sandbox Code Playgroud)
据我所知,运算符delete通常会调用析构函数然后释放内存,对吧?那么我们为什么不用delete呢?
delete pMyClass; //what's wrong with that?
Run Code Online (Sandbox Code Playgroud)
在第一种情况下,我们被迫将pMyClass设置为nullptr在我们调用析构函数之后:
pMyClass->~MyClass();
pMyClass = nullptr; // is that correct?
Run Code Online (Sandbox Code Playgroud)
但是析构函数没有释放内存,对吧? 这会是内存泄漏吗?
我很困惑,你能解释一下吗?
灵感来自这个问题:
当条件是命令替换时,如果命令不产生输出,if语句应该做什么?
注意:示例是if $(true); then ...,而不是if true ; then ...
例如,给定:
if $(true) ; then echo yes ; else echo no ; fi
Run Code Online (Sandbox Code Playgroud)
我认为$(true)应该用true命令的输出代替,这没什么.它应该等同于:
if "" ; then echo yes ; else echo no ; fi
Run Code Online (Sandbox Code Playgroud)
打印,no因为没有名称为空字符串的命令,或者这样:
if ; then echo yes ; else echo no ; fi
Run Code Online (Sandbox Code Playgroud)
这是一个语法错误.
但实验表明,如果命令不产生输出,则该if语句将其视为true或false,具体取决于命令的状态,而不是其输出.
这是一个演示行为的脚本:
#!/bin/bash
echo -n 'true: ' ; if true ; then echo yes ; else echo …Run Code Online (Sandbox Code Playgroud) C标准main为托管实现指定了两种形式的定义:
int main(void) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
和
int main(int argc, char *argv[]) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
它可以以与上述"等效"的方式定义(例如,您可以更改参数名称,替换int为定义为int或写char *argv[]为的typedef名称char **argv).
它也可以"以某种其他实现定义的方式"定义 - 这意味着如果实现记录它们,那么它们int main(int argc, char *argv[],
char *envp)是有效的.
"以其他一些实施方式定义的方式"条款不在1989/1990标准中; 它是由1999标准添加的(但早期标准允许扩展,因此实现仍然允许其他形式).
我的问题是:鉴于当前(2011)ISO C标准,是表格的定义
int main() { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
所有托管实现都有效且可移植?
(请注意,我没有解决C++中的任何一个void main或没有使用
int main()括号的问题.这只是ISO int main(void)和int main()ISO 之间的区别.)
在阅读文章时,我遇到了以下功能:
SolidColor::SolidColor(unsigned width, Pixel color)
: _width(width),
_color(color) {}
__attribute__((section(".ramcode")))
Rasterizer::RasterInfo SolidColor::rasterize(unsigned, Pixel *target) {
*target = _color;
return {
.offset = 0,
.length = 1,
.stretch_cycles = (_width - 1) * 4,
.repeat_lines = 1000,
};
}
Run Code Online (Sandbox Code Playgroud)
作者用return语句做了什么?我之前没有见过这样的东西,我不知道如何搜索它...它对普通C也有效吗?
编辑: 链接到原始文章
我最近将gcc和g ++更新到版本7.2.我想尝试一下std::experimental::any,std::variant特别是,我在QtCreator中使用Qt 5.9.1.
到目前为止,我已经在项目文件中写了这个:
CONFIG += c++17
Run Code Online (Sandbox Code Playgroud)
我在正确的位置添加了正确的标题:
#include <variant>
#include <experimental/any>
Run Code Online (Sandbox Code Playgroud)
任何工作都很好,没有问题.但是,当我包含变体头文件时,我收到此错误:
/usr/include/c++/7/bits/c++17_warning.h:32: error: #error This file requires compiler and library support for the ISO C++ 2017 standard. This support must be enabled with the -std=c++17 or -std=gnu++17 compiler options.
Run Code Online (Sandbox Code Playgroud)
#error此文件需要编译器和库支持\ ^ ~~~~
我在项目文件中尝试过各种各样的东西,这里是完整列表:
CONFIG += c++17
Run Code Online (Sandbox Code Playgroud)
&
CONFIG += c++1z
Run Code Online (Sandbox Code Playgroud)
&
QMAKE_CXXFLAGS += -std=c++17
Run Code Online (Sandbox Code Playgroud)
&
QMAKE_CXXFLAGS += -std=c++1z
Run Code Online (Sandbox Code Playgroud)
&
CONFIG += c++17
QMAKE_CXXFLAGS += -std=c++17
Run Code Online (Sandbox Code Playgroud)
&
CONFIG += c++1z
QMAKE_CXXFLAGS += -std=c++1z
Run Code Online (Sandbox Code Playgroud)
&
CONFIG …Run Code Online (Sandbox Code Playgroud) 我知道在C编译器中,该main()函数由_start()函数调用,该函数具有如下代码:
exit(main()); // return value of main is returned
Run Code Online (Sandbox Code Playgroud)
如何_start()工作的时候main()不返回int,例如,如果它的返回类型void,float或者其他什么东西?
我试图在linux环境下运行linux驱动程序.遵循指令运行winkvm ..卡在点运行make命令使用cygwin环境..喜欢
使用Cygwin环境构建原始KVM驱动程序:
cd kvm/kernel ##不要输入configure make ##你会得到id:无法识别的仿真模式:elf_i386但它不是错误make cpobjs ##如果你没有得到目录消息,那就试试吧再试一次
如何从cygwin终端运行make命令..从哪个控制台的cygwin ..获取错误bash使命令找不到..
在我测试的两个系统上(一个32位的Ubuntu 12.04服务器和一个64位的Ubuntu 13.10 VM),time()给出的纪元以来的秒数可能与gettimeofday()不同.
具体来说,虽然我在调用time() 后调用gettimeofday(),但返回的值time()有时小于tv_sec返回的值gettimeofday().
这显然发生在时钟滚动到新的秒钟之后.
这导致了我的一些代码中的错误,这些代码期望time()和gettimeofday()的秒可以互换.
示例代码演示此问题:
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
int main()
{
time_t start = time(NULL);
int same = 0;
int different = 0;
int max_usec = 0;
while (1) {
time_t t;
struct timeval tv;
gettimeofday(&tv, NULL);
t = time(NULL);
if (t < tv.tv_sec) {
different++;
if (tv.tv_usec > max_usec) {
max_usec = tv.tv_usec;
}
} else { …Run Code Online (Sandbox Code Playgroud) ...我看起来非常仔细(我相信),并且没有成功为我的Mac安装Git.
出于各种原因,我正在运行10.6.8的Mac OS X,并且不会很快改变它.
我已经收集并安装了此处公开的软件包:
https://help.github.com/articles/set-up-git
安装说明非常清楚,我很清楚安装的软件包.但是,任何从命令行使用git客户端的尝试都会导致"非法指令"错误.
我仔细筛选了这里提供的信息:
http://git-scm.com/book/en/Getting-Started-Installing-Git
还有另一个似乎可用的捆绑包.它被称为"GitHub for Mac 1.7.5,但似乎需要Mac OS X 10.7或更高版本.
还有其他人遇到过这个困难吗?我必须从源码构建吗?
我有几个小时的阅读和黑客入侵这项工作?有没有明显的东西我没有考虑过?
从1999版开始,ISO C标准定义了一个标准头<stdint.h>,其中定义了typedef intmax_t和uintmax_t.它们分别指定"能够表示任何(有符号|无符号)整数类型的任何值的(有符号|无符号)整数类型".
例如,如果像典型的是,最宽的符号和无符号整数类型是long long int和unsigned long long int,这两者通常是64位,则intmax_t和uintmax_t在可能被定义<stdint.h>如下:
typedef long long int intmax_t;
typedef unsigned long long int uintmax_t;
Run Code Online (Sandbox Code Playgroud)
有一组预定义的有限符号和无符号整数类型,从的signed,unsigned和普通char高达signed和unsigned long long int.
C99和C11还允许实现定义扩展的整数类型,这些类型不同于任何标准类型,并且具有实现定义的关键字的名称.
gcc和clang在某些但不是所有目标上都支持类型__int128和unsigned __int128.这些行为类似于128位整数类型,但它们不被视为扩展整数类型,并且两个编译器的文档都声明它们不支持任何扩展整数类型.因为这些都不是整数类型的标准定义的术语,该类型定义intmax_t和uintmax_t是64位类型的,而不是128位的类型.
这些都不违反C标准(实现不需要具有任何扩展的整数类型,并且只要它们不破坏任何严格符合的程序,它们就被允许具有任意扩展).但在我看来,它将使完美感__int128和unsigned __int128被视为扩展整数类型,以及intmax_t和uintmax_t为128位的类型. …