Re: auto_ptr array

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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


.



Relevant Pages

  • Re: Window Management
    ... (* prompt is a string resource identifier specifying the string ... PROCEDURE YesNoCancel(prompt: ARRAY OF CHAR; ... VAR INOUT response: ARRAY OF CHAR): BOOLEAN; ...
    (comp.lang.ada)
  • Window Management
    ... (* prompt is a string resource identifier specifying the string ... PROCEDURE YesNoCancel(prompt: ARRAY OF CHAR; ... VAR INOUT response: ARRAY OF CHAR): BOOLEAN; ...
    (comp.lang.ada)
  • Re: Pointer
    ... require in input a pointer char: ... unsafe public static extern int OpenFile; ... What is it pointing to exactly, is it pointing to a "Unicode character" array or is it pointing to a "Single byte character" array or is it pointing to something else? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Array Troubles
    ... puts 'Please type your title for your table (if you type nothing the ... through the array. ... # split turns our string into an array, it uses a comma for a delimiter ...
    (comp.lang.ruby)
  • Re: Problem with va_ macros and arrays of arrays
    ... > the arrays passed to a ... > specific char, somewhat similar to what the standard function ... that with an array of struct, or possibly a pointer to a dynamic array ... > As I'm still a beginner in C without a copy of the standard I ...
    (comp.lang.c)