Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

about pointer and string(strdup)

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Dear all,

In my program:

int main()
{
....
char *tmp1=new char[10];
strcpy(tmp1,"abcdefgh");
..........
char *tmp2=strdup(Somefunction(tmp1));
....//use tmp2 and tmp1 in the code below

return 0;

}

char* Somefunction(char *instr)
{
outstr=strdup(instr);
.....//do some manipulation on outstr
.....//such that the original string is not distorted
return outstr;
}

question 1:when I use strdup, do I need to check whether the return value is null,
just like malloc?
question 2:how can I free the outstr?

Thank you very much!

 
strdup() uses malloc() therefore you should presumably test that the result is not null. And you need to free the string with free().
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top