C语言程序设计:malloc()函数另一个函数内 [英] C Programming: malloc() inside another function
问题描述
我需要用帮助的malloc()
另一个函数里面的
我传递的指针和尺寸以功能从我的的main()
,我想分配内存为指针动态使用的malloc()
从调用函数内部,但我看到那是什么....内存,这是越来越分配,是指针我调用的函数内声明并没有为指针,是在的main()
。
我应该如何传递一个指向函数的指针和传递的指针分配内存的从被调用的函数
我写了下面的code和我得到如下图所示的输出。
来源:
INT的main()
{
无符号字符* input_image;
unsigned int类型bmp_image_size = 262144; 如果(alloc_pixels(input_image,bmp_image_size)== NULL)
的printf(\\ nPoint2:内存分配:%d字节,_ MSIZE(input_image));
其他
的printf(\\ nPoint3:内存未分配);
返回0;
}符号字符alloc_pixels(无符号字符* PTR,无符号整型大小)
{
符号字符状态= NO_ERROR;
PTR = NULL; PTR =(无符号字符*)malloc的(大小); 如果(PTR == NULL)
{
状态=错误;
免费(PTR);
的printf(\\ n错误:内存分配没有成功完成!);
} 的printf(\\ nPoint1:内存分配:%d字节,_ MSIZE(PTR)); 返回状态;
}
程序输出:
点1:内存分配PTR:262144字节
点2:内存分配input_image:0字节
您需要将指针传递给一个指针作为参数传递给你的函数。
INT的main()
{
无符号字符* input_image;
unsigned int类型bmp_image_size = 262144; 如果(alloc_pixels(安培; input_image,bmp_image_size)== NO_ERROR)
的printf(\\ nPoint2:内存分配:%d字节,_ MSIZE(input_image));
其他
的printf(\\ nPoint3:内存未分配);
返回0;
}符号字符alloc_pixels(unsigned char型** PTR,无符号整型大小)
{
符号字符状态= NO_ERROR;
* PTR = NULL; * PTR =(无符号字符*)malloc的(大小); 如果(* PTR == NULL)
{
状态=错误;
免费(* PTR); / *此行完全是多余的* /
的printf(\\ n错误:内存分配没有成功完成!);
} 的printf(\\ nPoint1:内存分配:%d字节,_ MSIZE(* PTR)); 返回状态;
}
I need help with malloc()
inside another function.
I'm passing a pointer and size to the function from my main()
and I would like to allocate memory for that pointer dynamically using malloc()
from inside that called function, but what I see is that.... the memory, which is getting allocated, is for the pointer declared within my called function and not for the pointer which is inside the main()
.
How should I pass a pointer to a function and allocate memory for the passed pointer from inside the called function?
I have written the following code and I get the output as shown below.
SOURCE:
int main()
{
unsigned char *input_image;
unsigned int bmp_image_size = 262144;
if(alloc_pixels(input_image, bmp_image_size)==NULL)
printf("\nPoint2: Memory allocated: %d bytes",_msize(input_image));
else
printf("\nPoint3: Memory not allocated");
return 0;
}
signed char alloc_pixels(unsigned char *ptr, unsigned int size)
{
signed char status = NO_ERROR;
ptr = NULL;
ptr = (unsigned char*)malloc(size);
if(ptr== NULL)
{
status = ERROR;
free(ptr);
printf("\nERROR: Memory allocation did not complete successfully!");
}
printf("\nPoint1: Memory allocated: %d bytes",_msize(ptr));
return status;
}
PROGRAM OUTPUT:
Point1: Memory allocated ptr: 262144 bytes
Point2: Memory allocated input_image: 0 bytes
You need to pass a pointer to a pointer as the parameter to your function.
int main()
{
unsigned char *input_image;
unsigned int bmp_image_size = 262144;
if(alloc_pixels(&input_image, bmp_image_size) == NO_ERROR)
printf("\nPoint2: Memory allocated: %d bytes",_msize(input_image));
else
printf("\nPoint3: Memory not allocated");
return 0;
}
signed char alloc_pixels(unsigned char **ptr, unsigned int size)
{
signed char status = NO_ERROR;
*ptr = NULL;
*ptr = (unsigned char*)malloc(size);
if(*ptr== NULL)
{
status = ERROR;
free(*ptr); /* this line is completely redundant */
printf("\nERROR: Memory allocation did not complete successfully!");
}
printf("\nPoint1: Memory allocated: %d bytes",_msize(*ptr));
return status;
}
这篇关于C语言程序设计:malloc()函数另一个函数内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!