Dear Experts

In our application the values that are sent from FRONT end( Java) are stored in a C++ Structure on Unix box and then a remote procedure call is made .
To store the values that are coming from front end , Im allocating the memory using operator new as below
sys_corresp_struct * p_str_Corresp = (sys_corresp_struct*)new char[sizeof(sys_corresp_struct)];
memset(p_str_Corresp,'\0',sizeof(sys_corresp_struct));

where in sys_corresp_struct is the Structure to hold all the incoming values..
then a function CopyToDcesys_corresp_structcopies the data as below

void CCaslifUtility::CopyToDceSYS_CORRESP_STRUCT(sys_corresp_struct* output,SysCorrespStruct &input)
{

       strcpy(char *)output->record_type,(char *)input->recordType.c_str());
       strcpy((char *)output->country_code,(char *)input->countryCode.c_str());
       strcpy((char *)output->leg_veh,(char *)input->legVeh.c_str());
       strcpy((char *)output->boff_code,(char *)input->boffCode.c_str());
       strcpy((char *)output->ref_num,(char *)input->refNum.c_str());
       strcpy((char *)output->seq_num,(char *)input->seqNum.c_str());
       strcpy((char *)output->prod_type,(char *)input->prodType.c_str());
       strcpy((char *)output->opn_type,(char *)input->opnType.c_str());
       strcpy((char *)output->txn_ccy,(char *)input->txnCcy.c_str());

}

After the data has been copied and the RPC has been made , the memory is released as

delete [] p_str_Corresp;
p_str_Corresp = NULL;

I have been thinking of using an auto_ptr or shared_ptr (from Boost Libraries) instead of using new operator for better memory management.
Can some show me how to allocate a memory to a structure using auto_ptr or shared_ptr ?

Many Thanks

Edited 3 Years Ago by Nick Evan: Fixed formatting

Basically using operator new for memory allocation has nothing to do with the smart pointers (auto_ptr or shared_ptr), the purpose of auto_ptr is to encapsulate the naked pointer and release the memory automatically when it exists from scope basically there are several objectives of smart pointers. among those mostly used are.
i) ownership
ii) reference counting

in your case I think you just need the wrapper to exclude the explicit delete call, you can write the wrapper like auto_ptr in more appropriate way which most suited your needs and scenarios (multithreaded or not). otherwise check for the auto_ptr implementation again be careful if you pass this pointer to some functions as I told you if auto_ptr is implemented by your compiler as ownership transfer then you must always pass (auto_ptr<T>&) to any function (i.e. reference to auto_ptr) otherwise you'll lost the ownership from main function. Its good to use the Auto pointer

Read any good article of Auto_ptr, but keep in mind the different between delete[] & delete operator provided by your auto pointer implementation.

Hope this helps?

Hi Laid
Thanks for your reply
This is what I have
auto_ptr<SYS_CORRESP_STRUCT> p_str_Corresp = new (SYS_CORRESP_STRUCT);

memset(p_str_Corresp.get(),'\0',sizeof(SYS_CORRESP_STRUCT));

conObj.CopyToDceSYS_CORRESP_STRUCT(p_str_Corresp.get(),pStrCorresp)
and then possible have a constructor to do the copying like

SYS_CORRESP_STRUCT::SYS_CORRESP_STRUCT(SysCorrespStruct*)
{

// code of void CCaslifUtility::CopyToDceSYS_CORRESP_STRUCT() goes here - or just call it
}
also
do u see a potential bug when strcpy() is used instead of std::string types ??

Thanks

Hi Laid
Thanks for your reply
This is what I have
auto_ptr<SYS_CORRESP_STRUCT> p_str_Corresp = new (SYS_CORRESP_STRUCT);

memset(p_str_Corresp.get(),'\0',sizeof(SYS_CORRESP_STRUCT));

conObj.CopyToDceSYS_CORRESP_STRUCT(p_str_Corresp.get(),pStrCorresp)
and then possible have a constructor to do the copying like

SYS_CORRESP_STRUCT::SYS_CORRESP_STRUCT(SysCorrespStruct*)
{

// code of void CCaslifUtility::CopyToDceSYS_CORRESP_STRUCT() goes here - or just call it
}
also
do u see a potential bug when strcpy() is used instead of std::string types ??

Thanks

Hi ronan,
Basically std::string are preffered over char* due to several reasons you can read many articles on that but again if you can't change it from char* to std::string you can write the adapter for SYS_CORRESP_STRUCT class or you can use the handle body ideom in this case its totally up to you and the scenario. because RPC requires marshaling so some libraries of RPC requires char* instead of std::string and if you are getting the class SYS_CORRESP_STRUCT from that lib you can better write the adapter for better manageability or you can keep it as it, as far as strcpy is concerned I don't recommend you to use strcpy you should use strncpy why ? read about buffer overflow exploit :). and I don't know why you write the strcpy as follows. strcpy((char *)output->record_type,(char *)input->recordType.c_str()); the second parameter of strcpy takes const char* and string function c_str() returns the const char* so no need to cast. if you replace the above call to strncpy if you're not intended to use the strings. then use the following call

I assume ouput->record_type is a char*. not a string. and is allocated somewhere either on stack or heap strncpy(output->record_type, input->recordType.c_str(), input->recordType.size()); remember from practical point of view we normally change the code to meet our requirement at best, depending on our requirement & needs, first set the objective what actually you want to achieve will all the changes you are interested in. efficiency, manageability, scalability ? then tweak your code as per your requirement.

Hope above help.

auto_ptr<SYS_CORRESP_STRUCT> p_str_Corresp = new (SYS_CORRESP_STRUCT);
declaration throws an error that
SYS_CORRESP_STRUCT*' could not be converted to 'const auto_ptr<SYS_CORRESP_STRUCT> &'.

so I declared as
auto_ptr<SYS_CORRESP_STRUCT>p_str_Corresp (new SYS_CORRESP_STRUCT); which gets compiled without any issues

Since the copying all the parameter values from JAVA to the local Structure SYS_CORRESP_STRUCT , I have used a custom strcpyDCE () function that is
void strcpyDCE(std::String, const WSSP_STD_NS::String &, int) ; where in
std::String is the o/p string and const WSSP_STD_NS::String & is the i/p string and int is the size to be copied
and im using for example as below
strcpyDCE(output->record_type.get(),input->recordType, 4);

Previously
for normal char* i used to allocate memory like
char *dce_sTabName = (char *)malloc(strlen(sTabName.c_str())+1);
memset((void *)dce_sTabName,'\0',strlen(sTabName.c_str())+1);
strcpyDCE(dce_sTabName,(char *)sTabName.c_str(),30);
here above 30 is the max size that can be used to copy
Now using std::String class
using just using
std::String dce_sTabName; // declaration
dce_sTabName = sTabName.c_str();
// Here c_str() will append '\0' at the end of dce_sTabName
NO need to mention how many bytes to be copied
Let me know your views on it

This question has already been answered. Start a new discussion instead.