xpi0t0s's Avatar, Join Date: Aug 2004
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);

Why do you do a memset before the strcpy? Is freshly allocated memory "dirty" and needs "cleaning" before it can be used? The strcpy overwrites all allocated memory.

Why does strcpyDCE take a numeric parameter (30)? Does this do something significantly different from strncpy?

dce_sTabName = sTabName.c_str();

Why not just do dce_sTabName = sTabName; if dce_sTabName is a std::string? Strings are designed to work that way.
ronan_40060's Avatar
Go4Expert Member
memset is done to initialize it to '\0' . strcpyDCE takes 30 s that the maximum allocated bytes to be copied is 30 only ,
Im declaring dce_sTabName as std::String dce_sTabName;
but sTabName is an char array so i thought of doing dce_sTabName = sTabName.c_str();

The other question is .. I have used auto_ptr declaration like
auto_ptr<SYS_CORRESP_STRUCT>p_str_Corresp (new SYS_CORRESP_STRUCT); which gets compiled without any issues as
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> &'.
xpi0t0s's Avatar, Join Date: Aug 2004
sTabName can't be a char array because (char*)::c_str() doesn't exist. On the other hand, std::string::c_str() *does* exist, so the fact that you refer to sTabName.c_str(); shows that sTabName isn't a char array, might be a std::string, and is definitely a class that defines c_str() even if it's not a std::string.

auto..sp(new SYS..) works because there is a constructor defined that takes a pointer to SYS.. (can't be bothered with all that typing, so using ".." to abbreviate the names)

On the other hand, there is no auto_ptr<SYS..>:perator=(SYS..*), so you get the error.

By the way, new T is fine; you don't need to do new (T). Actually the latter is the syntax for placement new, which you really don't want to start using yet. Get the simple stuff sorted before going onto that kind of stuff.

> memset is done to initialize it to '\0'

Yeah, I know what memset does. Let me try a parallel question:
int x; // x contains junk
x=0; // equivalent to your memset. We set it to zero.
x=2; // we wanted to initialise it to 2, but we felt we needed to initialise it to zero first.

In this code the x=0; is completely redundant; we can safely set x to 2 without having to set it to zero first. As is your memset call. Hence the question: why do you feel you need to do that?
ronan_40060's Avatar
Go4Expert Member
many problems are solved thank so much for shaing ur knowledge and cleaing my doubts .. Sorry for not being active on the post