I am using Oracle Occi bulk interface, where oracle can load huge amount of data of string into a buffer of char*. Say if the string is 20 bytes and record size is 100, then the buffer is char[21*100], with one char of \0 marking the end of every string record.
Now my issue is, I want to save this buffer for later bulk insertion, deletion or update to oracle. Meanwhile I still want to use std::string to operate on the record, like changing the value of the string and want the underlying char* to be changed simultaneously.
So the question is, how can I do a constructor for string like std::string(char* please_dont_care), and ensure that string doesn't release the char* upon destruction?
Some more detail. My starting point is the char[] I got from Oracle. And I don't want to copy it to string, since it can be 288 million operations in the case I did. I think the copying of memory would take sometime. Then the other calling function is written by someone else, which takes only string, and would change the content of the string. And I don't want to do another 288 million operations to copy string to char* again and then save it back to oracle.
I knew that string only have string(const char*) as constructor. I am studying the string constructor and maybe I can modify it and create a string(char*) constructor and set an internal flag for string not to clear the memory upon destruction?
Thank you.
I tried below: added an _is_shared private bool varaible to basic_string class. Based on this basic_string constructor,
template<typename _CharT, typename _Traits, typename _Alloc>
basic_string<_CharT, _Traits, _Alloc>::
basic_string(const _CharT* __s, const _Alloc& __a)
: _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
__s + npos, __a), __a)
{ }
I added a new constructor as below, skipping the copying function
basic_string(_CharT* __s, size_type __n, bool shared, const _Alloc& __a = _Alloc())
:_M_dataplus(__s, __a)
{
_is_shared=shared;
}
And I changed the destructor to add judgement for _is_shared:
~basic_string() _GLIBCXX_NOEXCEPT
{
if(!_is_shared)
_M_rep()->_M_dispose(this->get_allocator()); }
But when I tried to compile, I get a error as below: undefined reference to `std::basic_string, std::allocator >::basic_string(char*, unsigned long, bool, std::allocator const&)'
I moved the constructor from the .tcc(source file) to .h file, but it didn't help. Wonder how I can get it compile? Maybe the string class is C++ internal so that I need to do something extra to make it work?
Thank you.
Aucun commentaire:
Enregistrer un commentaire