草庐IT

func_pointer

全部标签

c++ - 在 C/C++ 中编写 "pointer to something"的好方法

在C/C++中是否有一种“好的”方式来编写“指向某物的指针”?我曾经写过voidfoo(char*str);但有时我发现它很不合逻辑,因为str的类型是“指向char的指针”,那么它应该更合乎逻辑将*附加到类型名称。写指针有规律吗?char*str;char*str;char*str;char*str; 最佳答案 没有严格的规则,但请记住*附加到变量,所以:char*str1,*str2;//str1andstr2arepointerschar*str1,str2;//str1isapointer,str2isachar有些人也喜欢

c++ - 函数返回 auto 自动参数 munmap_chunk() : invalid pointer

我正在测试newfeature对于GCC4.9(自动输入参数)并出现一些奇怪的错误。#include#includeautofoo(autov){for(auto&&i:v)std::cout{1,2,3});}这给我以下错误:***glibcdetected***./a.out:munmap_chunk():invalidpointer:0x00007f87f58c6dc0***=======Backtrace:=========/lib/x86_64-linux-gnu/libc.so.6(+0x7e846)[0x7f87f4e4c846]./a.out[0x400803]/lib

c++ - 为什么将 "pointer to pointer to non-const"转换为 "pointer to pointer to const"是不合法的

将非const指针转换为const指针是合法的。那为什么将指向非const的指针转换为指向const的指针是不合法的呢?例如,为什么下面的代码是非法的:char*s1=0;constchar*s2=s1;//OK...char*a[MAX];//akachar**constchar**ps=a;//error! 最佳答案 来自标准:constcharc='c';char*pc;constchar**pcc=&pc;//notallowed*pcc=&c;*pc='C';//wouldallowtomodifyaconstobject

c++ - 使用 stxxl 队列时为 `pointer being freed was not allocated`

我的代码似乎可以工作(由于上述错误,我还没有在大型数据集上尝试过)。代码:#include#include#includeintmain(){//queueq;//thisworksstxxl::queueq;//doesnotworkfor(inti=0;i我的简单.stxxl就是:disk=./testfile,0,syscall但我的错误是:stackexchangeexample(3884)malloc:***errorforobject0x101c04000:pointerbeingfreedwasnotallocated***setabreakpointinmalloc_e

c++ - 在 C++ 中,是否允许删除 list<Pointer>::unique 中的对象

我们有遗留代码,它返回巨大的原始指针列表到堆分配的对象(我们不能使用智能指针),我们将从列表中删除重复项,并将它们从堆中删除。现在,正如专家建议的那样,我想尝试std::list::unique(或forward_list::unique)而不是算法std::unique。我读过http://en.cppreference.com/w/cpp/container/list/unique在'unique'谓词中我们不应该改变对象,那么根据标准术语删除list::unique中的“将要被删除的”对象是否安全?如果是这样,list::unique中的哪个对象应该被视为重复项?在gnu实现中,

c# - 如何从 [C# WP8.1 WRT/SL] 项目中调用 [Unmanaged C++ DLL func]

这不是“如何从C#调用C++函数”的拷贝我知道如何为常规PC应用程序执行此操作,我尝试过并且有效:这叫做P/Invoke1.CreateaC++dllproject,butyoucan'taddtheC++dllprojectasareferencetotheC#project.CopythedllfromC++,andpastethedlltotheC#exefolder2.inC#use[DllImport...]inC++useextern"C"__declspec(dllexport)...但是当我在WindowsPhone上执行此操作时,失败了。WindowsPhone有两种

c++ - 如何从 std::array<T, N>::pointer 成员/依赖类型中推断出数组大小?

我的目标是为strcpy编写安全的替代品对于在编译期间已知目标缓冲区大小的情况,我希望推断出缓冲区大小,因此用户不需要知道它。例如:charxs[2]={0};strcpy(xs,"abc");//bufferoverflow!printf("[%s]\n",xs);此输出(希望)是:[abc]对于简单的情况,当传递C风格的数组时,可以毫不费力地写成:templatechar*safe_strcpy(char(&dst)[N],constchar*src)noexcept{std::snprintf(dst,N,"%s",src);return&dst[0];}推导出数组的大小,snp

c++ - 用于测试 func(args) 是否格式正确且具有必需的返回类型的特征

有许多类似的问题/答案,但我无法完全将这些答案放在一起来满足我的目的。我想要一个特质templatestructreturns_a{staticconstboolvalue;};这样returns_a::value如果F(Args)格式正确并返回T,则为真。经过更多研究后,我的工作方式如下://valueistrueifFunc(Args...)iswellformedtemplateclassis_callable{templatestaticdecltype(std::declval()(std::declval()...),void(),0)test(int);templates

c++ - 基本问题 : Pointers to objects in unordered_maps (C++)

我是C++编程的新手,非常感谢没有假定太多先验知识的回复。感谢这里的建议,我创建了一个无序map:typedefstd::tr1::unordered_maphmap;此映射中的数据是指向Strain类实例的指针。一旦创建了这些实例,我就创建了指向它们的指针,然后将这些指针添加到我的哈希表(hmapstrainTable)和另一个vector(vectorliveStrains),例如,stringMRCA;for(intb=0;bStrain类的实例永远不会被删除,指向它们的指针也不会从strainTable中删除。指针偶尔会在vectorliveStrains和vectordead

c++ - 警告 : resolving Func by linking to Func@##

我正在构建一个包含以下内容的DLL:extern"C"__declspec(dllexport)void__stdcallDrawMouse(intX,intY,intR,intG,intB){Buffer.SetMouse(X,Y,R,G,B);}然后在.def文件中我添加了:LIBRARYTest;DESCRIPTION"TestDefinitionFile"EXPORTSDrawMouse;然后当我编译时,我添加链接器选项:-static-static-libstdc++-static-libgcc-Wl,--kill-at-d--input-defsrc\Test.def-m3