Za mało miejsca!?

0

Witam przy takim kodzie:

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
	char t1[] = { "tekst tablicy t1 " };
	char t2[120];
	strcpy_s(t1,t2);
	cout << t2 << endl;
	strncpy_s(t1, "strncpy t1", 3 );
	cout << t2 << endl;
	strcpy_s(t1, "strcpy t1 ");
	cout << t1 << " i " << t2 << endl;
	system("pause");
} 

Odpala sie DoS lecz się zawiesza i wywala błąd okienkowy coś że za mało miejsca a w Microsoft Visual C++ expres 2010 wywala mi coś takiego:

/***
*tcscpy_s.inl - general implementation of _tcscpy_s
*

  •   Copyright (c) Microsoft Corporation. All rights reserved.
    

*Purpose:

  •   This file contains the general algorithm for strcpy_s and its variants.
    

****/

_FUNC_PROLOGUE
errno_t __cdecl _FUNC_NAME(_CHAR *_DEST, size_t _SIZE, const _CHAR *_SRC)
{
_CHAR *p;
size_t available;

/* validation section */
_VALIDATE_STRING(_DEST, _SIZE);
_VALIDATE_POINTER_RESET_STRING(_SRC, _DEST, _SIZE);

p = _DEST;
available = _SIZE;
while ((*p++ = *_SRC++) != 0 && --available > 0)
{
}

if (available == 0)
{
    _RESET_STRING(_DEST, _SIZE);
    _RETURN_BUFFER_TOO_SMALL(_DEST, _SIZE);
}
_FILL_STRING(_DEST, _SIZE, _SIZE - available + 1);
_RETURN_NO_ERROR;

}

0

Użyj strcpy zamiast strcpy_s i pamiętaj o poprawnej kolejnośći argumentów - strcpy(dokąd, skąd); W 3 linijcie maina masz źle podaną kolejność argumentów.

0

Jak używam

strcpy 

zamiast strcpy_s

 to wywala komunikaty że to nie zbyt bezpieczne i by używać jednak <code class="cpp"> strcpy_s
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
	char t1[] = { "czesc o whoa"};
	char t2[120];
	strcpy(t2,t1);
	cout << t2 << endl;
	strncpy(t2, "narka! ", 9);
	cout << t2 << endl;
	strcpy(t2, "--aku-ku--!");
	cout << t2 << endl;
	system("pause");
} 

1>------ Build started: Project: Zadania12, Configuration: Debug Win32 ------
1> glowna.cpp
1>d:\inne\programowanie\c++\zadania12\zadania12\glowna.cpp(8): warning 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.
1> d:\programy\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
1>d:\inne\programowanie\c++\zadania12\zadania12\glowna.cpp(10): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> d:\programy\microsoft visual studio 10.0\vc\include\string.h(188) : see declaration of 'strncpy'
1>d:\inne\programowanie\c++\zadania12\zadania12\glowna.cpp(12): warning 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.
1> d:\programy\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
1> Zadania12.vcxproj -> D:\Inne\Programowanie\C++\Zadania12\Debug\Zadania12.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

0

Ale ty nie tylko zmieniłeś nazwy funkcji, ale zmieniłeś kolejność argumentów! Było strcpy_s(t1,t2); a jest strcpy(t2,t1);.
Skoro kopiujesz coś o niekreślonej wartości (co penie nie jest zakoćzone zerem) to nie dziwne, że masz crash'a.

Twój błąd w pierwszym programie wynika ze złego kierunku kopiowania (śmieci), nic więcej.

0

Ogólnie to chyba najlepiej użyć strncpy().

1 użytkowników online, w tym zalogowanych: 0, gości: 1