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
|
Mentor
|
![]() |
| 25Nov2008,17:23 | #2 |
|
Looks easy enough; found this with Google:
http://www.gotw.ca/publications/usin...ffectively.htm Did you try Googling it, and if not why not? If you did, where are you stuck? |
|
Go4Expert Member
|
|
| 25Nov2008,18:36 | #3 |
|
Im not sure how to use auto ptr to allcoate a memory for a structure .
|
|
Mentor
|
![]() |
| 25Nov2008,20:23 | #4 |
|
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); |
|
Go4Expert Member
|
|
| 26Nov2008,09:11 | #5 |
|
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 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 |
|
Mentor
|
![]() |
| 26Nov2008,14:06 | #6 |
|
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. |
|
Go4Expert Member
|
|
| 26Nov2008,15:11 | #7 |
|
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 ? |
|
Mentor
|
![]() |
| 26Nov2008,15:38 | #8 |
|
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. |
|
Go4Expert Member
|
|
| 26Nov2008,16:40 | #9 |
|
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); |
|
Go4Expert Member
|
|
| 26Nov2008,16:59 | #10 |
|
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 |


