【问题标题】:Visual Studio 2015 error C4996 'std::_Copy_impl': Function call with parameters that may be unsafeVisual Studio 2015 错误 C4996 'std::_Copy_impl':带有可能不安全的参数的函数调用
【发布时间】:2016-07-24 09:05:03
【问题描述】:

我写了这个模板类:

.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

和.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());
}

当我编译时,我收到这条消息:

错误 C4996 'std::_Copy_impl': 使用可能的参数调用函数 不安全 - 此调用依赖于调用者检查传递的 值是正确的。要禁用此警告,请使用 -D_SCL_SECURE_NO_WARNINGS。请参阅有关如何使用 Visual C++ 'Checked Iterators' bivariateGaussianPDF_lib c:\program files 的文档 (x86)\microsoft visual studio 14.0\vc\include\xutility 2229

阅读this answer 我明白了,因为我得到了错误(我没有根据 msdn 函数签名调用 std::copy),但我不知道如何修复它。我认为错误行是:

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

感谢任何帮助。

【问题讨论】:

  • 如果你打算使用来自&lt;algorithm&gt; 的东西,那么你应该包括那个标题。试试看会发生什么。
  • this KB article。问题是您将普通指针作为输出迭代器传递。请注意,解决方案(和问题)都是特定于 MSVS 的。
  • 谢谢@DanMašek。根据您发布的文章,我已经解决了问题。

标签: c++ opencv c++11 visual-studio-2015


【解决方案1】:

我按照@DanMašek 在评论中建议的说明in this article 解决了问题。

我添加了标题&lt;iterator&gt;,然后我使用了类stdext::checked_array_iterator。结果是这样的:

    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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多