강의

멘토링

커뮤니티

Inflearn Community Q&A

wjdgus26251771's profile image
wjdgus26251771

asked

[10 minutes a day | C++] Introduction to C++ programming that anyone can easily learn

Pointer arithmetic

delete 연산자 관련하여

Written on

·

340

3

char animal[SIZE];

char* ps;

cout << "동물 이름을 입력하십시오\n";

cin >> animal;

ps = new char[strlen(animal) + 1];

strcpy(ps, animal);

cout << "입력하신 동물 이름을 복사하였습니다." << endl;

cout << "입력하신 동물 이름은 " << animal << " 이고 그 주소는 " << (int*)animal << " 입니다. " << endl;

cout << "복사된 동물 이름은 " << ps << " 이고 그 주소는 " << (int*)ps << "입니다." << endl;

delete[] ps; // 예제에는 없던 문장입니다,

위와 다르게 delete[] ps; 문장을 삽입하지 않아도 무방한가요? 

C++

Answer 2

0

wjdgus26251771님의 프로필 이미지
wjdgus26251771
Questioner

감사합니다 !!

0

pandacoding님의 프로필 이미지
pandacoding
Instructor

아닙니다.

무방하지 않습니다!!

new연산자는 반드시 delete 연산자와 함께 사용되어야 합니다.

제가 놓쳤네요.. 부끄럽습니다.

예제 코드는 수정해놓겠습니다! 감사합니다 :)

wjdgus26251771's profile image
wjdgus26251771

asked

Ask a question