strcpy
is a unsafe function. When you try to copy a string using strcpy()
to a buffer which is not large enough to 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 to avoid buffer overflows during copies.
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.
Related Posts:
- Windows 7 exception code: 0xc0000409
- VC++ fatal error LNK1168: cannot open filename.exe for writing
- VC++ fatal error LNK1168: cannot open filename.exe for writing
- LPCSTR, LPCTSTR and LPTSTR
- outputting ascii table in C++
- Unresolved external symbol in object files
- Unresolved external symbol in object files
- finding dll for “The specified module could not be found”
- How to use _CRT_SECURE_NO_WARNINGS
- How can I clear console
- How to go from fopen to fopen_s
- undefined reference to WinMain@16 (codeblocks)
- “X does not name a type” error in C++
- make: g++: Command not found
- Exception Error c0000005 in VC++
- Visual Studio debugger error: Unable to start program Specified file cannot be found
- MinGW: “gcc is not recognized as an internal or external command”
- LNK1168: cannot open debug\file.exe for writing
- Identifier is undefined
- How do I add the MinGW bin directory to my system path?
- Run C++ in command prompt – Windows
- LINK : fatal error LNK1561: entry point must be defined ERROR IN VC++
- How to get Current Directory?
- Cannot open Windows.h in Microsoft Visual Studio
- The program can’t start because libgcc_s_dw2-1.dll is missing
- Is there a replacement for unistd.h for Windows (Visual C)?
- LINK : fatal error LNK1561: entry point must be defined ERROR IN VC++
- Win32 Console Application
- https://stackoverflow.com/questions/3865946/error-generic-array-creation
- C++ Compiler Error C2280 “attempting to reference a deleted function” in Visual Studio 2013 and 2015
- error C2601: ‘main’ : local function definitions are illegall – MS VS 2013 Compiler
- cin.eof() functionality
- LINK : fatal error LNK1561: entry point must be defined ERROR IN VC++
- CMake : C and CXX compiler identification is unknown Win10 CMakeGUI
- error LNK2019: unresolved external symbol “” referenced in function
- What is the source of the data for the ProgramFiles, ProgramW6432Dir, ProgramFilesDir (x86), CommonProgramFiles environment variables?
- REGSVR32: the module “xxxxx.dll” failed to load … dependent assembly could not be found
- error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
- Download c++ in existing visual studio 2017
- module unsafe for SAFESEH image C++
- Unexpected end of file error
- Is “delete this” allowed in C++?
- What is a segmentation fault?
- How many spaces for tab character(\t)?
- How to create a dynamic array of integers
- How to create a dynamic array of integers
- Linker Error C++ “undefined reference ” [duplicate]
- Microsoft Visual C++ 14.0 is required (Unable to find vcvarsall.bat)
- How do I build a graphical user interface in C++? [closed]
- C++ convert from 1 char to string?
- How do I build a graphical user interface in C++? [closed]
- convert a char* to std::string
- system(“pause”); – Why is it wrong?
- Pause Console in C++ program
- system(“pause”); – Why is it wrong?
- Pause Console in C++ program
- How to implement 2D vector array?
- Why the switch statement cannot be applied on strings?
- What is the difference between g++ and gcc?
- How to use setprecision in C++
- How to dynamically allocate arrays in C++
- What does (~0L) mean?
- How to dynamically allocate arrays in C++
- What is the best way to use a HashMap in C++?
- What is the best way to use a HashMap in C++?
- What are the differences between a pointer variable and a reference variable in C++?
- What is an undefined reference/unresolved external symbol error and how do I fix it?
- Sleep for milliseconds
- How to convert string to char array in C++?
- How can I convert a std::string to int?
- Easiest way to convert int to string in C++
- What is the difference between float and double?
- Why is “using namespace std;” considered bad practice?
- What is the easiest way to initialize a std::vector with hardcoded elements?
- What is a lambda expression in C++11?
- Vector of Vectors to create matrix
- How to throw a C++ exception
- std::string to char*
- How to generate a random number in C++?
- pinpointing “conditional jump or move depends on uninitialized value(s)” valgrind message
- What is an undefined reference/unresolved external symbol error and how do I fix it?
- How do I find the length of an array?
- Sleep for milliseconds
- How to convert string to char array in C++?
- C++ — expected primary-expression before ‘ ‘
- Using getline() with file input in C++
- What is the effect of extern “C” in C++?
- C++ Cout & Cin & System “Ambiguous” [closed]
- ld: symbol(s) not found for architecture x86_64 error
- C++ string to double conversion
- Why are #ifndef and #define used in C++ header files?
- g++ ld: symbol(s) not found for architecture x86_64
- What exactly is the difference between “pass by reference” in C and in C++?
- How to throw a C++ exception
- Conversion from string to char – c++
- How to find out if an item is present in a std::vector?
- What is meant by Resource Acquisition is Initialization (RAII)?
- When to use extern “C” in simple words? [duplicate]
- What is `CString`?
- Floating point exception( core dump