PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > New/Delete and Malloc/Free in C++ code
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
New/Delete and Malloc/Free in C++ code

Réponse
 
LinkBack Outils de la discussion
Vieux 07/02/2008, 14h43   #1
Chris
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut New/Delete and Malloc/Free in C++ code

Hello all,

Does anyone know of or have experienced any problems with using new/delete
and malloc/free in the same same C++ code base? Of course, not using free()
on something you new'd.

Specially, I tend to deal with C-strings a lot and when I want to make a
copy I tend to use the strlen/new char[]/strncpy combination as in:

// s1 may be on the stack or heap it is unknown to the authour
// (i.e. returned for some library, etc.)

s1 = "Some string";

int n = strlen(s1);
char *s2 = new char[n+1];

strncpy(s2, s1, n);
s2[n] = '\0';

I would like to simplify this to simply:

s1 = "Some string"; // same conditions on s1 as above

char *s2 = strdup(s1);

However, the implementation of strdup() that I use uses malloc to duplicate
the string. Thus requiring me to use free() when I no longer want to use
s2.

So, could mixing new/delete and malloc/free in the same program cause
problems? I'm thinking issues with the heap, etc.

Thanks,

--
Chris
  Réponse avec citation
Vieux 07/02/2008, 14h51   #2
Pete Becker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: New/Delete and Malloc/Free in C++ code

On 2008-02-07 09:43:56 -0500, Chris <chris@thisisnotanemailaddress.ca> said:

> // s1 may be on the stack or heap it is unknown to the authour
> // (i.e. returned for some library, etc.)
>
> s1 = "Some string";
>
> int n = strlen(s1);
> char *s2 = new char[n+1];
>
> strncpy(s2, s1, n);
> s2[n] = '\0';


strcpy will work just fine here.

>
> I would like to simplify this to simply:
>
> s1 = "Some string"; // same conditions on s1 as above
>
> char *s2 = strdup(s1);
>
> However, the implementation of strdup() that I use uses malloc to duplicate
> the string. Thus requiring me to use free() when I no longer want to use
> s2.
>
> So, could mixing new/delete and malloc/free in the same program cause
> problems? I'm thinking issues with the heap, etc.
>


No, there are no inherent problems.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

  Réponse avec citation
Vieux 07/02/2008, 14h53   #3
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: New/Delete and Malloc/Free in C++ code

Chris wrote:
> Does anyone know of or have experienced any problems with using
> new/delete and malloc/free in the same same C++ code base? Of course,
> not using free() on something you new'd.
>
> Specially, I tend to deal with C-strings a lot and when I want to
> make a copy I tend to use the strlen/new char[]/strncpy combination
> as in:
>
> // s1 may be on the stack or heap it is unknown to the authour
> // (i.e. returned for some library, etc.)
>
> s1 = "Some string";
>
> int n = strlen(s1);
> char *s2 = new char[n+1];
>
> strncpy(s2, s1, n);
> s2[n] = '\0';
>
> I would like to simplify this to simply:
>
> s1 = "Some string"; // same conditions on s1 as above
>
> char *s2 = strdup(s1);
>
> However, the implementation of strdup() that I use uses malloc to
> duplicate the string. Thus requiring me to use free() when I no
> longer want to use s2.


That's fine since the type is 'char'.

> So, could mixing new/delete and malloc/free in the same program cause
> problems? I'm thinking issues with the heap, etc.


Nope. Beyond the fact that 'strdup' is your own non-C++ function
which you use at your own risk, using new/delete (or new[]/delete[])
along with malloc/free is perfectly OK. Just don't use 'malloc'
for any non-POD types.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 07/02/2008, 15h05   #4
Chris
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: New/Delete and Malloc/Free in C++ code

Pete Becker wrote:

> On 2008-02-07 09:43:56 -0500, Chris <chris@thisisnotanemailaddress.ca>
> said:
>> ...
>>
>> int n = strlen(s1);
>> char *s2 = new char[n+1];
>>
>> strncpy(s2, s1, n);
>> s2[n] = '\0';

>
> strcpy will work just fine here.
>


That's true. I tend to use strncpy() out of habit.

>> ...
>>
>> So, could mixing new/delete and malloc/free in the same program cause
>> problems? I'm thinking issues with the heap, etc.
>>

>
> No, there are no inherent problems.
>


Thanks for the confirmation.

--
Chris
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h19.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10732 seconds with 12 queries