Memory leak/dynamic allocation of memory.

Do i have to "delete" the pointer I declared in the function **ptr_fj?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

int **ptr_fj(int r,int c);
void initialization(int **p,int r,int c);
void deallocation(int **&p,int r,int c);

int main()  // {  }  \  ||  ~
{
  
  int **matrix=NULL;
  int r,c;cin>>r>>c;
  matrix=ptr_fj(r,c);
  initialization(matrix,r,c);
  deallocation(matrix,r,c);
  
  
 

return 0;  
}
int **ptr_fj(int r,int c)
{
  int **ptr=new int*[r];
  
  for(int i=0;i<r;i++)
  {
    ptr[i]=new int[c];
  }
    return ptr;
}

void initialization(int **p,int r,int c)
{
  
  for(int i=0;i<r;i++)
  {
    for(int j=0;j<c;j++)
	{
	  cin>>p[i][j];  
	}  
  }
  
  for(int i=0;i<r;i++)
  {
    for(int j=0;j<c;j++)
	{
	  cout<<"+----+";  
	}
	cout<<"+"<<endl;
	for(int j=0;j<c;j++)
	{
	  cout<<"|"<<setw(4)<<p[i][j];  
	}
	cout<<"|"<<endl;  
  }
  
  for(int i=0;i<c;i++)
  {
    cout<<"+----";  
  }
    cout<<"+"<<endl;
}

void deallocation(int **&p,int r,int c)
{
  for(int i=0;i<r;i++)
  {
    delete[]p[i];  
  }
    delete[]p;p=NULL;
}
Last edited on
through the matrix variable which now has ownership of the objects created within ptr_fj
edit: this is already done by deallocation()
Last edited on
great thx a lot :D
Topic archived. No new replies allowed.