2

I know there are better options, like a std::vector or std::array etc. In my case, I have to use a pointer to a dynamically allocated array because I'm learning the copy-and-swap idiom and it involves creating my own resource management class.

Say I have a following copy constructor for a resource handle class:

A(const A& other) : size(other.size), arr(size ? new int[size]() : nullptr) {
    std::copy(other.arr, other.arr + size, arr);
}

It doesn't compile in Visual Studio (2013 Preview nor 2012 Express). The error I'm getting is: enter image description here

Is it possible to use std::copy in another way so that the compiler stops yelling at me? Or is it a better idea to manually copy the contents of the array using a simple loop like

for (int i = 0; i < size; i++) 
    arr[i] = other.arr[i];

P.S. I don't want to use any hacks/macros to disable warnings etc.

Oleksiy
  • 37,477
  • 22
  • 74
  • 122
  • It yells so since there is a possibility of running through buffer overflow if size is large. Your usage may make it clear if that at all is possible. You can disable warning for just this file. http://stackoverflow.com/questions/903064/compiler-error-function-call-with-parameters-that-may-be-unsafe – Manoj Awasthi Sep 04 '13 at 03:46
  • @JesseGood it's a simple `int*` in this example – Oleksiy Sep 04 '13 at 03:48
  • Why not use `std::vector`? – Jesse Good Sep 04 '13 at 03:48
  • @JesseGood you see, I'm trying to learn the copy_and_swap idiom, as well as the rule_of_five and all that resource management stuff. I **need** to use a pointer, otherwise of course I would've used a `vector` - makes life a lot easier! – Oleksiy Sep 04 '13 at 03:50
  • @ManojAwasthi Thanks for your comment, but I explicitly state that I don't want to disable warnings – Oleksiy Sep 04 '13 at 03:57
  • If `arr` is a POD array you could use `memcpy_s` which would get around the warning issue. – Jonathan Potter Sep 04 '13 at 03:58
  • @JonathanPotter Thanks for your comment! Can you please tell me if this is how I use it? `memcpy_s(arr, size, other.arr, size);` I never used that function before so I just want to make sure – Oleksiy Sep 04 '13 at 04:01
  • `memcpy_s(arr, size * sizeof(*arr), other.arr, size * sizeof(*arr));` but it will only work for PODs like ints and isn't really a great solution in the long term. – Jonathan Potter Sep 04 '13 at 04:05
  • 1
    If Visual Studio refuses to compile conforming C++ code, then it is not a C++ compiler. Put another way, your question is about Visual Studio, not about C++. – Nemo Sep 04 '13 at 05:09
  • 4
    @Nemo: You're jumping to conclusions. c4996 is a **warning**. If it triggers error, then OP himself enabled "treat warnings as errors" flag. – SigTerm Sep 04 '13 at 06:06

2 Answers2

2

As far as I can tell, you've enabled "treat warnings as errors" in your project (see /WX here). Disable the flag, recompile. Enabling this flag on windows+msvc is not a very good idea, because some warnings are simply nuts (try /Wall or /W4 in any program that includes windows.h).

To get rid of warnings, #define D_SCL_SECURE_NO_WARNINGS in your project properties. Or replace container with vector. you could also disable specific warning with compiler pragmas.

SigTerm
  • 26,089
  • 6
  • 66
  • 115
1

You have several options:

1) Define D_SCL_SECURE_NO_WARNINGS as the error message says. Also, do you have "make warning into errors" turned on? It seems this should be a warning, not an error. (EDIT: I see now that you don't want to use this approach).

2) Use checked_array_iterator (however, this is not standard):

std::copy(other.arr, other.arr + size, stdext::checked_array_iterator<int*>(arr, size));

3) Use std::vector which would be the best approach.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166
  • +1 for the second option. One question though, is there a replacement for `checked_array_iterator` in normal `std`? – Oleksiy Sep 04 '13 at 03:59
  • @Oleksiy: No there isn't. Even in my link it says `To avoid the need for the checked_array_iterator class when using Standard C++ Library algorithms, consider using a vector instead of a dynamically allocated array.` – Jesse Good Sep 04 '13 at 04:03