Difference between 'strcpy' and 'strcpy_s'?


error C4996 "strcpy": This function or variable may be unsafe.Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

Bạn đang xem: Difference between 'strcpy' and 'strcpy_s'?

What is the difference between strcpy and strcpy_s?



strcpy is a unsafe funtion.When you try to lớn copy a string using strcpy(), lớn a buffer which is not large enough lớn contain it, it will cause a buffer overflow.

strcpy_s() is a security enhanced version of strcpy().With strcpy_s you can specify the size of the destination buffer lớn avoid buffer overflows during copies.

Xem thêm: Công Ty Truyền Thông Đa Phương Tiện Vtc, Truyền Thông Đa Phương Tiện Là Gì

char tuna<5>; // a buffer which holds 5 chars incluing the null character.char salmon<> = "A string which is longer than 5 chars";strcpy( tuna, salmon ); // This will corrupt your memory because of the buffer overflow.strcpy_s( tuna, 5, salmon ); // strcpy_s will not write more than 5 chars.


I"d like to lớn add that if you ever try khổng lồ compile other people"s code, MS will always complain about unsafe functions in the standard library. Just define _CRT_SECURE_NO_WARNINGS lượt thích the error message tells you lớn & MSVC will work lượt thích any other compiler.


Thanks for contributing an answer khổng lồ Staông chồng Overflow!

Please be sure lớn answer the question. Provide details & nội dung your research!

But avoid

Asking for help, clarification, or responding khổng lồ other answers.Making statements based on opinion; baông chồng them up with references or personal experience.

Xem thêm: Báo Giá Hình Thức Dán Quảng Cáo Trên Ô Tô, Xe Cá Nhân, Grabcar, Becar

To learn more, see our tips on writing great answers.

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to lớn our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged c++ windows visual-c++ or ask your own question.

Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations with _mm_popcnt_u64 on Hãng sản xuất Intel CPUs
site design / logo sản phẩm © 2021 Staông chồng Exchange Inc; user contributions licensed under cc by-sa. rev2021.5.28.39383

Your privacy

By clicking “Accept all cookies”, you agree Staông xã Exchange can store cookies on your device & discthất bại information in accordance with our Cookie Policy.

Chuyên mục: Tổng hợp