delete partial memory of the pointer

爱⌒轻易说出口 提交于 2021-02-05 11:58:48

问题


Is there any way i can delete the partial memory of the pointer.? for example

char *c = new char[1000];
sprintf(c,"this is it");

As it can be seen a lot of memory is getting wasted here. can I free the memory more than the required.?


回答1:


Not directly. The best you can do in C++ is to make a new copy that's the right size and delete the old one. There's no analog of C's realloc.




回答2:


Unless your system is a RAM-restricted embedded system, why bother? Just use ginormous buffers and include a 'dataLen' int.




回答3:


Well, allocate another memory block with the precise size required for the data, copy the data there and free the original (exceedingly large) memory block. Done.



来源:https://stackoverflow.com/questions/12023215/delete-partial-memory-of-the-pointer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!