auto_ptr problem

why am i getting error while destructing array of objects through auto_ptr;
first object was deleting smoothly but the second one is giving problem
plz sort it out..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<string>
#include<memory>
using namespace std;
class Ibuffer{
        string buf;
        static const int i=1;
        public:
        Ibuffer(){cout<<"input :";cin>>buf;}
        ~Ibuffer(){cout<<"destructed i="<<i<<endl;}

};
int main()
{
        auto_ptr<Ibuffer> words=auto_ptr<Ibuffer>(new Ibuffer[2]);
        
        return 0;


}



pavankumar@ubuntu:~/c$ g++ 10.4.7.cpp -o 10.4.7.out
pavankumar@ubuntu:~/c$ ./10.4.7.out
input :pavan
input :kumar
destructed i=1
*** glibc detected *** ./10.4.7.out: munmap_chunk(): invalid pointer: 0x08fa300c ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0x5d3ff1]
/lib/tls/i686/cmov/libc.so.6[0x5d51f5]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xcd06f1]
./10.4.7.out[0x8048bf6]
./10.4.7.out[0x8048a77]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x57fb56]
./10.4.7.out[0x80488f1]
======= Memory map: ========
00110000-0012c000 r-xp 00000000 08:01 4727       /lib/libgcc_s.so.1
0012c000-0012d000 r--p 0001b000 08:01 4727       /lib/libgcc_s.so.1
0012d000-0012e000 rw-p 0001c000 08:01 4727       /lib/libgcc_s.so.1
00152000-00176000 r-xp 00000000 08:01 4832       /lib/tls/i686/cmov/libm-2.10.1.so
00176000-00177000 r--p 00023000 08:01 4832       /lib/tls/i686/cmov/libm-2.10.1.so
00177000-00178000 rw-p 00024000 08:01 4832       /lib/tls/i686/cmov/libm-2.10.1.so
00387000-00388000 r-xp 00000000 00:00 0          [vdso]
00569000-006a7000 r-xp 00000000 08:01 4824       /lib/tls/i686/cmov/libc-2.10.1.so
006a7000-006a9000 r--p 0013e000 08:01 4824       /lib/tls/i686/cmov/libc-2.10.1.so
006a9000-006aa000 rw-p 00140000 08:01 4824       /lib/tls/i686/cmov/libc-2.10.1.so
006aa000-006ad000 rw-p 00000000 00:00 0 
00a8c000-00aa7000 r-xp 00000000 08:01 1723       /lib/ld-2.10.1.so
00aa7000-00aa8000 r--p 0001a000 08:01 1723       /lib/ld-2.10.1.so
00aa8000-00aa9000 rw-p 0001b000 08:01 1723       /lib/ld-2.10.1.so
00c18000-00cfe000 r-xp 00000000 08:01 4627       /usr/lib/libstdc++.so.6.0.13
00cfe000-00d02000 r--p 000e6000 08:01 4627       /usr/lib/libstdc++.so.6.0.13
00d02000-00d03000 rw-p 000ea000 08:01 4627       /usr/lib/libstdc++.so.6.0.13
00d03000-00d0a000 rw-p 00000000 00:00 0 
08048000-08049000 r-xp 00000000 08:01 132422     /home/pavankumar/c/10.4.7.out
08049000-0804a000 r--p 00001000 08:01 132422     /home/pavankumar/c/10.4.7.out
0804a000-0804b000 rw-p 00002000 08:01 132422     /home/pavankumar/c/10.4.7.out
08fa3000-08fc4000 rw-p 00000000 00:00 0          [heap]
b7776000-b7778000 rw-p 00000000 00:00 0 
b7784000-b7788000 rw-p 00000000 00:00 0 
bff53000-bff68000 rw-p 00000000 00:00 0          [stack]
Aborted

You can't put an array in an auto_ptr(). Only one item per auto_ptr().

1
2
3
4
5
6
int main()
{
	auto_ptr<Ibuffer> words(new Ibuffer);

	return 0;
}
You could do this:
1
2
3
4
5
6
7
8
9
10
int main()
{
	auto_ptr<Ibuffer> words[2];

	words[0].reset(new Ibuffer);
	words[1].reset(new Ibuffer);

	return 0;

}
Use a dynamic sequence container, std::vector, std::list, std::deque, or something to that effect. I wouldn't bother trying to play games with auto_ptr for arrays of pointers.
Topic archived. No new replies allowed.