C中If语句中的Malloc

C中If语句中的Malloc,c,if-statement,malloc,C,If Statement,Malloc,我是c语言的新手,我不能理解下面的代码。如果有人能解释的话,我真的很高兴。提前谢谢 if( ! ( st_cur = (struct ST_info *) malloc(sizeof( struct ST_info ) ) ) ) { perror( "malloc failed" ); return( 1 ); } 在中,赋值将始终返回赋值 在您的特定情况下,st_cur被赋值,赋值后会检查它是否返回了有效值 相当于这个 st_cur =

我是c语言的新手,我不能理解下面的代码。如果有人能解释的话,我真的很高兴。提前谢谢

 if( ! ( st_cur = (struct ST_info *) malloc(sizeof( struct ST_info ) ) ) )
    {
        perror( "malloc failed" );
        return( 1 );
    }
在中,赋值将始终返回赋值

在您的特定情况下,
st_cur
被赋值,赋值后会检查它是否返回了有效值

相当于这个

 st_cur = (struct ST_info*)malloc(sizeof(struct ST_info));
 if (!st_cur) {
     perror("malloc failed");
     return 1;
 }
如果您想知道,为什么有这么多括号,它们是必需的,因为
=
在C中具有所有运算符中最低的优先级。

在中,赋值总是返回赋值

在您的特定情况下,
st_cur
被赋值,赋值后会检查它是否返回了有效值

相当于这个

 st_cur = (struct ST_info*)malloc(sizeof(struct ST_info));
 if (!st_cur) {
     perror("malloc failed");
     return 1;
 }
如果您想知道,为什么有这么多括号,它们是必需的,因为
=
在C中的所有运算符中优先级最低。

这是一个赋值:

st_cur = (struct ST_info *) malloc(sizeof(struct ST_info))
它分配内存并返回指向该内存块的指针。如果分配失败,malloc返回NULL,这相当于0(在NULL和0上)。(顺便说一句,malloc的演员阵容毫无意义,请参见此)

然后对存储在st_cur中的赋值结果进行测试,这样我们就可以像下面一样使用它:

if (! st_cur )
这相当于:

if (st_cur == NULL)
struct ST_info * st_cur = NULL;
//...

/* Assign heap memory of native byte size equal to that 
 * of struct ST_info and save a pointer to the allocated 
 * memory. */
st_cur = malloc( sizeof( struct ST_info ) ); 

/* If the malloc succeeded it will have returned a valid 
 * pointer to some part of the heap, otherwise it returns 
 * NULL so we check for NULL here. */
if (st_cur == NULL) { 
    /* Print a relevant error message to stderr. */
    perror("malloc failed"); 
    /* Return a non-zero value to indicate that something failed. */
    return(1); 
}
因此,如果Allication失败,将调用perror函数,代码返回1

我会重写它:

st_cur = malloc(sizeof(struct ST_info));
if (st_cur == NULL) {
    perror( "malloc failed" );
    return(1);
}
这是一项任务:

st_cur = (struct ST_info *) malloc(sizeof(struct ST_info))
它分配内存并返回指向该内存块的指针。如果分配失败,malloc返回NULL,这相当于0(在NULL和0上)。(顺便说一句,malloc的演员阵容毫无意义,请参见此)

然后对存储在st_cur中的赋值结果进行测试,这样我们就可以像下面一样使用它:

if (! st_cur )
这相当于:

if (st_cur == NULL)
struct ST_info * st_cur = NULL;
//...

/* Assign heap memory of native byte size equal to that 
 * of struct ST_info and save a pointer to the allocated 
 * memory. */
st_cur = malloc( sizeof( struct ST_info ) ); 

/* If the malloc succeeded it will have returned a valid 
 * pointer to some part of the heap, otherwise it returns 
 * NULL so we check for NULL here. */
if (st_cur == NULL) { 
    /* Print a relevant error message to stderr. */
    perror("malloc failed"); 
    /* Return a non-zero value to indicate that something failed. */
    return(1); 
}
因此,如果Allication失败,将调用perror函数,代码返回1

我会重写它:

