25 / 23 / 2
Регистрация: 13.12.2009
Сообщений: 88
1
30.06.2011, 21:42. Показов 11693. Ответов 6
Решил после сдачи курсовой, для себя сделать курсовую до конца, и брать не честно стыренную у соседа по парте, и переделать, а написать свою. И всё бы хорошо, но вот столкнулся с проблемой…
Перерыл весь гугл, но до меня так и не дошло…
Решил сделать несколько языков в программе, для теста делаю отдельно, и вот собственно сама проблема:
warning C4996: ‘strcpy‘: This function or variable may be unsafe.
Гугл перерыл, нашел несколько решений, но не помогают…
Может кто объяснить в чём моя ошибка?
| C++ | ||
|
Для чего такое построение с функциями? enum улетит в хедер файл, massage_prog() в отдельный cpp файл, main () — в главный файл программы.
__________________
Помощь в написании контрольных, курсовых и дипломных работ, диссертаций здесь
0
|
|
|
|

Strcpy и strncpy не робит…
- Подписаться на тему
- Сообщить другу
- Скачать/распечатать тему
|
|
|
|
вот что за фигня у меня высвечивается? я в одной программе всё также сделал и всё пошло, а в этой уже ошибка… пишет: 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. |
|
KILLER |
|
|
Цитата Potroshitell @ 01.07.09, 07:37 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. Цитата Potroshitell @ 01.07.09, 07:37 я же вроди бы правильно её использую: strcpy(label , «none»); У тебя ведь в описании ворнинга все написано!!! |
|
popsa |
|
|
используй strcpy_s, strncpy_s! |
|
Potroshitell |
|
|
всё правильно говоришь) я об этом тоже знаю, и просто я этот пример привёл неудачный)) на самом деле у меня вот как там дело то обстоит:
// в заголовочном файле label обьявлен как указатель на char… label = new char[20]; strncpy(label , s2 , 19); label[19] = »; // ну и что компилятору тут не нравится? |
|
Urich |
|
|
Senior Member
Рейтинг (т): 20 |
Potroshitell |
|
KILLER |
|
|
Цитата Potroshitell @ 01.07.09, 07:50 // ну и что компилятору тут не нравится? ему все нравиться, он тебя просто предупреждает о том что использование strncpy может привести к проблемам, и даже приводит тебе пару выходов из этой ситуации, как ты поступишь дело твое, можешь отрубить этот ворнинг а можешь заменить свои две строчки: Цитата Potroshitell @ 01.07.09, 07:50
strncpy(label , s2 , 19); label[19] = »; одной:
strncpy_s(label, s2, 20); |
|
Potroshitell |
|
|
ок) хорошо, я с этим разобрался
… void Bravo (const Cd & obj); int main() { Classic c2(«Fantasia in C» , «Alfred Brendel» , 2 , 57.17 , «Philips»); Bravo(c2); … } void Bravo (const Cd & obj) { … } я кажется знаю в чём тут проблема… но как её решить? Classic наследует Cd, всмысле Cd базовый класс… поидее он должен преобразовывать Classic в Cd в функции Bravo… но почему то он этого не делает… может из-за того, что я забыл обьявить конструктор копирования? Сообщение отредактировано: Potroshitell — 01.07.09, 08:06 |
|
KILLER |
|
|
Цитата Potroshitell @ 01.07.09, 08:01 Classic наследует Cd, всмысле Cd базовый класс… поидее он должен преобразовывать Classic в Cd в функции Bravo… но почему то он этого не делает… Чего то ты недоговариваешь, кусок кода где эти классы объявлены и используються покажи, и откуда у тебя взялсо «Classic *» |
|
Potroshitell |
|
|
// classic.h #ifndef _CLASSIC_H_ #define _CLASSIC_H_ #include «cd.h» class Classic : Cd { private: char zadacha[40]; public: Classic(char * s1, char * s2, int n, double x, char * zad); Classic(); virtual void report() const; virtual ~Classic(); }; #endif
// реализация классика #include <iostream> using namespace std; #include <cstring> #include «cd.h» #include «Classic.h» Classic::Classic(char * s1, char * s2, int n, double x, char * zad) : Cd(s1, s2, n, x) { strncpy(zadacha ,zad, 39); zadacha[39] = »; } Classic::Classic() : Cd() { strcpy(zadacha, «none»); } Classic::~Classic() { } void Classic::report() const { Cd::report(); cout << » Zadacha = » << zadacha << endl; } а базовый класс нужен? |
|
Мяут |
|
|
Преобразовываться ничего и никогда не должно.
class A { public: void foo() const; }; class B : public A { public: void bar() const; }; void g(const A* a) { B* b; a->foo(); if(b = dynamic_cast<B*>(a)) { b->bar(); } } |
|
Potroshitell |
|
|
тьфу блин!!! |
|
KILLER |
|
|
дык… Цитата Potroshitell @ 01.07.09, 08:14 class Classic : Cd Ты юзаешь закрытое наследование, напиши так:
class Classic : public Cd { … }; |
0 пользователей читают эту тему (0 гостей и 0 скрытых пользователей)
0 пользователей:
- Предыдущая тема
- C/C++: Общие вопросы
- Следующая тема
[ Script execution time: 0,0672 ] [ 16 queries used ] [ Generated: 29.01.23, 02:08 GMT ]
- Forum
- General C++ Programming
- Strncpy Error
Strncpy Error
Im receiving a error C4996 and says it is unsafe to use «strncpy», how can I fix this?
If you are using Visual Studio you can #define _CRT_SECURE_NO_WARNINGS before your #include files.
Not recommended. It is using a bomb to swat a fly. And doesn’t fix the problems with the functions, you can still have buffer overruns.
OR
Use C11’s strncpy_s() function that was created to prevent buffer overruns.
See TheIdeasMan’s link for how to use.
At the design level, while the above answers tell you how to use strncpy safely, you might consider not using it at all. If you can, just use a C++ string type instead.
strncpy isn’t unsafe as it’s bounded by the buffer size of the input and/or the string length of the input.
Last edited on
That strncpy is unsafe and deprecated is Microsoft’s own opinion, it still part of C and C++ standard.
The suggested alternative strncpy_s is only supported on Visual Studio and C11.
IMHO people who like safety shouldn’t use C or C++ at all.
Topic archived. No new replies allowed.
See more:
When I use «strncpy» I had the error:
«Error C4996 ‘strncpy‘: This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.»
and when I try ‘strncpy_s‘ I had the error:
«Error (active) E0304 no instance of overloaded function «strncpy_s» matches the argument list»
What I have tried:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <crtdbg.h> // For _CrtSetReportMode #include <errno.h> bool GetWord(char* theString, char* word, int& wordOffset); // driver program int main() { const int bufferSize = 255; char buffer[bufferSize + 1]; // hold the entire string char word[bufferSize + 1]; // hold the word int wordOffset = 0; // start at the beginning std::cout << "Enter a string : "; std::cin.getline(buffer, bufferSize); while (GetWord(buffer, word, wordOffset)) { std::cout << "Got this word: " << word << std::endl; } return 0; } // function to parse words from a string. bool GetWord(char* theString, char* word, int& wordOffset) { if (theString[wordOffset] == 0) // end of string? return false; char *p1, *p2; p1 = p2 = theString + wordOffset; // point to the next word // eat leading spaces for (int i = 0; i<(int)strlen(p1) && !isalnum(p1[0]); i++) p1++; // see if you have a word if (!isalnum(p1[0])) return false; // p1 now points to start of next word // point p2 there as well p2 = p1; // march p2 to end of word while (isalnum(p2[0])) p2++; // p2 is now at end of word // p1 is at beginning of word // length of word is the difference int len = int(p2 - p1); // copy the word into the buffer strncpy(word, p1, len); // null terminate it word[len] = ''; // now find the beginning of the next word for (int j = int(p2 - theString); j<(int)strlen(theString) && !isalnum(p2[0]); j++) { p2++; } wordOffset = int(p2 - theString); return true; }
Solution 2
The are 2 possible solutions:
1. Add _CRT_SECURE_NO_WARNINGS to your project’s settings and use strncpy();
2. Use
strncpy_s(word, p1, len);
Comments
Solution 3
strncpy[^] and strncpy_s[^] have different signatures, namely strncpy_s needs the size of the destination string as additional parameter. Hence you would have to call it this way
strncpy(word, word_size, p1, len);
(and you would have to change GetWord signature accordingly, in order to pass word_size).
Another option is ignoring or suppressing the warning.
Comments
Solution 1
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


| Top Experts | |
| Last 24hrs | This month |
CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900




ппц я тупанул)) я в class Classic : public Cd, забыл слово public)) теперь всё норм работает) спс вам)