The compiler says 2, but in theory we refer to one memory area three times.
#include <iostream> #include <cstring> #include <cstdlib> using namespace std; class sample { char *s; public: sample() { s = 0; } ~sample() { if(s) delete [] s; cout << "Освобождение s-памяти.\n"; } void show() { cout << s << "\n"; } void set(char *str); }; // Загрузка строки. void sample::set(char *str) { s = new char[strlen(str)+1]; strcpy(s, str); } //Эта функция возвращает объект типа sample. sample input() { char instr[80]; sample str; cout << "Введите строку: "; cin >> instr; str.set(instr); return str; } int main() { sample ob; // Присваиваем объект, возвращаемый // функцией input(), объекту ob. ob = input(); // Эта инструкция генерирует ошибку(или вывод мусор) ob.show(); // Отображение "мусора". return 0; }