Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Using auto_ptr or shared_ptr to allocate memory to a Structure (http://www.go4expert.com/forums/using-autoptr-sharedptr-allocate-memory-t15242/)

ronan_40060 25Nov2008 15:53

Using auto_ptr or shared_ptr to allocate memory to a Structure
 
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 25Nov2008 17:23

Re: Using auto_ptr or shared_ptr to allocate memory to a Structure
 
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?

ronan_40060 25Nov2008 18:36

Re: Using auto_ptr or shared_ptr to allocate memory to a Structure
 
Im not sure how to use auto ptr to allcoate a memory for a structure .

xpi0t0s 25Nov2008 20:23

Re: Using auto_ptr or shared_ptr to allocate memory to a Structure
 
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 26Nov2008 09:11

Re: Using auto_ptr or shared_ptr to allocate memory to a Structure
 
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 ??
:confused:

Thanks

xpi0t0s 26Nov2008 14:06

Re: Using auto_ptr or shared_ptr to allocate memory to a Structure
 
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 26Nov2008 15:11

Re: Using auto_ptr or shared_ptr to allocate memory to a Structure
 
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 26Nov2008 15:38

Re: Using auto_ptr or shared_ptr to allocate memory to a Structure
 
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 26Nov2008 16:40

Re: Using auto_ptr or shared_ptr to allocate memory to a Structure
 
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 26Nov2008 16:59

Re: Using auto_ptr or shared_ptr to allocate memory to a Structure
 
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


All times are GMT +5.5. The time now is 06:35.