 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
(2b|!2b)==? Guest
|
Posted: Wed Dec 17, 2008 6:19 pm Post subject: _strdup and delete |
|
|
is it safe to deallocate memory allocated with _strdup?
I suspect that _strdup alocates memory using malloc/calloc behind the
scenes - what are the dangers of using delete to free _strdup allocated
memory? |
|
| Back to top |
|
 |
Jeff Schwab Guest
|
Posted: Wed Dec 17, 2008 6:19 pm Post subject: Re: _strdup and delete |
|
|
Andrey Tarasevich wrote:
| Quote: | If the memory is allocated with 'malloc/calloc', then you simply cannot
deallocate if with 'delete', period. There's not much sense in
discussing the "dangers" of trying to do so.
|
I'm not aware of any guarantee that deleting malloc'd memory will not
free the memory. In fact, this is exactly what happens on some
platforms. "Simply cannot" is overstating the case. |
|
| Back to top |
|
 |
Jeff Schwab Guest
|
Posted: Wed Dec 17, 2008 6:19 pm Post subject: Re: _strdup and delete |
|
|
(2b|!2b)==? wrote:
| Quote: | is it safe to deallocate memory allocated with _strdup?
|
As Kai-Uwe pointed out, POSIX strdup uses malloc, and you're supposed to
free the returned memory. GNU provides an strdupa function that uses
memory in the local stack frame, and is freed when the current function
returns.
Why are you calling _strdup, rather than using std::strings?
| Quote: | I suspect that _strdup alocates memory using malloc/calloc behind the
scenes - what are the dangers of using delete to free _strdup allocated
memory?
|
At best, your code will be non-portable. At worst, Bad Things can
happen (undefined behavior). |
|
| Back to top |
|
 |
Andrey Tarasevich Guest
|
Posted: Wed Dec 17, 2008 6:19 pm Post subject: Re: _strdup and delete |
|
|
(2b|!2b)==? wrote:
| Quote: | is it safe to deallocate memory allocated with _strdup?
|
Well, the real question is whether you _should_ deallocate that memory,
i.e. whether you _own_ it once it is allocated. You have to read the
docs in order to find that out. '_strdup' is not a standard function.
| Quote: | I suspect that _strdup alocates memory using malloc/calloc behind the
scenes - what are the dangers of using delete to free _strdup allocated
memory?
|
If the memory is allocated with 'malloc/calloc', then you simply cannot
deallocate if with 'delete', period. There's not much sense in
discussing the "dangers" of trying to do so.
--
Best regards,
Andrey Tarasevich |
|
| Back to top |
|
 |
Kai-Uwe Bux Guest
|
Posted: Wed Dec 17, 2008 6:19 pm Post subject: Re: _strdup and delete |
|
|
(2b|!2b)==? wrote:
| Quote: | is it safe to deallocate memory allocated with _strdup?
I suspect that _strdup alocates memory using malloc/calloc behind the
scenes - what are the dangers of using delete to free _strdup allocated
memory?
|
The standard does not mention _strdup.
The same applies to strdup, which is neither defined in the C++ standard nor
in the referenced C standard. However, that seems to be POSIX. According to
the man page, it obtains memory via malloc(), and the memory can be
released via free(). I suggest, you find the documentation for _strdup. It
should say something about this issues.
Best
Kai-Uwe Bux |
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|