Не могу удалить указатель в C ++ - PullRequest
0 голосов
/ 16 марта 2019

Я получил этот код из книги.Когда я запускал Visual Studio, он говорил, чтобы переключить strcpy () на strcpy_s (), и после того, как я это сделал, кажется, что программа остановилась на указателе удаления.Я пытался запустить на Dev-C ++, и он отлично работает.Кто-нибудь знает почему?Спасибо.

#include "pch.h"
#include <iostream>
#include <cstring>

int main()
{
    cout << "Enter a kind of animal: ";
    cin >> animal; // ok if input < 20 chars

    ps = animal; // set ps to point to string
    cout << ps << "!\n"; // ok, same as using animal
    cout << "Before using strcpy():\n";
    cout << animal << " at " << (int *)animal << endl;
    cout << ps << " at " << (int *)ps << endl;

    ps = new char[strlen(animal) + 1]; // get new storage
    strcpy_s(ps, sizeof(animal), animal); // copy string to new storage

    cout << "After using strcpy():\n";
    cout << animal << " at " << (int *)animal << endl;
    cout << ps << " at " << (int *)ps << endl;
    delete[] ps;
    return 0;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...