你的程序有兩種錯誤:
- 你是你的函數裏面cpytoheap沒有分配足夠的內存()。
- 您已經使用new分配了內存,因此您應該使用delete。如果您已使用new []分配,則只能使用delete []。
要理解這些問題,您可以在Windows平臺上使用GNU/Linux或WinDBG/PageHeap上的valgrind。以下報告由valgrind爲您的程序生成。
[email protected]:~$ valgrind ./a.out
==4575== Memcheck, a memory error detector
==4575== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==4575== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==4575== Command: ./a.out
==4575==
==4575== Invalid write of size 1
==4575== at 0x400A12: cpytoheap(char const*) (pratice.cpp:10)
==4575== by 0x400A4E: main (pratice.cpp:19)
==4575== Address 0x5a08041 is 0 bytes after a block of size 1 alloc'd
==4575== at 0x4C2B1C7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4575== by 0x4009D5: cpytoheap(char const*) (pratice.cpp:6)
==4575== by 0x400A4E: main (pratice.cpp:19)
==4575==
==4575== Invalid write of size 1
==4575== at 0x400A00: cpytoheap(char const*) (pratice.cpp:9)
==4575== by 0x400A4E: main (pratice.cpp:19)
==4575== Address 0x5a08041 is 0 bytes after a block of size 1 alloc'd
==4575== at 0x4C2B1C7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4575== by 0x4009D5: cpytoheap(char const*) (pratice.cpp:6)
==4575== by 0x400A4E: main (pratice.cpp:19)
==4575==
==4575== Invalid read of size 1
==4575== at 0x4C2BFB4: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4575== by 0x4EC7288: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18)
==4575== by 0x400A63: main (pratice.cpp:20)
==4575== Address 0x5a08041 is 0 bytes after a block of size 1 alloc'd
==4575== at 0x4C2B1C7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4575== by 0x4009D5: cpytoheap(char const*) (pratice.cpp:6)
==4575== by 0x400A4E: main (pratice.cpp:19)
==4575==
==4575== Invalid read of size 1
==4575== at 0x53C8132: _IO_default_xsputn (genops.c:485)
==4575== by 0x53C6069: [email protected]@GLIBC_2.2.5 (fileops.c:1393)
==4575== by 0x53BBCDC: fwrite (iofwrite.c:45)
==4575== by 0x4EC6FD4: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18)
==4575== by 0x4EC7296: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18)
==4575== by 0x400A63: main (pratice.cpp:20)
==4575== Address 0x5a08041 is 0 bytes after a block of size 1 alloc'd
==4575== at 0x4C2B1C7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4575== by 0x4009D5: cpytoheap(char const*) (pratice.cpp:6)
==4575== by 0x400A4E: main (pratice.cpp:19)
==4575==
==4575== Mismatched free()/delete/delete []
==4575== at 0x4C2A09C: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4575== by 0x400A76: main (pratice.cpp:21)
==4575== Address 0x5a08040 is 0 bytes inside a block of size 1 alloc'd
==4575== at 0x4C2B1C7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4575== by 0x4009D5: cpytoheap(char const*) (pratice.cpp:6)
==4575== by 0x400A4E: main (pratice.cpp:19)
==4575==
asdf
q
歡迎來到Stack Overflow。請儘快閱讀[關於]頁面。你爲什麼只分配一個角色?你在哪裏調用'cpytoheap()' - 你的'main()'調用'strdupa()'?你爲什麼不用'x [i] = y [i];'函數中的符號?循環後應該設置空字節;你在循環體內浪費時間做它。 –
你根本沒有使用cpytoheap()。 – Chuck
你似乎沒有調用'cpytoheap'。而且由於'z'沒有用'new []'分配,所以用'delete []'釋放它是個錯誤。 – TypeIA