本文介绍了C ++中的警告消息:delete [] a的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的代码中有以下警告信息:



$ g ++ GES1.cpp

GES1.cpp:在函数'int main(int,char **)'中:

GES1.cpp:100:14:警告:删除数组'A'

删除[] A;



没有GaussElimination(....)函数的代码是:

Hi,
I am gettingfollowing warning message in my code:

$ g++ GES1.cpp
GES1.cpp: In function ‘int main(int, char**)’:
GES1.cpp:100:14: warning: deleting array ‘A’
delete [ ] A;

My code without the GaussElimination(....) function is:

#include <iostream>
#include <cmath>
#include <fstream>
#include <ctime>
#include <vector>

#define n  3

using namespace std;


void GaussianElimination(double **,double *b ,double *y);
int main(int argc, char * argv[])
{
  /* values
  Row0 = 0, 2, 1; b0= -8
  Row1 = 1, -2, -3; b1 = 0
  Row3 = -1, 1, 2; b2= 3

  Ans = -4, -5, 2 */
  //int **A = new {{0.0, 2.0, 1.0, -8.0}, {1.0, -2.0, -3.0, 0.0},{-1.0, 1.0, 2.0,3.0}};
  
  
  double *A[n];
  for (int i=0; i < n; i++)
    A[i] = new double [n];
  double *b = new double[n];
  double *y = new double[n];
  
  A[0][0] =0.0;A[0][1] =2.0; A[0][2] = 1.0; //A[0][3] = -8.0;
  A[1][0] =1.0;A[1][1] =-2.0; A[1][2] =-3.0; //A[1][3] = 0.0;
  A[2][0] =-1.0;A[0][1] =1.0; A[0][2] = 2.0; //A[0][3] = 3.0;
  b[0] = -8.0;
  b[1] = 0.0;
  b[2] = 3.0;

  
  GaussianElimination(A, b ,y);
  delete [] y;
  delete [ ]b;
  for(int i = 0; i < n; i++)
    delete [] A[i];
  delete [ ] A;  
}



有些人请指导我。



Zulfi。



我尝试过的事情:



对不起我无法理解,因为我使用的是同样的代码在另一个程序中,没有任何警告信息。



Zulfi。


Some body please guide me.

Zulfi.

What I have tried:

Sorry I can't understand because I am using the same code in another program without any warning message.

Zulfi.

推荐答案

#include <iostream>
#include <cmath>
#include <fstream>
#include <ctime>
#include <vector>

#define n  3

using namespace std;


void GaussianElimination(double **,double *b ,double *y);
int main(int argc, char * argv[])
{
  /* values
  Row0 = 0, 2, 1; b0= -8
  Row1 = 1, -2, -3; b1 = 0
  Row3 = -1, 1, 2; b2= 3

  Ans = -4, -5, 2 */
  //int **A = new {{0.0, 2.0, 1.0, -8.0}, {1.0, -2.0, -3.0, 0.0},{-1.0, 1.0, 2.0,3.0}};
  
  
  double *A[n];
  for (int i=0; i < n; i++)
    A[i] = new double [n];
  double *b = new double[n];
  double *y = new double[n];
  
  A[0][0] =0.0;A[0][1] =2.0; A[0][2] = 1.0; //A[0][3] = -8.0;
  A[1][0] =1.0;A[1][1] =-2.0; A[1][2] =-3.0; //A[1][3] = 0.0;
  A[2][0] =-1.0;A[0][1] =1.0; A[0][2] = 2.0; //A[0][3] = 3.0;
  b[0] = -8.0;
  b[1] = 0.0;
  b[2] = 3.0;

  
  GaussianElimination(A, b ,y);
  delete [] y;
  delete [ ]b;
  for(int i = 0; i < n; i++)
    delete [] A[i];
  delete [ ] A;  
}



有些人请指导我。



Zulfi。



我尝试过的事情:



对不起我无法理解,因为我使用的是同样的代码在另一个程序中没有任何警告信息。



Zulfi。


Some body please guide me.

Zulfi.

What I have tried:

Sorry I can't understand because I am using the same code in another program without any warning message.

Zulfi.


double *A[n];

A是一个你没有动态创建的变量,它只是一个变量。那么为什么你认为你可以删除A?



另见我对你的问题的评论。

"A" is a variable you did not created dynamically, it is simply a variable. So why you think you can delete "A"?

See also my comment to your question.


double A[n][n];

无需正确描述0x01AA等其他分配或删除。

No need for additional allocations or deletes like 0x01AA correctly described.


这篇关于C ++中的警告消息:delete [] a的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 20:30