Alex Rivera | Logout

Is there any safe strcmp?

Asked 2009-10-26T09:02:21.873
16

I made a function like this:

bool IsSameString(char* p1, char* p2) 
{
     return 0 == strcmp(p1, p2);
}

The problem is that sometimes, by mistake, arguments are passed which are not strings (meaning that p1 or p2 is not terminated with a null character). Then, strcmp continues comparing until it reaches non-accessible memory and crashes. Is there a safe version of strcmp? Or can I tell whether p1 (and p2) is a string or not in a safe manner?

Edit
Report

1 Answer

3

you can use strncmp, But if possible use std::string to avoid many problems :)

answered 2009-10-26T09:07:02.250

Your Answer