草庐IT

c++ - C/C++ 指针,ptr+1 = ptr +1 byte 还是 ptr+1*sizeof(pointer_type)?

有any_type*ptr=(any_type*)malloc(sizeof(any_type)*size);my_ptr=ptr+1;memcpy(dst,my_ptr,sizeof(any_type));my_ptr会指向ptr之后的1个字节,还是指向ptr之后的sizeof(any_type)字节?对齐选项如何影响答案?有符号/无符号类型是否不同? 最佳答案 指针运算是在指针的静态类型[*]的大小上进行的,所以它会有效地添加sizeof*ptr。成员的对齐方式将作为类型的对齐方式(对象末尾的填充)考虑到对象的大小。struct

c++ - 在 C++ 中,表达式 "*pointer++"是如何工作的?

#includeusingnamespacestd;intmain(){intvalue=1,*pointer;pointer=&value;cout为什么++运算符不增加value? 最佳答案 Post-increment(++)hashigherprecedencethandereference(*).这意味着++绑定(bind)到pointer而不是*pointer。参见CFAQ4.3以及其中的引用资料。 关于c++-在C++中,表达式"*pointer++"是如何工作的?,我们在

c++ - 如何摆脱 - 来自 NULL 的 "warning: converting to non-pointer type ' 字符”?

我有这段代码:intmyFunc(std::string&value){charbuffer[fileSize];....buffer[bytesRead]=NULL;value=buffer;return0;}行-buffer[bytes]=NULL给我一个警告:convertingtonon-pointertype'char'fromNULL。我如何摆脱这个警告? 最佳答案 不要使用NULL?它一般是为指针保留的,你没有指针,只有一个简单的char。只需使用\0(空终止符)或简单的0。

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++ 严格的别名规则和指向成员的指针

以下代码在G++中产生警告:#include#includetemplateQT::*pointer_to(PT::*p,QP::*q){typedefQT::*output_ptr;//warning:dereferencingtype-punnedpointerwillbreakstrict-aliasingrules[-Wstrict-aliasing]size_ttmp=reinterpret_cast(p)+reinterpret_cast(q);returnreinterpret_cast(tmp);}structA{intx;};structB{Aa;};intmain(

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++ - 如何从 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