Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有什么办法可以删除指针的部分内存。?例如
char *c = new char[1000]; sprintf(c,"this is it");
可以看出,这里浪费了很多内存。我可以释放超过所需的内存吗?
不是直接的。在 C++ 中您能做的最好的事情就是制作一个大小合适的新副本并删除旧副本。没有 C 的类似物realloc。
realloc
除非您的系统是 RAM 受限的嵌入式系统,否则何必费心呢?只需使用巨大的缓冲区并包含一个'dataLen' int。
好吧,分配另一个具有数据所需精确大小的内存块,将数据复制到那里并释放原始(非常大的)内存块。完毕。