내 프로그램을 컴파일하고 실행할 때 "런타임 오류 검사 # 2"오류가 발생하는 것을 제외하면 모든 작업이 정상적으로 작동하는 것처럼 보입니다. 이것은 나의 hw 임무이며, cstring 함수 중 하나를 사용하려고 시도한 것은 처음 이었기 때문에 그것이 잘못되었다고 확신합니다. 기본적으로 나는 2 개의 문자열을 함께 추가하고 있는데, 거의 100 %는 오버런을 일으키는 결과 매개 변수와 관련이 있다는 것을 확신합니다. 그것을 고치는 법을 모른다. num
인수 대상의 전체 크기가 아니다, 그것은 복사 할 바이트 수 있기 때문에strncpy 런타임 검사 오류 # 2 오류 C++
#include <iostream>
#include <cstring>
using namespace std;
void concat(const char a[ ], const char b[ ], char result[ ], int result_maxlength);
int main()
{
char a[] = "Woozle";
char b[] = "Heffalump";
char c[5];
char d[10];
char e[20];
concat(a, b, c, 5);
concat(a, b, d, 10);
concat(a, b, e, 20);
cout << c << "\n";
cout << d << "\n";
cout << e << "\n";
return 0;
}
void concat(const char a[ ], const char b[ ], char result[ ], int result_maxlength)
{
strncpy (result,a, result_maxlength);
strncat (result, b, result_maxlength);
result[result_maxlength-1] = '\0';
}
이전 질문과 다른 점은 무엇입니까? http://stackoverflow.com/questions/12217528/strncat-strncpy-help-c?rq=1 –