I can allocate memory for pointer to struct bmp_ptr
and I can check if the pointer is valid. But I need to allocate memory for header
or dib
member, how could I check that the malloc()
was successful? I can malloc()
, but using type cast means that the resulting pointer from malloc()
would disappear. How could I declare the nested type?
我可以为指向struct bmp_ptr的指针分配内存,我可以检查指针是否有效。但是我需要为header或dib成员分配内存,我怎么能检查malloc()是否成功?我可以使用malloc(),但是使用类型转换意味着来自malloc()的结果指针将消失。我怎么能声明嵌套类型?
header:
typedef struct BMP_DIB BITMAPINFOHEADER;
// BMP_DIB definition follows..
// BMP_FILE_struct definition follows:
typedef struct BMP_FILE_struct {
BMPHEADER header;
BITMAPINFOHEADER dib;
} BMPFILE;
main inside function:
主要内部功能:
BMPFILE * bmp_ptr;
bmp_ptr = malloc(sizeof(BMPFILE));
if (bmp_ptr == NULL) return NULL;
1 个解决方案
#1
1
[Added after the comments]
[评论后添加]
TL;DR - You don't need to allocate dynamic memory [or, check for the address is NULL
or not] for compile time allocated variables. They will always have defined memory location and cannot be NULL.
TL; DR - 对于编译时分配的变量,您不需要分配动态内存[或者,检查地址是否为NULL]。它们将始终定义内存位置,不能为NULL。
As your header
and dib
are not pointer variables, you don't need to malloc()
separately for them. malloc()
-ing for bmp_ptr
will allocate memory for them both.
由于您的标头和dib不是指针变量,因此您不需要单独使用malloc()。 malloc() - 用于bmp_ptr将为它们分配内存。
[Currently outdated]
Point 1. You need not and please do not cast the return value of malloc()
and family.
要点1.您不需要并且请不要转换malloc()和family的返回值。
Point 2. Anyways, Cast
ing does not change the return value, it is related to the type.
点2.无论如何,Casting不会改变返回值,它与类型有关。
for head or dib member, how could I check that the malloc was successful?
对于head或dib成员,我怎么能检查malloc是否成功?
you need to check against NULL
for bmp_ptr->header
.
你需要检查bmp_ptr-> header的NULL。
- if
malloc()
is successful in allocating memory you'll have a non-NULL
value. - if
malloc()
fails, it'll returnNULL
.
如果malloc()成功分配内存,那么你将拥有一个非NULL值。
如果malloc()失败,它将返回NULL。
Same goes for dib
also.
dib也是如此。
Note : Considering header
and dib
are pointer type variables. If they are not pointers, then no need to allocate memory using malloc()
.
注意:考虑头和dib是指针类型变量。如果它们不是指针,则不需要使用malloc()分配内存。
#1
1
[Added after the comments]
[评论后添加]
TL;DR - You don't need to allocate dynamic memory [or, check for the address is NULL
or not] for compile time allocated variables. They will always have defined memory location and cannot be NULL.
TL; DR - 对于编译时分配的变量,您不需要分配动态内存[或者,检查地址是否为NULL]。它们将始终定义内存位置,不能为NULL。
As your header
and dib
are not pointer variables, you don't need to malloc()
separately for them. malloc()
-ing for bmp_ptr
will allocate memory for them both.
由于您的标头和dib不是指针变量,因此您不需要单独使用malloc()。 malloc() - 用于bmp_ptr将为它们分配内存。
[Currently outdated]
Point 1. You need not and please do not cast the return value of malloc()
and family.
要点1.您不需要并且请不要转换malloc()和family的返回值。
Point 2. Anyways, Cast
ing does not change the return value, it is related to the type.
点2.无论如何,Casting不会改变返回值,它与类型有关。
for head or dib member, how could I check that the malloc was successful?
对于head或dib成员,我怎么能检查malloc是否成功?
you need to check against NULL
for bmp_ptr->header
.
你需要检查bmp_ptr-> header的NULL。
- if
malloc()
is successful in allocating memory you'll have a non-NULL
value. - if
malloc()
fails, it'll returnNULL
.
如果malloc()成功分配内存,那么你将拥有一个非NULL值。
如果malloc()失败,它将返回NULL。
Same goes for dib
also.
dib也是如此。
Note : Considering header
and dib
are pointer type variables. If they are not pointers, then no need to allocate memory using malloc()
.
注意:考虑头和dib是指针类型变量。如果它们不是指针,则不需要使用malloc()分配内存。