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
- How to create a dynamic array of integers
- Making my own toString() method on c++ struct
- Struct Constructor in C++?
- What is meant with “const” at end of function declaration? [duplicate]
- What’s the C++ version of Java’s ArrayList
- What is the difference between “long”, “long long”, “long int”, and “long long int” in C++?
- ‘g++’ is not recognized as an internal or external command, operable program or batch file
- How do I convert a double into a string in C++?
- How to create timer events using C++ 11?
- How to concatenate two strings in C++?
- ‘g++’ is not recognized as an internal or external command, operable program or batch file
- What does T&& (double ampersand) mean in C++11?
- C++ – No matching member function for call to ‘push_back’
- What is the difference between const int*, const int * const, and int const *?
- How to convert a single char into an int [duplicate]
- Exception Handling in C++ Terminate called after throwing an instance of ‘char const*’
- c++ array – expression must have a constant value
- getline() does not work if used after some inputs
- Passing a 2D array to a C++ function
- ‘typeid’ versus ‘typeof’ in C++
- Fatal error: iostream: No such file or directory in compiling C program using GCC
- Thread pooling in C++11
- Callback functions in C++
- Reading from text file until EOF repeats last line
- How does std::forward work? [duplicate]
- Examples of good gotos in C or C++
- Error: Jump to case label in switch statement
- What does int & mean
- Split a string into an array in C++
- splitting a string into an array in C++ without using vector
- Remove spaces from std::string in C++
- How to use glOrtho() in OpenGL?
- error: ‘ostream’ does not name a type
- Remove spaces from std::string in C++
- Read data from a file into an array – C++
- C++ Fractions Class
- #pragma once vs include guards?
- Debug vs Release in CMake
- C++ Remove punctuation from String
- prototype for “….” does not match any in class “…”
- I get this error: “glibc detected”
- How can I get and use the header file
in my C++ program? - Implementing a tree in C++
- Drawing Circle with OpenGL
- Run-Time Check Failure #2 – Stack around the variable ‘foo’ was corrupted
- How to implement “Press Any Key To Exit”
- Reading and writing binary file
- C++ : Exception occurred in script: basic_string::_S_construct NULL not valid
- Xcode 11.1: iostream’ file not found
- non-member function cannot have cv-qualifier
- too many initializers for ‘int [0]’ c++
- Why do I get: -9223372036854775808
- Return a 2d array from a function
- Linker error: “linker input file unused because linking not done”, undefined reference to a function in that file
- What is the difference among ios::app, out, and trunc in c++?
- Where can I find ‘winmm.lib’ (I’m using Visual Studio 2012)
- What is a `char*`?
- Does static constexpr variable inside a function make sense?
- “Incomplete type not allowed ” when creating std::ofstream objects