为什么在我应该分配足够的空间之后数组似乎泄漏了?

问题描述

我正在尝试使用mkl计算方程式。但是似乎数组a []一直在这样泄漏:

  44.62  -0.09 -6277438562204192487878988888393020692503707483087375482269988814848.00 -6277438562204192487878988888393020692503707483087375482269988814848.00 -6277438562204192487878988888393020692503707483087375482269988814848.00
  -0.09  11.29  -0.09 -6277438562204192487878988888393020692503707483087375482269988814848.00 -6277438562204192487878988888393020692503707483087375482269988814848.00
 -6277438562204192487878988888393020692503707483087375482269988814848.00  -0.09   0.18  -0.09 -6277438562204192487878988888393020692503707483087375482269988814848.00
 -6277438562204192487878988888393020692503707483087375482269988814848.00 -6277438562204192487878988888393020692503707483087375482269988814848.00  -0.09  11.29  -0.09
 -6277438562204192487878988888393020692503707483087375482269988814848.00 -6277438562204192487878988888393020692503707483087375482269988814848.00 -6277438562204192487878988888393020692503707483087375482269988814848.00  -0.09  44.62

我的代码是:


#include <stdlib.h>
#include <stdio.h>
#include "mkl_lapacke.h"

/* Auxiliary routines prototypes */
extern void my_print_matrix(char* desc,MKL_INT m,MKL_INT n,double* a,MKL_INT lda,FILE* fpWrite);
extern void print_matrix(char* desc,MKL_INT lda);

/* Parameters */
#define N 5//nstep
#define LDA N
#define RMIN -10.0
#define RMAX 10.0
/* Main program */
int main() {
    /* Locals */
    MKL_INT n = N,lda = LDA,info;

    /* Local arrays */
    double h = (RMAX - RMIN) / (double(N) + 1.0);;
    double xi;
    double *w;
    double *a;

    w= (double*)malloc(sizeof(double) * N);
    a = (double*)malloc(sizeof(double) * N*LDA);

    for (int i = 0; i < N; i++) {
        xi = RMIN + double(1.0+i) * h;
        a[i*(N+1)] = 2.0 / h / h+xi * xi;
        if (i==0) {
            a[1] = -1.0 / h / h;
        }
        else if (i == N - 1) {
            a[LDA * N-2] =- 1.0 / h / h;
        }
        else {
            a[i *(N + 1)+1] = -1.0/h/h;
            a[i * (N + 1) - 1] = -1.0/h/h;
        }
    }
    print_matrix("Matrix",n,a,lda);
    /* Executable statements */
    printf("LAPACKE_dsyev (row-major,high-level) Example Program Results\n");
    /* Solve eigenproblem */
    info = LAPACKE_dsyev(LAPACK_ROW_MAJOR,'V','U',lda,w);
    /* Check for convergence */
    if (info > 0) {
        printf("The algorithm failed to compute eigenvalues.\n");
        exit(1);
    }
    exit(0);
} /* End of LAPACKE_dsyev Example */

/* Auxiliary routine: printing a matrix */
void print_matrix(char* desc,MKL_INT lda) {
    MKL_INT i,j;
    printf("\n %s\n",desc);
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) printf(" %6.2f",a[i * lda + j]);
        printf("\n");
    }
}

前两个元素看似正确,但数字错误...而最后一个数字是相同但非常大的数字。我认为这是数组泄漏,但我不知道如何处理。所以我寻求帮助。

a []显示只是初始化,而不是结果。我的问题是哪里初始化错误?

解决方法

让我们仔细看看如何初始化a

a[i*(N+1)] = 2.0 / h / h+xi * xi;
if (i==0) {
    a[1] = -1.0 / h / h;
}
else if (i == N - 1) {
    a[LDA * N-2] =- 1.0 / h / h;
}
else {
    a[i *(N + 1)+1] = -1.0/h/h;
    a[i * (N + 1) - 1] = -1.0/h/h;
}

让我们来考虑i == 0i == 1的两种情况:

  1. i == 0

    在这里,您首先进行无条件初始化

    a[i*(N+1)] = 2.0 / h / h+xi * xi;
    

    如果我们计算索引i*(N+1),则0*(N+1)就是0。因此,您将初始化a[0]

    然后您有if (i==0),在其中初始化a[1]

  2. i == 1

    首先对索引i*(N+1)进行无条件初始化,然后对索引1*(50+1)等于51。因此,您在这里初始化a[51]

    然后,条件i==1i == N - 1都是假的,因此我们以最后一个else子句结束:

    a[i *(N + 1)+1] = -1.0/h/h;
    a[i * (N + 1) - 1] = -1.0/h/h;
    

    第一个索引i *(N + 1)+1将是1 *(50 + 1)+1,即52。因此,您初始化了a[52]

    下一个索引i * (N + 1) - 1将是1 * (50 + 1) - 1,即50。因此,您初始化了a[50]

此模式在整个循环中重复,索引更高,但永远不会更低。

这意味着您将永远不会将索引2初始化为49。这些元素将具有不确定的值,如果您不走运,这些值之一可能是陷阱值,使用它们时会导致未定义的行为

您需要重新设计算法以初始化数组a所有元素。

,

关于内存泄漏-请不要忘记通过调用free(a)和free(w)函数为* w和*数组释放所有已分配的内存。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...