C++ Primer Plus(第五版)所有例题的代码
源代码在线查看: delete.cpp
// delete.cpp -- using the delete operator
#include
#include // or string.h
using namespace std;
char * getname(void); // function prototype
int main()
{
char * name; // create pointer but no storage
name = getname(); // assign address of string to name
cout delete [] name; // memory freed
name = getname(); // reuse freed memory
cout delete [] name; // memory freed again
return 0;
}
char * getname() // return pointer to new string
{
char temp[80]; // temporary storage
cout cin >> temp;
char * pn = new char[strlen(temp) + 1];
strcpy(pn, temp); // copy string into smaller space
return pn; // temp lost when function ends
}