Re: auto_ptr array
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Mon, 7 Apr 2008 08:56:51 -0500
George wrote:
Sorry Ben,
It is my typo and carelessness in my testing. I have made a couple of
testing and forget to change the length when I write the specific
test case I posted here. :-)
Now fixed. Output is a followed by space and ? -- I think they are not
displayable characters, right?
So, the conclusion is, puts function will dump character until NULL is
found? Right?
That's the contract for puts.
However, the problem with your array of auto_ptr to automatically destroy an
array is that:
puts will read every element of a real array or vector or any other datatype
with contiguous storage, stopping at the first NUL character.
If you hand your auto_ptr array to puts, you get the first element, followed
by whatever comes next in memory, *not whatever comes next in the array*.
And you still aren't even trying to pass a NUL character to puts. You're
lucky the process didn't crash with an access violation.
Here is the code I fixed.
#include <stdio.h>
#include <memory>
using namespace std;
int main( void )
{
auto_ptr<char> myAutoptrs [2];
char* p1 = new char;
char* p2 = new char;
memcpy (p1, "a", 1);
memcpy (p2, "b", 1);
myAutoptrs [0].reset (p1);
myAutoptrs [1].reset (p2);
puts (myAutoptrs[0].get()); // a ?, no heap
corruption
return 0;
}
regards,
George
.
- References:
- auto_ptr array
- From: George
- Re: auto_ptr array
- From: Igor Tandetnik
- Re: auto_ptr array
- From: George
- Re: auto_ptr array
- From: Ben Voigt [C++ MVP]
- Re: auto_ptr array
- From: George
- Re: auto_ptr array
- From: Ben Voigt [C++ MVP]
- Re: auto_ptr array
- From: George
- Re: auto_ptr array
- From: Ben Voigt [C++ MVP]
- Re: auto_ptr array
- From: George
- auto_ptr array
- Prev by Date: Re: Want help regarding : "disable bluetooth device"
- Next by Date: where is _ATL_SIMPLEMAPENTRY defined?
- Previous by thread: Re: auto_ptr array
- Next by thread: Re: auto_ptr array
- Index(es):
Relevant Pages
|