shared_ptr for a raw pointer argument

不羁的心 提交于 2019-12-10 17:26:23

问题


When the function requires a char*, can you pass in a shared_ptr?

I'm reading in a whole text file (length = 100), and want to store the char's into a char[] array. The naive way I used was this:

ifstream dictFile(fileName);
size_t fileLength = 100;
char* readInBuffer(new char[fileLength]);
dictFile.read(readInBuffer, fileLength);
//processing readInBuffuer..............
delete[] readInBuffer;
dictFile.close();

Of course there is memory leak if an exception is thrown before the delete[] statement. I'm wondering if I can use shared_ptr readInBuffer(new char[fileLength]); But the function prototype

read ( char* s, streamsize n )

won't accept a smart pointer as input? Any tricks?

Edit: I'm trying to write something like this:

shared_ptr<char[]> readInBuffer(new char[fileLength]);
dictFile.read(readInBuffer.get(), fileLength);

But it won't compile.


回答1:


BIG FAT WARNING: creating a shared_ptr that points to an array provokes undefined behaviour, because the smart pointer will delete the pointer, not delete[] it. Use a vector instead!

Leaving this here because it might serve as a useful warning. Original answer follows...

The get() function returns the underlying raw pointer. You already wrote this in your code!

shared_ptr<char> readInBuffer(new char[fileLength]);
dictFile.read(readInBuffer.get(), fileLength);

The same result can be achieved with &*readInBuffer.

Of course, you have to be certain that dictFile.read() doesn't delete the pointer, or demons might fly out of your nose.




回答2:


Rather than using a pointer, you can use a vector instead.

std::vector<char> readInBuffer(fileLength);
dictFile.read(&readInBuffer[0], fileLength);



回答3:


No, you can't pass a shared_ptr. But you can create one, and call its get() member function to get a copy of the raw pointer to pass to the function. However, a shared_ptr doesn't deal with arrays; that's what vector is for. But you can use a unique_ptr to an array to manage that object:

std::unique_ptr<char[], std::default_delete<char[]> ptr(new char[whatever]);
f(ptr.get());

There may be a shorter way to write that first line, but I don't have time to dig it out.



来源:https://stackoverflow.com/questions/13405124/shared-ptr-for-a-raw-pointer-argument

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