C语言程序设计:malloc()函数的二维数组(使用指针到指针) [英] C Programming: malloc() for a 2D array (using pointer-to-pointer)
问题描述
昨天我已发布了一个问题:我应该如何传递一个指向函数的指针和从被调用的函数传递的指针分配内存?
这是我得到的答案,我能够理解我在做什么错误。
我现在面临一个新的问题,谁能帮助这吗?
我要动态分配一个二维数组,所以我传递一个指针到指针从我的的main()
来叫 alloc_2D_pixels(...)
,其中我用的malloc(...)
和的(...)
循环为二维数组分配内存。
那么,从 alloc_2D_pixels(...)
函数返回后,指针到指针仍然NULL,所以很自然,当我尝试访问或尝试免费(...)
指针到指针,程序挂起。
任何人都可以建议我什么错误,我在这里干什么?
帮助!
维克拉姆
来源:
的main()
{
unsigned char型** PTR;
unsigned int类型行,COLS; 如果(alloc_2D_pixels(安培; PTR,行,COLS)== ERROR)//满足这一条件
的printf(内存不分配的二维数组); //没有错误返回 如果(PTR == NULL)// PTR为NULL,所以没有分配内存
的printf(是其空!); //因为PTR是NULL,任何程序挂起低于这些3语句
ptr的[0] [0] = 10;
的printf(元素:%D,PTR [0] [0]); free_2D_alloc(安培; PTR);}
符号字符alloc_2D_pixels(unsigned char型***内存,无符号整型行,无符号整型COLS)
{
符号字符状态= NO_ERROR; 内存=的malloc(行*的sizeof(unsigned char型**)); 如果(内存== NULL)
{
状态=错误;
的printf(ERROR:内存分配失败!); }
其他
{
INT I; 对于(i = 0; I< COLS;我++)
{
内存[I] =的malloc(COLS * sizeof的(无符号字符)); 如果(内存[I] == NULL)
{
状态=错误;
的printf(ERROR:内存分配失败!);
}
} } //下面插入仅为调试目的的声明
内存[0] [0] =(unsigned char型)10; //我能够从访问数组
的printf(\\ nElement%D,记忆[0] [0]); //这里没有问题
返回状态;
}
无效free_2D_pixels(unsigned char型*** PTR,无符号整型行)
{
INT I; 对于(i = 0; I<行;我++)
{
免费(PTR [I]);
} 免费(PTR);
}
错误之一是张贴code,将无法编译:)。下面是纠正code。与我的结果评论
/ *这种风格* /
/ *接下来四行让你的code编译* /
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#定义NO_ERROR 0
#定义错误1/ *对函数的原型由主使用,但主后宣布
(或移动到主文件*年底/
符号字符alloc_2D_pixels(unsigned char型***内存,无符号整型行,无符号整型COLS);
无效free_2D_pixels(unsigned char型** PTR,无符号整型行);/ *主应该返回为int * /
诠释的main()
{
unsigned char型** PTR;
/ *需要定义与实际值行列数* /
unsigned int类型行= 5,COLS = 5; 如果(alloc_2D_pixels(安培; PTR,行,COLS)== ERROR)//满足这一条件
的printf(内存不分配的二维数组); //错误返回 如果(PTR == NULL)// PTR为NULL,所以没有分配内存
的printf(是其空!);
其他
{
/ *增加else子句所以下面在分配工作code只运行。 * /
/ *增加了code写的每一个元素作为测试。 * /
unsigned int类型行,列;
为(行= 0;&行LT;排;排++)
对于(COL = 0;&山坳下,COLS;西++)
PTR [0] [0] =(unsigned char型)(行+列); / *无须&放大器; PTR这里,没有返回过这么没必要通过
参照* /
free_2D_pixels(PTR,行);
} 返回0;
}符号字符alloc_2D_pixels(unsigned char型***内存,无符号整型行,无符号整型COLS)
{
符号字符状态= NO_ERROR; / *如果我们失败了返回的内存PTR将被初始化* /
*内存= NULL; / *定义一个临时PTR,否则将不得不使用(*内存)无处不在
PTR用于(呸)* /
unsigned char型** PTR; / *每行只能包含一个无符号的char *,而不是一个无符号
焦炭**,因为每一行都将是无符号的char *数组/
PTR =的malloc(行*的sizeof(无符号字符*)); 如果(PTR == NULL)
{
状态=错误;
的printf(ERROR:内存分配失败!);
}
其他
{
/ *行/ COLS是无符号的,所以这应该是太* /
无符号整型我; / *这里有一个错误。通过以上这样行迭代alloced行
不COLS这里* /
对于(i = 0; I<行;我++)
{
PTR [I] =的malloc(COLS * sizeof的(无符号字符)); 如果(PTR [I] == NULL)
{
状态=错误;
的printf(ERROR:内存分配失败!);
/ *这里仍然是一个问题,如果与错误退出,
应该释放这是任何列mallocs
成功的。 * /
}
}
} / *它的工作因此返回PTR * /
*内存= PTR;
返回状态;
}
/ *无需***在这里。不修改并返回PTR * /
/ *这也是一个错误......将需要已经(* PTR)以下随处可见* /
无效free_2D_pixels(unsigned char型** PTR,无符号整型行)
{
/ *应无符号像行* /
无符号整型我; 对于(i = 0; I<行;我++)
{
免费(PTR [I]);
} 免费(PTR);
}
yesterday I had posted a question: How should I pass a pointer to a function and allocate memory for the passed pointer from inside the called function?
From the answers I got, I was able to understand what mistake I was doing.
I'm facing a new problem now, can anyone help out with this?
I want to dynamically allocate a 2D array, so I'm passing a Pointer-to-Pointer from my main()
to another function called alloc_2D_pixels(...)
, where I use malloc(...)
and for(...)
loop to allocate memory for the 2D array.
Well, after returning from the alloc_2D_pixels(...)
function, the pointer-to-pointer still remains NULL, so naturally, when I try accessing or try to free(...)
the Pointer-to-Pointer, the program hangs.
Can anyone suggest me what mistakes I'm doing here?
Help!!!
Vikram
SOURCE:
main()
{
unsigned char **ptr;
unsigned int rows, cols;
if(alloc_2D_pixels(&ptr, rows, cols)==ERROR) // Satisfies this condition
printf("Memory for the 2D array not allocated"); // NO ERROR is returned
if(ptr == NULL) // ptr is NULL so no memory was allocated
printf("Yes its NULL!");
// Because ptr is NULL, with any of these 3 statements below the program HANGS
ptr[0][0] = 10;
printf("Element: %d",ptr[0][0]);
free_2D_alloc(&ptr);
}
signed char alloc_2D_pixels(unsigned char ***memory, unsigned int rows, unsigned int cols)
{
signed char status = NO_ERROR;
memory = malloc(rows * sizeof(unsigned char** ));
if(memory == NULL)
{
status = ERROR;
printf("ERROR: Memory allocation failed!");
}
else
{
int i;
for(i = 0; i< cols; i++)
{
memory[i] = malloc(cols * sizeof(unsigned char));
if(memory[i]==NULL)
{
status = ERROR;
printf("ERROR: Memory allocation failed!");
}
}
}
// Inserted the statements below for debug purpose only
memory[0][0] = (unsigned char)10; // I'm able to access the array from
printf("\nElement %d",memory[0][0]); // here with no problems
return status;
}
void free_2D_pixels(unsigned char ***ptr, unsigned int rows)
{
int i;
for(i = 0; i < rows; i++)
{
free(ptr[i]);
}
free(ptr);
}
One mistake is posting code that won't compile :). Below is corrected code with my comments in
/* this style */:
/* Next four lines get your code to compile */
#include <stdio.h>
#include <stdlib.h>
#define NO_ERROR 0
#define ERROR 1
/* prototypes for functions used by main but declared after main
(or move main to the end of the file */
signed char alloc_2D_pixels(unsigned char*** memory, unsigned int rows, unsigned int cols);
void free_2D_pixels(unsigned char** ptr, unsigned int rows);
/* main should return int */
int main()
{
unsigned char** ptr;
/* need to define rows and cols with an actual value */
unsigned int rows = 5, cols = 5;
if(alloc_2D_pixels(&ptr, rows, cols) == ERROR) // Satisfies this condition
printf("Memory for the 2D array not allocated"); // ERROR is returned
if(ptr == NULL) // ptr is NULL so no memory was allocated
printf("Yes its NULL!");
else
{
/* Added else clause so below code only runs if allocation worked. */
/* Added code to write to every element as a test. */
unsigned int row,col;
for(row = 0; row < rows; row++)
for(col = 0; col < cols; col++)
ptr[0][0] = (unsigned char)(row + col);
/* no need for &ptr here, not returning anything so no need to pass
by reference */
free_2D_pixels(ptr, rows);
}
return 0;
}
signed char alloc_2D_pixels(unsigned char*** memory, unsigned int rows, unsigned int cols)
{
signed char status = NO_ERROR;
/* In case we fail the returned memory ptr will be initialized */
*memory = NULL;
/* defining a temp ptr, otherwise would have to use (*memory) everywhere
ptr is used (yuck) */
unsigned char** ptr;
/* Each row should only contain an unsigned char*, not an unsigned
char**, because each row will be an array of unsigned char */
ptr = malloc(rows * sizeof(unsigned char*));
if(ptr == NULL)
{
status = ERROR;
printf("ERROR: Memory allocation failed!");
}
else
{
/* rows/cols are unsigned, so this should be too */
unsigned int i;
/* had an error here. alloced rows above so iterate through rows
not cols here */
for(i = 0; i < rows; i++)
{
ptr[i] = malloc(cols * sizeof(unsigned char));
if(ptr[i] == NULL)
{
status = ERROR;
printf("ERROR: Memory allocation failed!");
/* still a problem here, if exiting with error,
should free any column mallocs that were
successful. */
}
}
}
/* it worked so return ptr */
*memory = ptr;
return status;
}
/* no need for *** here. Not modifying and returning ptr */
/* it also was a bug...would've needed (*ptr) everywhere below */
void free_2D_pixels(unsigned char** ptr, unsigned int rows)
{
/* should be unsigned like rows */
unsigned int i;
for(i = 0; i < rows; i++)
{
free(ptr[i]);
}
free(ptr);
}
这篇关于C语言程序设计:malloc()函数的二维数组(使用指针到指针)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!