草庐IT

malloc_size

全部标签

c++ - 为什么我得到 "Invalid Allocation Size: 4294967295 Bytes"而不是 std::bad_alloc 异常?

我写了下面一段代码来为一个数组分配内存:try{intn=0;cin>>n;double*temp=newdouble[n];...}catch(exception&e){cout当然,我正在检查n的负值等。但是当我输入一些超过536*(10^6)的大数字时,我没有收到错误分配异常,而是收到“无效分配大小:4294967295字节”崩溃。例如我输入n=536*(10^6)-->bad-allocexception我输入n=537*(10^6)-->分配大小无效:4294967295字节-->崩溃知道为什么会这样吗? 最佳答案 调用n

c++ - 为什么 std::hex 会导致内存损坏 vector.size()?

我有以下代码:vectortest=GetMemoryAddresses();cout打印以下结果:Size:18MemoryAddress:fc06a0MemoryAddress:13a70f0MemoryAddress:36c78c1MemoryAddress:3da0ea0MemoryAddress:3e21b80MemoryAddress:c0a6881MemoryAddress:c747690MemoryAddress:c748b98MemoryAddress:c74a1b8MemoryAddress:c74ce10MemoryAddress:c750c78MemoryAdd

c++ - 我真的需要返回 Type::size_type 吗?

我经常有一些类,它们大多只是一些STL容器的包装器,如下所示:classFoo{public:typedefstd::vectorVec;typedefVec::size_typesize_type;constVec&GetVec(){returnvec_;}size_typesize(){returnvec_.size()}private:Vecvec_;};我不太确定返回size_type。通常,某些函数会调用size()并将该值传递给另一个函数,另一个函数将使用它并可能传递它。现在每个人都必须包含那个Fooheader,虽然我实际上只是传递一些大小值,无论如何它应该只是unsig

c++ - size_t 和 offset_t 的使用指南?

这可能是一个C++101问题:我很好奇使用size_t和offset_t的指南是什么,例如它们用于什么情况,它们不用于什么情况,等等。我没有做过很多可移植的编程,所以我通常只使用int或unsignedint这样的东西用于数组大小、索引等。但是,我发现最好尽可能使用这些更标准的typedef,所以我想知道如何正确地做到这一点。作为后续问题,对于使用VisualStudio2008在Windows上进行开发,我应该在哪里寻找实际的typedef?我发现size_t在VS安装目录中的许多header中定义,所以我不确定我应该使用哪一个,而且我找不到offset_t任何地方。

c++ - size of datatype 和 sizeof(data type) 的区别

我在学习C++时遇到了以下问题。我只是一个初学者,我很困惑。sizeof()函数不应该返回数据类型的大小吗?为什么数据对象的大小可能与其sizeof()不同?我不明白答案的解释。假设在一台假想的机器中,char的大小是32位。sizeof(char)会返回什么?一)4b)1c)依赖于实现d)机器相关答案:b说明:标准不要求char为8位,但确实要求sizeof(char)返回1。 最佳答案 sizeof运算符以bytes为单位生成类型的大小,其中字节定义为char的大小。所以根据定义,sizeof(char)始终为1,无论bitsc

c++ - 从 std::size_t* 到 long unsigned int* 的无效转换

在raspberrypi-arv7l上,我正在编译以下C++程序#includevoidfun(unsignedlongint*i){std::cout对于上面的代码,我收到以下错误:a.cpp:Infunction'intmain()':a.cpp:12:9:error:invalidconversionfrom'std::size_t*{akaunsignedint*}'to'longunsignedint*'[-fpermissive]fun(&i);^~a.cpp:3:6:note:initializingargument1of'voidfun(longunsignedint*

c++ - 一些内存似乎在 malloc() 和 free() 之后分配

我是C的新手。我正在尝试熟悉malloc+free。我已经编写了以下测试代码,但由于某种原因,内存没有完全释放(顶部仍然指示分配给进程的大约150MB内存)。这是为什么?#include#includetypedefstruct{char*inner;}structure;intmain(){inti;structure**structureArray;structureArray=(structure**)malloc(sizeof(structure*)*1000*10000);for(i=0;iinner=(char*)malloc(sizeof(char)*1000*1000*

C++:malloc:错误:从 ‘void*’ 到 ‘uint8_t*’ 的无效转换

我遇到了这个问题:invalidconversionfrom‘void*’to‘uint8_t*’执行此操作时:intnumBytes;uint8_t*buffer;buffer=malloc(numBytes);//errorhere,why?还是我必须这样说?buffer=malloc(numBytes);请解释一下。 最佳答案 您不能在C++中从void*隐式转换(在这方面与C不同)。你可以这样做:buffer=static_cast(malloc(numBytes));但实际上,您应该只使用new/delete而不是mall

c++ - 使用 malloc() 和 sizeof() 在堆上创建结构

我正在尝试使用malloc()和sizeof()在堆上创建一个结构。这是我的代码:#include#include#includestructEmployee{charfirst[21];charlast[21];chartitle[21];intsalary;};structEmployee*createEmployee(char*first,char*last,char*title,intsalary)//CreatesastructEmployeeobjectontheheap.{structEmployee*p=malloc(sizeof(structEmployee));if

c++ - 使用malloc代替new,创建对象时调用拷贝构造函数

我想试用TBB的scalable_allocator,但是当我不得不替换我的一些代码时感到困惑。这是使用分配器完成分配的方式:SomeClass*s=scalable_allocator().allocate(sizeof(SomeClass));编辑:上面显示的不是使用scalable_allocator完成分配的方式。作为ymettcorrectlymentioned,分配是这样完成的:intnumberOfObjectsToAllocateFor=1;SomeClass*s=scalable_allocator().allocate(numberOfObjectsToAlloca