Hi,
I am trying to teach myself C++, and really appreciate your time & help.
This program is supposed to dynamically create an array of type string that
will hold the actors names. The program compiles okay & begins to execute okay,
but then I get weird memory address stuff for the rest of the program execution
and then the program aborts. Can you please tell me what is causing the weird output.
Sample Output:
matthewmpp@anthonyrogers:/usr/ Data/Notes/LAMP/Programming/Programs/Cpp_progs/Personal$ ./sort_stars_v2
Please enter the number of stars you would like to enter: 5
Please enter a star followed by a '#': Arnold#
Please enter a star followed by a '#': John Wayne#
Please enter a star followed by a '#': Jimmy Stewart#
Please enter a star followed by a '#': Danny Kaye#
Please enter a star followed by a '#': Anne Sheridan#
*** glibc detected *** ./sort_stars_v2: double free or corruption (fasttop): 0x00000000024b4080 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x78a8f)[0x7f214052fa8f]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x73)[0x7f21405338e3]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(_ZNSsD1Ev+0x39)[0x7f2140d83019]
./sort_stars_v2[0x400bd6]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xff)[0x7f21404d5eff]
./sort_stars_v2[0x4009e9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:11 4850366
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
// Program - Sorting Stars with pointers.
#include <iostream>
#include <iomanip>
#include <cctype>
#include <cstring>
#include <string>
using namespace std;
int main ()
{
int num_stars = 0;
cout << "Please enter the number of stars you would like to enter: ";
cin >> num_stars;
string* pstars = 0;
pstars = new string[num_stars];
for(int i = 0; i < num_stars; i++)
{
cout << "Please enter a star followed by a '#': ";
getline(cin, *(pstars+i), '#');
}
delete [] pstars;
delete pstars;
return 0;
}
|