我需要帮助malloc()
内部另一个功能.
我正在向我的函数传递一个指针和大小main()
,我想malloc()
从被调用的函数内部动态地为该指针分配内存,但我看到的是......正在分配的内存是对于在我调用的函数内声明的指针,而不是指向内部的指针main()
.
我应该如何传递指向函数的指针并为被调用函数内部传递的指针分配内存?
我写了下面的代码,得到如下所示的输出.
资源:
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!"); …
Run Code Online (Sandbox Code Playgroud)