使用参数可能不安全的函数调用

使用参数可能不安全的函数调用

本文介绍了Visual Studio 2015错误C4996'std :: _ Copy_impl':使用参数可能不安全的函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个模板类:

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

我编译时收到以下消息:

When I compile I get this message:

阅读此答案我理解是因为我得到了错误(我没有根据msdn函数签名调用std :: copy),但是我不知道如何解决它.我认为错误行是:

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

感谢您的帮助.

推荐答案

我已按照在本文中由@DanMašek在评论中建议.

I solved the problem following instructions in this article suggested by @DanMašek in the comment.

我添加了标题<iterator>,然后使用了类stdext::checked_array_iterator.结果是这样的:

I added the header <iterator> and then I used the class 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));

这篇关于Visual Studio 2015错误C4996'std :: _ Copy_impl':使用参数可能不安全的函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 06:24