69,371
社区成员
发帖
与我相关
我的任务
分享
/* 主程序*/
// matrixleaktest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
// #include "vld.h"
int _tmain(int argc, _TCHAR* argv[])
{
int nrows,ncols;
nrows = 5 ;
ncols = 3 ;
float **row, **col,**theta,**y;
unsigned char **edgemap;
/* 源程序提供的内存机制,-1等参数设置存在问题 */
row = (float **)matrix(nrows, ncols, 0, 0, sizeof(float)); // 内存能够申请且后面可以释放
col = (float **)matrix(nrows, ncols, 0, 0, sizeof(float)); // 内存能够申请且后面可以释放
edgemap = (unsigned char **) matrix(nrows,ncols,0,0,sizeof(char)); // 内存能够申请且后面可以释放
theta = (float **)matrix(nrows+2, ncols+2, -1, -1, sizeof(float)); // 内存能够申请但后面不能释放
y = (float **)matrix(nrows+2, ncols+2, -1, -1, sizeof(float)); // 内存能够申请但后面不能释放
if ( row != NULL ) free( row ); // 释放成功
if ( col != NULL ) free( col ); // 释放成功
if ( edgemap != NULL ) free( edgemap ); // 释放成功
if ( theta != NULL ) free( theta ); // 释放失败
if ( y != NULL ) free( y ); // 释放失败
return 0;
}
/* 子函数 */
void **matrix(int nrows, int ncols, int first_row_coord,int first_col_coord, int element_size)
{
void **p;
int alignment;
long i;
if(nrows < 1 || ncols < 1) return(NULL);
i = nrows*sizeof(void *);
/* align the addr of the data to be a multiple of sizeof(long double) */
alignment = i % sizeof(long double);
if(alignment != 0) alignment = sizeof(long double) - alignment;
i += nrows*ncols*element_size+alignment;
if((p = (void **)malloc((size_t)i)) != NULL)
{
/* compute the address of matrix[first_row_coord][0] */
p[0] = (char *)(p+nrows)+alignment-first_col_coord*element_size;
for(i = 1; i < nrows; i++)
/* compute the address of matrix[first_row_coord+i][0] */
p[i] = (char *)(p[i-1])+ncols*element_size;
/* compute the address of matrix[0][0] */
p -= first_row_coord;
}
return(p);
}
p -= first_row_coord;
在子函数内部,P指针移动了
到了主函数内,需要反向移位相同长度才能free掉
/* 主程序*/
// matrixleaktest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
// #include "vld.h"
void myfree(void *p) {
printf("free %p\n",p);//★★★★★★★★★★★★
free(p);
}
int _tmain(int argc, _TCHAR* argv[])
{
int nrows,ncols;
nrows = 5 ;
ncols = 3 ;
float **row, **col,**theta,**y;
unsigned char **edgemap;
/* 源程序提供的内存机制,-1等参数设置存在问题 */
row = (float **)matrix(nrows, ncols, 0, 0, sizeof(float)); // 内存能够申请且后面可以释放
col = (float **)matrix(nrows, ncols, 0, 0, sizeof(float)); // 内存能够申请且后面可以释放
edgemap = (unsigned char **) matrix(nrows,ncols,0,0,sizeof(char)); // 内存能够申请且后面可以释放
theta = (float **)matrix(nrows+2, ncols+2, -1, -1, sizeof(float)); // 内存能够申请但后面不能释放
y = (float **)matrix(nrows+2, ncols+2, -1, -1, sizeof(float)); // 内存能够申请但后面不能释放
if ( row != NULL ) myfree( row ); // 释放成功
if ( col != NULL ) myfree( col ); // 释放成功
if ( edgemap != NULL ) myfree( edgemap ); // 释放成功
if ( theta != NULL ) myfree( theta ); // 释放失败
if ( y != NULL ) myfree( y ); // 释放失败
return 0;
}
/* 子函数 */
void **matrix(int nrows, int ncols, int first_row_coord,int first_col_coord, int element_size)
{
void **p;
int alignment;
long i;
if(nrows < 1 || ncols < 1) return(NULL);
i = nrows*sizeof(void *);
/* align the addr of the data to be a multiple of sizeof(long double) */
alignment = i % sizeof(long double);
if(alignment != 0) alignment = sizeof(long double) - alignment;
i += nrows*ncols*element_size+alignment;
if((p = (void **)malloc((size_t)i)) != NULL)
{
printf("malloc return %p\n",p);//★★★★★★★★★★★★
/* compute the address of matrix[first_row_coord][0] */
p[0] = (char *)(p+nrows)+alignment-first_col_coord*element_size;
for(i = 1; i < nrows; i++)
/* compute the address of matrix[first_row_coord+i][0] */
p[i] = (char *)(p[i-1])+ncols*element_size;
/* compute the address of matrix[0][0] */
p -= first_row_coord;
}
return(p);
}
i = nrows*sizeof(void *);
/* align the addr of the data to be a multiple of sizeof(long double) */
alignment = i % sizeof(long double);
if(alignment != 0) alignment = sizeof(long double) - alignment;
i += nrows*ncols*element_size+alignment; // 不仅仅只是得到一大小为nrows*ncols的二维数组
if((p = (void **)malloc((size_t)i)) != NULL)
p -= first_row_coord; //你这步啥意思?
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int **newarr2d(int rows,int cols) {
int **p,i;
p=(int **)malloc(rows*sizeof(int *));
if (NULL==p) exit(1);
for (i=0;i<rows;i++) {
p[i]=(int *)malloc(cols*sizeof(int));
if (NULL==p[i]) exit(1);
}
return p;
}
void deletearr2d(int **p,int rows) {
int i;
for (i=0;i<rows;i++) {
free(p[i]);
}
free(p);
}
int main() {
int **arr2d,i,j,r,c;
r=4;
c=5;
//在堆中开辟一个4×5的二维int数组
arr2d=newarr2d(r,c);
for (i=0;i<r;i++) {
for (j=0;j<c;j++) {
arr2d[i][j]=i*c+j;
}
}
for (i=0;i<r;i++) {
for (j=0;j<c;j++) {
printf(" %2d",arr2d[i][j]);
}
printf("\n");
}
deletearr2d(arr2d,r);
r=6;
c=3;
//在堆中开辟一个6×3的二维int数组
arr2d=newarr2d(r,c);
for (i=0;i<r;i++) {
for (j=0;j<c;j++) {
arr2d[i][j]=i*c+j;
}
}
for (i=0;i<r;i++) {
for (j=0;j<c;j++) {
printf(" %2d",arr2d[i][j]);
}
printf("\n");
}
deletearr2d(arr2d,r);
return 0;
}
// 0 1 2 3 4
// 5 6 7 8 9
// 10 11 12 13 14
// 15 16 17 18 19
// 0 1 2
// 3 4 5
// 6 7 8
// 9 10 11
// 12 13 14
// 15 16 17
//