草庐IT

VARIABLE_SIZE_STRUCT

全部标签

c++ - 如果类有析构函数/delete[],则成员运算符 new[] 的参数 "size"增加

以下代码中的4个类:A、B、C和D。他们都有一个成员operatornew[]。此外,B有一个构造函数;C有析构函数;D有一个成员operatordelete[]。输出成员operatornew[]的参数size和4个类的sizeof:new[]A40new[]B40new[]C48new[]D48sizeof(A)4sizeof(B)4sizeof(C)4sizeof(D)4大小不同的原因是什么?代码(我知道很丑):#includeusingnamespacestd;classA{inti;public:staticvoid*operatornew[](std::size_tsize

C++ 重载 new[] 查询 : What size does it take as parameter?

我像这样重载了operatornew[]void*human::operatornew[](unsignedlongintcount){cout现在打电话human*h=newhuman[14];说sizeof(human)=16,但计算它打印出来的是232,也就是14*16+sizeof(int*)=224+8。为什么要分配这个额外的空间?它落在内存中的什么地方?因为当我打印*h或h[0]我得到相同的结果,所以它不在内存块的开头。它是否完全正确,或者我在这里遗漏了一些东西? 最佳答案 分配的额外空间用于存储内部使用的数组大小(在实

没有宏的 C++ 简单反射 : Print Variable Name and Its Value

在C++中是否有一种非宏的方式来打印变量名及其值。这是宏方法:#defineSHOW(a)std::coutPS:我用的是Linux,不需要跨平台的解决方案 最佳答案 不,C++不支持反射,唯一的方法(据我所知)是使用宏。 关于没有宏的C++简单反射:PrintVariableNameandItsValue,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6849965/

c++ - 从 size_t 转换为 int,还是用 size_t 迭代?

将迭代器条件右操作数从size_t转换为int更好,还是迭代可能超过int的最大值?答案实现具体吗?inta;for(size_ti=0;i 最佳答案 我几乎总是使用第一个变体,因为我发现大约80%的时间,我发现some_func应该也需要一个size_t。如果事实上some_func采用带符号的int,您需要了解当vect大于INT_MAX时会发生什么.如果解决方案在您的情况下不明显(通常不是),您至少可以替换some_func((int)i)与some_func(numeric_cast(i))(有关numeric_cast的一

c++ - C/C++ : Packing or padding data in a struct

我正在使用带有GNUGCC编译器的Code::BlocksIDE。structtest{chara;chare;charf;charb;chard;};sizeof(test)返回5。我读了这个答案:Whyisn'tsizeofforastructequaltothesumofsizeofofeachmember?为什么最后一个char后没有填充,所以sizeof(test)返回6或8?添加short和int等后,我可以问更多的问题。但我认为这个问题现在很好。填充不会使处理器更容易处理结构吗? 最佳答案 char的对齐方式仅为1,因

c++ - 将前向声明的 C-struct 定义为 C++-struct

将struct前向声明为C-struct是否合法?//api.h#ifdef__cplusplusextern"C"{#endiftypedefstructhandle_taghandle_t;handle_t*construct();voiddestruct(handle_t*h);voidfunc(handle_t*h);#ifdef__cplusplus}#endif然后将其定义为C++-struct,即非POD类型?//api.cppstructhandle_tag{voidfunc();std::stringmember;};voidfunc(handle_t*h){h->f

c++ - 将 std::condition_variable 与 atomic<bool> 一起使用

有几个关于SO处理原子的问题,以及其他处理std::condition_variable的问题。但是我的问题是我下面的用法是否正确?三个线程,一个ctrl线程在取消暂停其他两个线程之前做准备工作。当工作线程(发送者/接收者)处于紧密的发送/接收循环中时,ctrl线程还能够暂停它们。使用atomic的想法是在未设置暂停bool值的情况下使紧密循环更快。classSomeClass{public://...//Disregardthatdataispublic...std::condition_variablecv;//UDPthreadswillwaitonthiscvuntilallo

c++ - C/C++ : Pointers within Const Struct

如何在函数fn中强制obj->val1指向的内存的常量性?#includestructfoo{int*val1;int*val2;int*val3;};voidfn(constfoo*obj){//Idon'twanttobeabletochangetheintegerthatval1pointsto//obj->val1=newint[20];//Ican'tchangethepointer,*(obj->val1)=20;//ButIcanchangethememoryitpointsto...}intmain(intargc,char*argv[]){//Ineedtobeabl

c++ - QML 和 C++ 属性 - ReferenceError : Can't find variable

在编写QML应用程序时,我遇到了绑定(bind)问题,resp。在使用Qt4.8.1构建的QtQuick1应用程序中使用QML访问C++属性。每当我运行该应用程序时,我都会收到ReferenceError:Can'tfindvariable:...。在搜索文档、示例和论坛并创建一个小型QML项目来测试此行为后,我仍然无法弄清楚为什么会出现这些错误。这是我为测试获得的“应用程序输出”:应用输出Starting/.../build-QML_Cpp_propertyTest-Qt_4_8_1_in_PATH_System-Debug/QML_Cpp_propertyTest...Qmldeb

c++ - std::match_results::size 返回什么?

我对以下C++11代码有点困惑:#include#include#includeintmain(){std::stringhaystack("abcdefabcghiabc");std::regexneedle("abc");std::smatchmatches;std::regex_search(haystack,matches,needle);std::cout我希望它打印出3但我却得到了1。我错过了什么吗? 最佳答案 你得到1因为regex_search仅返回1个匹配项,size()将返回捕获组的数量+整个匹配值。你的匹配是.