C++ 如何在函数内部返回指针值后删除指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3145799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete a pointer after returning its value inside a function
提问by Emer
I have this function:
我有这个功能:
char* ReadBlock(fstream& stream, int size)
{
char* memblock;
memblock = new char[size];
stream.read(memblock, size);
return(memblock);
}
The function is called every time I have to read bytes from a file. I think it allocates new memory every time I use it but how can I free the memory once I have processed the data inside the array? Can I do it from outside the function? Processing data by allocating big blocks gives better performance than allocating and deleting small blocks of data?
每次我必须从文件中读取字节时都会调用该函数。我认为它每次使用它都会分配新的内存,但是一旦我处理了数组中的数据,我该如何释放内存?我可以从函数外部执行吗?通过分配大块来处理数据比分配和删除小块数据具有更好的性能?
Thank you very much for your help!
非常感谢您的帮助!
采纳答案by Georg Fritzsche
Dynamic arrays are freed using delete[]
:
使用delete[]
以下方法释放动态数组:
char* block = ReadBlock(...);
// ... do stuff
delete[] block;
Ideally however you don't use manual memory management here:
理想情况下,您不要在这里使用手动内存管理:
std::vector<char> ReadBlock(std::fstream& stream, int size) {
std::vector<char> memblock(size);
stream.read(&memblock[0], size);
return memblock;
}
回答by sigfpe
Just delete[]
the return value from this function when you've finished with it. It doesn't matter that you're deleting it from outside. Just don't delete it before you finish using it.
只是delete[]
从这个函数的返回值,当你完成它。你从外面删除它并不重要。在使用完之前不要删除它。
回答by Stephen
You cancall:
您可以致电:
char * block = ReadBlock(stream, size);
delete [] block;
But... that's a lot of heap allocation for no gain. Consider taking this approach
但是......这是很多没有收益的堆分配。考虑采用这种方法
char *block = new char[size];
while (...) {
stream.read(block, size);
}
delete [] block;
*Note, if size
can be a compile time constant, you can just stack allocate block
.
*注意,如果size
可以是编译时常量,则可以只堆栈分配block
。
回答by Clark Gaebel
Yes. You may call delete from outside of the function. In this case though, may I suggest using an std::string so you don't have to worry about the management yourself?
是的。您可以从函数外部调用 delete。不过,在这种情况下,我可以建议使用 std::string 以便您不必自己担心管理吗?
回答by matt
first thing to note: memory allocated with new and delete is completely global. things are not automatically deleted when pointers go out of scope or a function is exited. as long as you have a pointer to the allocation (such as the pointer being returned there) you can delete it when ever and where ever you want. the trick, is just makeing sure other stuff doesn't delete it with out you knowing.
首先要注意:用 new 和 delete 分配的内存是完全全局的。当指针超出范围或退出函数时,事物不会自动删除。只要您有一个指向分配的指针(例如在那里返回的指针),您就可以随时随地删除它。诀窍是确保其他内容不会在您不知情的情况下将其删除。
that is a benefit with the sort of function structure the fstream read function has. it is fairly clear that all that function is going to do is read 'size' number of bytes into the buffer you provide, it doesn't matter whether that buffer has been allocated using new, whether its a static or global buffer, or even a local buffer, or even just a pointer to a local struct. and it is also fairly clear that the function is going to do nothing more with the buffer you pass it once it's read the data to it.
这是 fstream 读取函数所具有的那种函数结构的好处。很明显,该函数要做的就是将“大小”字节数读入您提供的缓冲区中,无论该缓冲区是否已使用 new 分配,无论是静态缓冲区还是全局缓冲区,甚至是一个本地缓冲区,甚至只是一个指向本地结构的指针。并且很明显,一旦函数将数据读取到它,它就不会对传递给它的缓冲区执行任何操作。
on the other hand, take the structure of your ReadBlock function; if you didn't have the code for that it would be tricky to figure out exactly what it was returning. is it returning a pointer to new'd memory? if so is it expecting you to delete it? will it delete it it's self? if so, when? is it even a new pointer? is it just returning an address to some shared static buffer? if so, when will the buffer become invalid (for example, overwritten by something else)
另一方面,采用 ReadBlock 函数的结构;如果您没有相应的代码,则很难弄清楚它到底返回了什么。它是否返回一个指向新内存的指针?如果是这样,是否希望您删除它?它会删除它自己吗?如果是这样,什么时候?它甚至是一个新的指针吗?它只是向某个共享静态缓冲区返回一个地址吗?如果是这样,缓冲区何时会变得无效(例如,被其他东西覆盖)
looking at the code to ReadBlock, it is clear that it is returning a pointer to new'd memory, and is expecting you to delete it when ever you are done with it. that buffer will never be overwritten or become invalid until you delete it.
查看 ReadBlock 的代码,很明显它正在返回一个指向新内存的指针,并希望您在完成后将其删除。该缓冲区永远不会被覆盖或变为无效,直到您将其删除。
speed wise, thats the other advantage to fsream.read's 'you sort out the buffer' approach: YOU get the choice on when memory is allocated. if you are going "read data, process, delete buffer, read data process delete buffer, ect.... " it is going to be alot more efficient to just allocate one buffer (to the maximum size you will need, this will be the size of your biggest single read) and just use that for everything, as suggested by Stephen.
速度方面,这是 fsream.read 的“你整理缓冲区”方法的另一个优势:你可以选择何时分配内存。如果您要“读取数据、处理、删除缓冲区、读取数据处理删除缓冲区等......”,只分配一个缓冲区会更有效率(到您需要的最大大小,这将是您最大的单次阅读的大小),并按照斯蒂芬的建议将其用于所有内容。
回答by Aakash Naik
How about using a static char* memblock; It will be initialised just once and it wont allocate memblock a new space everytime.
使用静态 char* memblock 怎么样?它只会被初始化一次,并且不会每次都为 memblock 分配一个新空间。