Memory allocation and problem

Hello,
I started writing my code following the below link:
http://www.phon.ox.ac.uk/~jcoleman/SLP/Extras/dtw.c
but I need to follow my file format and memory allocation in c++, also some other modifications.

After few attempts of running this and my few test codes that I deleted forseeing more upcoming disaster to my macbook due to lack of good knowledge in good memory allocation and deallocation practical knowledge, my system behaves still strange after my program and does not sleep anymore. I have several times Bus error and segmentsegv errors. Could you let me know if my following memory allocation and deallocation is on the right tract.

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

/**
class FixedArray
	{
	public:
		template<class T>
		static T **New(int sizeX, int sizeY)
		{
			int sizeAddress = sizeof(T*)*sizeX;
			int sizeData = sizeof(T)*sizeX*sizeY;
			
			char *data = new char[sizeAddress+sizeData];
			
			for(int i=0;i<sizeX;i++){
				((T**)data)[i] = &((T*)(data+sizeAddress))[i*sizeY];
			}
			
			return (T **)data;
		}
		
		static void Delete(void *p)
		{
			delete [] p;
		}
	};
**/
int main (int argc, char * const argv[]) {
    
	//unsigned int I, X, Y, n, i, j, k;
	unsigned int xsize = 10;
	unsigned int ysize = 10;
	unsigned int params = 3;
	
	int i;
	//float **x, **y; /*now 2 dimensional*/
	
	//allocate memory for x and y parameters
	float **x = new float*[xsize];
	for(i = 0; i < xsize; i++){
		x[i] = new float[params];	
	}
	
	float **y = new float*[ysize];
	for(int i = 0; i < ysize; i++){
		y[i] = new float[params];	
	}
	
	//memory allocation for local distance and global distance
	//double **globdist;
	//double **Dist;
	
	double **Dist = new double*[xsize];
	for(int i = 0; i < xsize; i++){
		Dist[i] = new double[ysize];	
	}
	
	//double **globdist;
	double **globdist = new double*[xsize];
	for(int i = 0; i < xsize; i++){
		globdist[i] = new double[ysize];	
	}
	
	
	
	double top, mid, bot, cheapest, total;
	unsigned short int **move = new unsigned short int*[xsize];
	for(int i = 0; i < xsize; i++){
		move[i] = new unsigned short int[ysize];	
	} 
	
	unsigned short int **temp = new unsigned short int* [xsize*2];
	for(int i = 0; i < (xsize*2); i++){
		temp[i] = new unsigned short int[2];
	}
	
	unsigned short int **warp = new unsigned short int*[xsize*2];
	
	for(int i = 0; i < (xsize*2); i++){
		warp[i] = new unsigned short int[2];
	}
	
	
	//deallocate memory
	//deallocate memory for x
	if (x){
	for(int i =  0; i < xsize; i++){
		delete [] x[i];	
	}
	delete [] x;
	x = 0;
	}
	//deallocate memory
	//deallocate memory for y
	if(y){
	for(int i =  0; i < ysize; i++){
		delete [] y[i];	
	}
	delete [] y;
	//y = 0;
	}
	
	if(Dist){
		for(int i =  0; i < xsize; i++){
			delete [] Dist[i];	
		}
		delete [] Dist;
		//Dist = 0;
	}
	
	
	if(globdist){
		for(int i =  0; i < xsize; i++){
			delete [] globdist[i];	
		}
		delete [] globdist;
		//globdist = 0;
	}
	
	if(move){
		for(int i = 0; i < xsize; i++){
		delete [] move[i];	
		}
		delete [] move;
		//move = 0;
	}
	
	if(temp){
		
		for(int i = 0; i < (xsize * 2); i++){
			delete [] temp[i];	
		}
		delete [] temp;
	//	temp = 0;
	}
	
	if(warp){
		
		for(int i = 0; i < (xsize * 2); i++){
			delete [] warp[i];	
		}
		delete [] warp;
	//	warp = 0;
	}
		
    return 0;
}


Thank you for your time and help as well as any suggestions.
The program makes some allocations and releases them. It cannot affect an OS X host's configuration.
Thank you for your response, KBW.

Actually, I would like to know whether the allocation and deallocation is being managed correctly before I start teh next processing using these allocations.

Thank you for your response.
Yes, it seems fine to me. This is the sort of thing that valgrind works well on. You can verify it with that.
Topic archived. No new replies allowed.