C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Better way of assigning memory

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Amit_Basnak
Guest





PostPosted: Fri May 18, 2007 9:10 am    Post subject: Better way of assigning memory Reply with quote



Dear Friend which one is the better way of assigning memory and
releasing it
//WF_STRUCT_WORKUNIT is a Structure

WF_STRUCT_WORKUNIT *p_str_Workunit = (WF_STRUCT_WORKUNIT *) new char
[ sizeof(WF_STRUCT_WORKUNIT) ];
if (p_str_Workunit!)
{

TError error( TError ( CString( __FILE__ )
,__LINE__
,TRM_NO_MEMORY
,0
,CString("") ));

throw new tException(error);
}

conObj.CopyToDceWF_STRUCT_WORKUNIT(p_str_Workunit,pStrWorkUnit); //
function call

OR

WF_STRUCT_WORKUNIT *p_str_Workunit = (WF_STRUCT_WORKUNIT
*)malloc(sizeof(WF_STRUCT_WORKUNIT));
memset(p_str_Workunit,'\0',sizeof(WF_STRUCT_WORKUNIT));
conObj.CopyToDceWF_STRUCT_WORKUNIT(p_str_Workunit,pStrWorkUnit); //
function call

Thanks
Amit
Back to top
Salt_Peter
Guest





PostPosted: Fri May 18, 2007 9:11 am    Post subject: Re: Better way of assigning memory Reply with quote



On May 18, 3:01 am, Amit_Basnak <Amit.Bas...@gmail.com> wrote:
Quote:
Dear Friend which one is the better way of assigning memory and
releasing it
//WF_STRUCT_WORKUNIT is a Structure

WF_STRUCT_WORKUNIT *p_str_Workunit = (WF_STRUCT_WORKUNIT *) new char
[ sizeof(WF_STRUCT_WORKUNIT) ];
if (p_str_Workunit!)
{

TError error( TError ( CString( __FILE__ )
,__LINE__
,TRM_NO_MEMORY
,0
,CString("") ));

throw new tException(error);
}

conObj.CopyToDceWF_STRUCT_WORKUNIT(p_str_Workunit,pStrWorkUnit); //
function call

OR

WF_STRUCT_WORKUNIT *p_str_Workunit = (WF_STRUCT_WORKUNIT
*)malloc(sizeof(WF_STRUCT_WORKUNIT));
memset(p_str_Workunit,'\0',sizeof(WF_STRUCT_WORKUNIT));
conObj.CopyToDceWF_STRUCT_WORKUNIT(p_str_Workunit,pStrWorkUnit); //
function call

Thanks
Amit

A name for a struct like WF_STRUCT_WORKUNIT is silly and all uppercase
should be reserved for constants. Try something like:
struct WorkUnit { ... };

Replace all of the above code by providing an appropriate default
ctor, copy ctor and op== for class/struct WorkUnit.
And do as follows:

#include <vector>

int main()
{
std::vector< WorkUnit > v(100); // done, 100 WorkUnits
}

Just in case you fail to see the light:

#include <vector>
#include <string>

int main()
{
// 1000 doubles all initilixed to 1.1
std::vector< double > vd(1000, 1.1);
// 100 strings all initialized to 'default'
std::vector< string > vs(100, "default");
}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.