st_cur = malloc(sizeof(struct ST_info));
if (st_cur == NULL) {
    perror( "malloc failed" );
    return(1);
}

if
条件中,可以有任何具有值的语句,可以标识为0或非-0、false或true

st_cur=(struct st_info*)malloc(sizeof(struct st_info))

在该语句中调用
malloc
,并将其返回值分配给
st\u cur
。all语句的值是
st\u cur
的值。
!当st_cur为空时,st_cur
为真 所以,等价代码是

st_cur = (struct ST_info *) malloc(sizeof( struct ST_info ) ) 
if( ! st_cur )
{
    perror( "malloc failed" );
    return( 1 );
}

if
条件中,可以有任何具有值的语句,可以标识为0或非-0、false或true

st_cur=(struct st_info*)malloc(sizeof(struct st_info))

在该语句中调用
malloc
,并将其返回值分配给
st\u cur
。all语句的值是
st\u cur
的值。
!当st_cur为空时,st_cur
为真 所以,等价代码是

st_cur = (struct ST_info *) malloc(sizeof( struct ST_info ) ) 
if( ! st_cur )
{
    perror( "malloc failed" );
    return( 1 );
}
  • malloc(sizeof(struct ST_info)
    -->分配给定大小的内存
  • st\u cur=(struct st\u info*)malloc…
    -->将malloc返回的指针存储在
    st\u cur
    中。请注意,强制转换
    (struct st\u info*)
    是不必要的,有时是危险的。请参阅更多信息
  • 如果(!st_cur..
    -->在malloc完成后,检查
    st_cur
    的值。如果该值为0(NULL),则报告错误并返回
  • malloc(sizeof(struct ST_info)
    -->分配给定大小的内存
  • st\u cur=(struct st\u info*)malloc…
    -->将malloc返回的指针存储在
    st\u cur
    中。请注意,强制转换
    (struct st\u info*)
    是不必要的,有时是危险的。请参阅更多信息
  • 如果(!st_cur..
    -->在malloc完成后,检查
    st_cur
    的值。如果该值为0(NULL),则报告错误并返回

  • Malloc
    函数用于根据需要动态分配一些内存。使用
    Malloc
    函数的语法如下:

    (void *) malloc(size_of_memory_block)
    
    其中,argument
    size\u of_memory\u block
    是使用
    sizeof(data\u type)
    函数可以找到的以字节为单位请求的内存量。它返回一个指向所分配内存块的指针(类型为
    void*

    就你而言:

    st_cur = (struct ST_info *) malloc( sizeof (struct ST_info) )
    
    它分配与您的结构大小相同的内存块(
    struct ST_info
    ),并返回一个指向它的指针,您将该指针分配给
    ST_cur
    。有时
    Malloc
    可能会失败(主要是由于系统内存不足)分配请求的内存大小。然后它返回
    NULL
    ,您在代码中使用
    if
    条件处理该值


    Malloc
    函数返回类型总是
    void*
    ,您可以根据需要将其强制转换为类型。在代码中,您将其强制转换为type
    struct ST_info*

    Malloc
    函数用于根据需要动态分配一些内存。使用
    Malloc
    函数的语法如下:

    (void *) malloc(size_of_memory_block)
    
    其中,argument
    size\u of_memory\u block
    是使用
    sizeof(data\u type)
    函数可以找到的以字节为单位请求的内存量。它返回一个指向所分配内存块的指针(类型为
    void*

    就你而言:

    st_cur = (struct ST_info *) malloc( sizeof (struct ST_info) )
    
    它分配与您的结构大小相同的内存块(
    struct ST_info
    ),并返回一个指向它的指针,您将该指针分配给
    ST_cur
    。有时
    Malloc
    可能会失败(主要是由于系统内存不足)分配请求的内存大小。然后它返回
    NULL
    ,您在代码中使用
    if
    条件处理该值


    Malloc
    函数返回类型始终是
    void*
    ,您可以根据需要将其转换为类型。在您的代码中,您正在将其转换为类型
    struct ST_info*

    让我们先看一下:

    函数的作用是:分配大小字节并返回一个p