Using auto_ptr or shared_ptr to allocate memory to a Structure

ronan_40060's Avatar
Go4Expert Member
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_struc t));
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_co rresp_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
xpi0t0s's Avatar, Join Date: Aug 2004
Looks easy enough; found this with Google:

Did you try Googling it, and if not why not?
If you did, where are you stuck?
ronan_40060's Avatar
Go4Expert Member
Im not sure how to use auto ptr to allcoate a memory for a structure .
xpi0t0s's Avatar, Join Date: Aug 2004
Oh I see. You don't. You initialise an auto_ptr from an existing pointer, either with
T *foo=new T;
auto_ptr<T> bar(T);

or with:
auto_ptr<T> bar(new T);
ronan_40060's Avatar
Go4Expert Member
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.g et(),pStrCorresp)
and then possible have a constructor to do the copying like


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

xpi0t0s's Avatar, Join Date: Aug 2004
You don't need to call a constructor explicitly; it gets called automatically when you "new" an object.
T *foo=new T; will automatically call T::T().

Bugs when you use strcpy instead of string? Depends on the code. sizeof(string) != strlen(string.c_str()) so watch out you don't assume that it is, although you don't show how you defined the "output" class/structure so I can only guess. How is output::record_type defined and why do you want to cast it to char*? If it isn't already char* then I can almost 100% guarantee you don't want to strcpy to it.
ronan_40060's Avatar
Go4Expert Member
record_type is a member of a structure SYS_CORRESP_STRUCT and is a char array.
where as recordType which comes from Java Front END is defined as
string recordType;

So my question is instead of using strcpy what if I use std:string to copy the value from input to output ?
if yes then how ?
xpi0t0s's Avatar, Join Date: Aug 2004
If it's already a char* (or char[]) why do you need to cast it to char*?

> strcpy...
> (char *)... <-- this bit
> ...output->record_type,(char *)input->recordType.c_str());

Remove the casts and let me know what errors you get.
ronan_40060's Avatar
Go4Expert Member
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);

ronan_40060's Avatar
Go4Expert Member
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