c++ better to use new/delete and not malloc

This commit is contained in:
Stanislav Shwartsman 2016-12-05 19:05:31 +00:00
parent 0059d33102
commit 98c69ce6ae
2 changed files with 7 additions and 9 deletions

View File

@ -3443,11 +3443,11 @@ BxEvent *enh_dbg_notify_callback(void *unused, BxEvent *event)
static size_t strip_whitespace(char *s)
{
size_t ptr = 0;
char *tmp = (char*)malloc(strlen(s)+1);
char *tmp = new [strlen(s)+1];
strcpy(tmp, s);
while (s[ptr] == ' ') ptr++;
if (ptr > 0) strcpy(s, tmp+ptr);
free(tmp);
delete [] tmp;
ptr = strlen(s);
while ((ptr > 0) && (s[ptr-1] == ' ')) {
s[--ptr] = 0;

View File

@ -206,14 +206,12 @@ int main (int argc, char **argv)
#if !BX_HAVE_STRDUP
/* XXX use real strdup */
char *bx_strdup(const char *str)
char *bx_strdup(const char *s)
{
char *temp = (char*)malloc(strlen(str)+1);
sprintf(temp, "%s", str);
return temp;
// Well, I'm sure this isn't how strdup is REALLY implemented,
// but it works...
char *p = malloc (strlen (s) + 1); // allocate memory
if (p != NULL)
strcpy (p,s); // copy string
return p; // return the memory
}
#endif /* !BX_HAVE_STRDUP */