简体   繁体   English

Visual Studio 2015错误C4996'std :: _ Copy_impl':使用参数可能不安全的函数调用

[英]Visual Studio 2015 error C4996 'std::_Copy_impl': Function call with parameters that may be unsafe

I wrote this template class: 我写了这个模板类:

.hpp: .HPP:

#ifndef BIVARIATEGAUSSIANPDF_LIB_H
#define BIVARIATEGAUSSIANPDF_LIB_H

#if defined BIVARIATEGAUSSIANPDF_LIB_EXPORTS
#define BIVARIATEGAUSSIANPDFDLL_API __declspec(dllexport)
#else
#define BIVARIATEGAUSSIANPDFDLL_API __declspec(dllimport)
#endif

#include <vector>
#include <opencv2\core\core.hpp>

namespace cv
{
    class Mat;
}

#define _USE_MATH_DEFINES
#include <math.h>

template<class T, class N>
class BivariateGaussianPDF
{
public:
    BIVARIATEGAUSSIANPDFDLL_API BivariateGaussianPDF(const std::vector<T> & i_v1,
                                                     const std::vector<N> & i_v2);

    BIVARIATEGAUSSIANPDFDLL_API ~BivariateGaussianPDF();

    BIVARIATEGAUSSIANPDFDLL_API void compute();

    BIVARIATEGAUSSIANPDFDLL_API inline std::vector<double> getPDF() const { return m_pdf; }

private:
    cv::Mat m_matrix;
    std::vector<double> m_pdf;
};

template class BivariateGaussianPDF<int, int>;
template class BivariateGaussianPDF<int, float>;
template class BivariateGaussianPDF<int, double>;
template class BivariateGaussianPDF<float, int>;
template class BivariateGaussianPDF<float, float>;
template class BivariateGaussianPDF<float, double>;
template class BivariateGaussianPDF<double, int>;
template class BivariateGaussianPDF<double, float>;
template class BivariateGaussianPDF<double, double>;

#endif

and .cpp: 和.cpp:

#include "stdafx.h"
#include "bivariateGaussianPDF_lib.h"
#include <iterator>

template<class T, class N>
BivariateGaussianPDF<T, N>::BivariateGaussianPDF(const std::vector<T>& i_v1,
                                                 const std::vector<N>& i_v2)
    :m_pdf(i_v1.size())
{
    std::vector<double> temp_v1(std::begin(i_v1), std::end(i_v1));
    std::vector<double> temp_v2(std::begin(i_v2), std::end(i_v2));
    m_matrix.create(2, i_v1.size(), CV_64F);
    double * r0_ptr = m_matrix.ptr<double>(0);
    double * r1_ptr = m_matrix.ptr<double>(1);
    std::copy(temp_v1.begin(), temp_v1.end(), r0_ptr);
    std::copy(temp_v2.begin(), temp_v2.end(), r1_ptr);
}

template<class T, class N>
BivariateGaussianPDF<T, N>::~BivariateGaussianPDF()
{
    m_matrix.release();
}

template<class T, class N>
void BivariateGaussianPDF<T, N>::compute()
{
    cv::Mat row_mean, cov, inverseCov, temp_pdf;

    // compute covariance matrix
    cv::calcCovarMatrix(m_matrix, cov, row_mean, CV_COVAR_ROWS | CV_COVAR_NORMAL);
    // scale covariance matrix
    cov = cov / (m_matrix.cols - 1);
    // inverse of covariance matrix
    inverseCov = cov.inv();

    // determinant of covariance matrix
    const double det = cv::determinant(cov);

    // X = [x1; x2 ... ; xN] --> [x1 - mu1; x2 - mu2; ... ; xN - muN ]
    cv::subtract(m_matrix.row(0), row_mean.row(0), m_matrix.row(0));
    cv::subtract(m_matrix.row(1), row_mean.row(1), m_matrix.row(1));

    // (inv*X).*X
    cv::reduce((inverseCov*m_matrix).mul(m_matrix), temp_pdf, 0, CV_REDUCE_SUM);
    temp_pdf = -0.5*temp_pdf;
    cv::exp(temp_pdf, temp_pdf);

    // bivariate gaussian pdf
    temp_pdf = (0.5 / (M_PI*std::sqrt(det)))*temp_pdf;

    const double * d_ptr = temp_pdf.ptr<double>(0);
    std::copy(d_ptr, d_ptr + temp_pdf.cols, m_pdf.begin());
}

When I compile I get this message: 编译时收到以下消息:

Error C4996 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. 错误C4996'std :: _ Copy_impl':使用参数可能不安全的函数调用-此调用依赖于调用者检查传递的值是否正确。 To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. 要禁用此警告,请使用-D_SCL_SECURE_NO_WARNINGS。 See documentation on how to use Visual C++ 'Checked Iterators' bivariateGaussianPDF_lib c:\\program files (x86)\\microsoft visual studio 14.0\\vc\\include\\xutility 2229 请参阅有关如何使用Visual C ++“检查的迭代器” bivariateGaussianPDF_lib的文档c:\\ program files(x86)\\ microsoft visual studio 14.0 \\ vc \\ include \\ xutility 2229

Reading this answer I understood because I get the error (I'm not calling std::copy according to msdn function signature) but I don't know how to fix it. 阅读这个答案,我理解是因为我得到了错误(我没有根据msdn函数签名调用std :: copy),但是我不知道如何解决它。 I think the error lines are: 我认为错误行是:

std::copy(temp_v1.begin(), temp_v1.end(), r0_ptr);
std::copy(temp_v2.begin(), temp_v2.end(), r1_ptr);

Any help is appreciated. 任何帮助表示赞赏。

I solved the problem following instructions in this article suggested by @DanMašek in the comment. 我按照注释中@DanMašek建议的本文中的说明解决了问题。

I added the header <iterator> and then I used the class stdext::checked_array_iterator . 我添加了标题<iterator> ,然后使用了stdext::checked_array_iterator类。 The result is something like that: 结果是这样的:

    double * r0_ptr = m_matrix.ptr<double>(0);
    std::copy(temp_v1.begin(), temp_v1.end(), stdext::checked_array_iterator<double *>(r0_ptr, m_matrix.cols));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误C4996:&#39;std :: _ Copy_impl&#39;:带有可能不安全的参数的函数调用 - error C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe xutility(2227):警告C4996:&#39;std :: _ Copy_impl&#39; - xutility(2227): warning C4996: 'std::_Copy_impl' 错误C4996:&#39;ctime&#39;:此函数或变量可能不安全 - error C4996: 'ctime': This function or variable may be unsafe Visual Studio 2015不会禁止错误C4996 - Visual Studio 2015 won't suppress error C4996 为什么 Visual Studio 2013 在 C4996 上出错? - Why does Visual Studio 2013 error on C4996? 警告C4996:与POSIX上的GCC相比,此功能或变量可能不安全 - Warning C4996: This function or variable may be unsafe — compared to GCC on POSIX sprintf错误“变量可能不安全”(C4996)…替代品? - c++ - sprintf error “variable may be unsafe” (C4996)… alternatives? Visual Studio警告C4996 - Visual Studio Warning C4996 C++17 什么新:错误 C4996:'getenv':此 function 或变量可能不安全。 考虑改用 _dupenv_s - C++17 what new with : error C4996: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s instead 带有_wfopen函数的C ++ C4996错误 - C++ C4996 error with _wfopen function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM