在VS2015上调试期​​间的C ++代码异常错误(Windows 8):访问冲突写入位置 [英] C++ code exception error during debugging on VS2015 (windows 8) : Access violation writing location

查看:80
本文介绍了在VS2015上调试期​​间的C ++代码异常错误(Windows 8):访问冲突写入位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好C / C ++程序员我有一些我目前在VS2015 windows 8上调试的代码。
$


当本地windows调试器的箭头进入我以粗体显示的代码行调试中止并抛出异常:

异常抛出0x0018A2E8访问冲突写入位置0x00000000。



我使用粗体标签突出显示隐含的代码,但如果它没有显示中止调试的异常:[code] MH(cr1.photonNum,mat1,stdXVals); [/ code]



以下是来自montecarlo.cpp的大型C ++代码示例:

Hello fellow C/C++ programmers I have some code that I am currently debugging on VS2015 windows 8.

When the arrow of the local windows debugger gets to the line of code that I have higlighted in bold the debugging aborts and throws the exception:
Exception thrown at 0x0018A2E8 Access violation writing location 0x00000000.

I used the bold tags to highlight the implicated code but if it does not show the exception that aborts the debugging is: [code]MH(cr1.photonNum, mat1, stdXVals);[/code]

Here is a sample of my large C++ code from montecarlo.cpp:

   double * mat = nullptr;
        //declaration of variables here-declaration will be pasted above
        getMedianX = p1.CalcMedian(xVal, cr1.photonNum);
        getMedianY = p1.CalcMedian(yVal, cr1.photonNum);
        getMedianZ = p1.CalcMedian(zVal, cr1.photonNum);
        getMedianXDir = p1.CalcMedian(xPlaneDir, cr1.photonNum);
        getMedianYDir = p1.CalcMedian(yPlaneDir, cr1.photonNum);
        getMedianZDir = p1.CalcMedian(zPlaneDir, cr1.photonNum);
        meanXVals = mc1.getMean1(xVal, cr1.photonNum);
        meanYVals = mc1.getMean1(yVal, cr1.photonNum);
        meanZVals = mc1.getMean1(zVal, cr1.photonNum);
        meanXDirs = mc1.getMean1(xPlaneDir, cr1.photonNum);
        meanYDirs = mc1.getMean1(yPlaneDir, cr1.photonNum);
        meanZDirs = mc1.getMean1(zPlaneDir, cr1.photonNum);
        stdXVals = p1.getStdDev(meanXVals, cr1.photonNum, xVal);
        stdYVals = p1.getStdDev(meanYVals, cr1.photonNum, yVal);
        stdZVals = p1.getStdDev(meanZVals, cr1.photonNum, zVal);
        stdxDir = p1.getStdDev(meanXDirs, cr1.photonNum, xPlaneDir);
        stdyDir = p1.getStdDev(meanYDirs, cr1.photonNum, yPlaneDir);
        stdzDir = p1.getStdDev(meanZDirs, cr1.photonNum, zPlaneDir);
        double * mat1 = nullptr;
        double * mat2 = nullptr;
        double * mat3 = nullptr;
        double * mat4 = nullptr;
        double * mat5 = nullptr;
        double * mat6 = nullptr;
        //2nd MH run if 1st MH run does not succeed
        [b]MH(cr1.photonNum, mat1, stdXVals);[/b]
        MH(cr1.photonNum, mat2, stdYVals);
        MH(cr1.photonNum, mat3, stdZVals);
        MH(cr1.photonNum, mat4, stdxDir);
        MH(cr1.photonNum, mat5, stdyDir);
        MH(cr1.photonNum, mat6, stdzDir);
        varXVals = stdXVals * stdXVals;
        varYVals = stdYVals * stdYVals;
        varZVals = stdZVals * stdZVals;
        varXDir = stdxDir * stdxDir;
        varYDir = stdyDir * stdyDir;
        varZDir = stdzDir * stdzDir;




$


函数MH来自我包含的头文件( #include" MetropHastingsH.h")

调试器停在[code] mat [2 * i] = update [0]; [/ code]所以这必须是错误来自的地方我想。



MetropHastings.h




The function MH is from a header files I included (#include "MetropHastingsH.h")
The debugger stops at [code]mat[2 * i] = update[0];[/code] so this must be where the error is coming from I think.

MetropHastings.h

#ifndef METROPHASTING_H
#define METROPHASTING_H
#include <iostream>
#include <cstdlib>
#include "chartdir.h"
#include <string>
#include <sstream>
#include <array>
#include <gsl/gsl_sf_gamma.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_linalg.h>
#include <ctime>
#include <stdio.h>

int rmvnorm(const gsl_rng *r, const int n, const gsl_vector *mean, const gsl_matrix *var, gsl_vector *result);
double target(double x[2]);

     /*
     MH() is the main function
     n_sim: number of simulations, pointer
     mat: stores the generated samples. A matrix with dimension n_sim by 2, pointer
*/
   /*In this case I have changed the datatype of n_sim from double to int
    *without the pointer so double *n_sim is change to int n_sim */
void MH(const int n_sim, double * mat, double stdDev) {
    //in our case the sample is 2 dimensional
    int n = 2;
    //define standard deviation
    //double sd = 1.5;
    double sd = stdDev;
    //initial values are 0,0
    double can[2] = { 0,0 };
    double update[2], uni, accept;
    int i, j;
    //const gsl_rng_type * T;
    gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);
        /*Define random number t-Mersenne random twister*/
    //srand(time(0)); //added
    long seed = (long)time(NULL);
        /*Define a seed for r*-previous code was time(NULL)*/
    gsl_rng_set(r, seed); /*Initiate the random number generator with seed*/

    gsl_vector *mean = gsl_vector_alloc(n);
    gsl_matrix *var = gsl_matrix_alloc(n, n);
    gsl_vector *result = gsl_vector_alloc(n);
    //set up variance matrix
    gsl_matrix_set(var, 0, 0, pow(sd, 2));
    gsl_matrix_set(var, 1, 1, pow(sd, 2));
    gsl_matrix_set(var, 0, 1, 0);
    gsl_matrix_set(var, 1, 0, 0);
    //start Metropolis Hasting algorithm
    for (i = 0; i < n_sim; i++) {
        //set up the mean vector
        gsl_vector_set(mean, 0, can[0]);
        gsl_vector_set(mean, 1, can[1]);
        //block update, use multivariate normal random sample generator
        rmvnorm(r, n, mean, var, result);
        //store the sample
        for (j = 0; j < n; j++) {
            update[j] = gsl_vector_get(result, j);
        }
        /*now calculate the acceptance rate and see whether the sample is accepted or not
        */
        uni = gsl_rng_uniform(r);
        accept = target(update) / target(can);

        if (uni < accept) {
            //if acceptance rate is large enough, accept the update
            can[0] = update[0];
            can[1] = update[1];
            [b]mat[2 * i] = update[0];[/b]
            mat[2 * i + 1] = update[1];
            //printf("\%f\t\%f\n",can[0],can[1]);

        }
        else {
            //otherwise do not update
            mat[2 * i] = can[0];
            mat[2 * i + 1] = can[1];
            //printf("\%f\t\%f\n",can[0],can[1]);
        }

    }
    gsl_vector_free(result);
    gsl_vector_free(mean);
    }

    //random multivariate normal sample generator
    int rmvnorm(const gsl_rng *r, const int n, const gsl_vector *mean, const gsl_matrix *var, gsl_vector *result) {
    /* multivariate normal distribution random number generator */
    /*
    *    n    dimension of the random vector
    *    mean    vector of means of size n
    *    (var) variance    variance matrix of dimension n x n
    *result    output variable with a single random vector normal distribution generation
    */
    int k;
    gsl_matrix *work = gsl_matrix_alloc(n, n);

    gsl_matrix_memcpy(work, var);
    gsl_linalg_cholesky_decomp(work);

    for (k = 0; k<n; k++)
        gsl_vector_set(result, k, gsl_ran_ugaussian(r));

    gsl_blas_dtrmv(CblasLower, CblasNoTrans, CblasNonUnit, work, result);
    gsl_vector_add(result, mean);
        gsl_matrix_free(work);
        return 0;
     }

    //the posterior PDF
    double target(double x[2]) {
    return exp(-pow(x[0], 2) - pow(x[1] * x[0], 2) - pow(x[1], 2));
      }
     #endif /*METROPHASTING_H*/






$
有人可以在租赁时建议这个错误的原因,谢谢你的帮助。




Could someone please at leaset suggest the cause of this error, thanks as always for the help.

推荐答案

好吧,直接调用MH之前的行,您将mat变量设置为null。然后将其传递给MH函数,然后在函数中尝试写入它。

Well, the lines directly before the call to MH, you set the mat variables to null. You then pass it into the MH function and later in the function try to write to it.

为了使这更简单,将mat1设置为指向无效的位置,保证始终导致错误,然后尝试使用它。

To put this even simpler, you set mat1 to point to an invalid location which is guaranteed to always cause an error, and then try to use it.

要使用这样的指针,必须为它分配内存,因为指针本身不分配内存,除非存储指针本身。您需要使用malloc或new分配足够的内存,或使用
运算符的地址使用已存在的内存块。

To use a pointer like this you must allocate memory for it to use since the pointer itself doesn't allocate memory except to store the pointer itself. You need to allocate enough memory using malloc or new, or use an already existing block of memory using the address of operator.


这篇关于在VS2015上调试期​​间的C ++代码异常错误(Windows 8):访问冲突写入位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