C 结构的分配和自由对象

C 结构的分配和自由对象,c,malloc,C,Malloc,通用条款4.4.4 c89 我的channel.h文件中有以下代码 typedef struct channel_tag channel_t; channel_t* open_channel(size_t channel_id); void close_channel(channel_t *channel); 在我的channel.c文件中 #include "channel.h" struct channel_tag { size_t channel_id; }; channe

通用条款4.4.4 c89

我的channel.h文件中有以下代码

typedef struct channel_tag channel_t;

channel_t* open_channel(size_t channel_id);
void close_channel(channel_t *channel);
在我的channel.c文件中

#include "channel.h"

struct channel_tag {
    size_t channel_id;
};

channel_t* open_channel(size_t channel_id)
{
    channel_t *channel = malloc(sizeof *channel);

    if(channel == NULL) {
        fprintf(stderr, "Cannot allocate memory\n");
        return NULL;
    }

    channel->channel_id = channel_id;
    printf("Channel [ %zu ] has been created\n", channel->channel_id);

    return channel;
}

void close_channel(channel_t *channel)
{
    printf("Channel [ %zu ] resources has been released\n", channel->channel_id);
    free(channel);
}
问题在于我的主.c文件。这里我有一个for循环,它创建5个通道对象并为它们分配内存。然而,如果我想在以后的程序中释放它们,我不确定如何获得它们的引用。这只是我正在测试的5个。但后来可能会达到300

int main(void)
{
    size_t i = 0;

    channel_t *channel = NULL;

    for(i = 0; i < 4; i++) {
        channel = open_channel(i);

        if(channel == NULL) {
            fprintf(stderr, "Cannot create channel [ %zu ]\n", i);
        }
    }

    /* Do some stuff with the channels and now free them before the program exists. 
       However, I need to loop and pass all of them, not just one */
    for(i = 0; i < 4; i++) {
       close_channel(channel);
    }
    return 0;
}
int main(无效)
{
尺寸i=0;
channel_t*channel=NULL;
对于(i=0;i<4;i++){
信道=开放信道(i);
如果(通道==NULL){
fprintf(stderr,“无法创建通道[%zu]\n”,i);
}
}
/*对频道做一些事情,现在在节目存在之前释放它们。
但是,我需要循环并传递所有这些,而不仅仅是一个*/
对于(i=0;i<4;i++){
关闭_通道(通道);
}
返回0;
}

非常感谢您的建议,

好吧,您正在一次又一次地重写同一频道的主要内容。如果您想要4个通道,显然需要4个变量来存储它们,或者需要一个包含4项的数组

channel_t*channel[4];对于(…)信道[i]=开放信道(i)


Oh和
for(inti=0;i<4;i++)
将生成4个循环,而不是5个循环。

好吧,您正在主通道中反复重写同一个通道。如果您想要4个通道,显然需要4个变量来存储它们,或者需要一个包含4项的数组

channel_t*channel[4];对于(…)信道[i]=开放信道(i)


Oh和
for(int i=0;i<4;i++)
将生成4个循环,而不是5个循环。

在创建通道时将其存储在数组中。确保您可以在程序结束时判断
malloc
s是否工作(因此在此代码中的
memset

channel\u t**channel=malloc(5*sizeof(channel\u t*));
memset(通道,0,5*sizeof(通道t*);
对于(i=0;i<5;i++){
信道[i]=开放信道(i);
if(通道[i]==NULL){
fprintf(stderr,“无法创建通道[%zu]\n”,i);
}
}
/*对频道做一些事情,现在在节目存在之前释放它们。
但是,我需要循环并传递所有这些,而不仅仅是一个*/
对于(i=0;i<5;i++){
if(channel[i]!=NULL)/*处理某些打开失败的情况*/
{
关闭_通道(通道[i]);
}
}
自由(通道);

在创建频道时将其存储在阵列中。确保您可以在程序结束时判断
malloc
s是否工作(因此在此代码中的
memset

channel\u t**channel=malloc(5*sizeof(channel\u t*));
memset(通道,0,5*sizeof(通道t*);
对于(i=0;i<5;i++){
信道[i]=开放信道(i);
if(通道[i]==NULL){
fprintf(stderr,“无法创建通道[%zu]\n”,i);
}
}
/*对频道做一些事情,现在在节目存在之前释放它们。
但是,我需要循环并传递所有这些,而不仅仅是一个*/
对于(i=0;i<5;i++){
if(channel[i]!=NULL)/*处理某些打开失败的情况*/
{
关闭_通道(通道[i]);
}
}
自由(通道);
int main(无效)
{
int i=0;
通道**通道=malloc(5*sizeof(通道*);
如果(通道==NULL)退出(1);//错误等。
对于(i=0;i<5;i++){
信道[i]=开放信道(i);
如果(通道[i]==NULL){
fprintf(stderr,“无法创建通道[%d]\n”,i);
打破
}
}
//…工作,但前提是i==5
对于(i--;i>=0;i--){
关闭_通道(通道[i]);
}
免费(频道);
返回0;
}
int main(无效)
{
int i=0;
通道**通道=malloc(5*sizeof(通道*);
如果(通道==NULL)退出(1);//错误等。
对于(i=0;i<5;i++){
信道[i]=开放信道(i);
如果(通道[i]==NULL){
fprintf(stderr,“无法创建通道[%d]\n”,i);
打破
}
}
//…工作,但前提是i==5
对于(i--;i>=0;i--){
关闭_通道(通道[i]);
}
免费(频道);
返回0;
}

您的右侧只有4个循环,该代码只是为了测试我的理论。谢谢。你的权利只有4个循环,那代码只是为了测试我的理论。谢谢。
memset()
在这种情况下是不需要的:
channel[i]
在测试之前被分配到任何地方。如果要检查
malloc()
的结果,应在调用
channel
@Sjoerd上的
memset()
之前进行检查-是的,如果失败后不继续循环,
memset
非常有用。在使用内存之前,应检查
malloc
结果。在这种情况下,不需要
memset()
channel[i]
在测试之前已分配给所有通道。如果要检查
malloc()
的结果,应在调用
channel
@Sjoerd上的
memset()
之前进行检查-是的,如果失败后不继续循环,
memset
非常有用。在使用内存之前,应检查
malloc
结果。
channel_t **channel = malloc(5 * sizeof(channel_t*));
memset(channel, 0, 5 * sizeof(channel_t*));

for(i = 0; i < 5; i++) {
    channel[i] = open_channel(i);

    if(channel[i] == NULL) {
        fprintf(stderr, "Cannot create channel [ %zu ]\n", i);
    }
}

/* Do some stuff with the channels and now free them before the program exists. 
   However, I need to loop and pass all of them, not just one */
for(i = 0; i < 5; i++) {
   if (channel[i] != NULL)  /* Handle case where some of the opens failed */
   {
     close_channel(channel[i]);
   }
}

free(channel);
int main( void )
{
    int i = 0;
    channel_t** channels = malloc( 5 * sizeof( channel_t* ));

    if ( channels == NULL ) exit( 1 ); // error, etc.

    for ( i = 0; i < 5; i++ ) {
        channels[i] = open_channel( i );

        if ( channels[i] == NULL ) {
            fprintf( stderr, "Cannot create channel [ %d ]\n", i );
            break;
        }
    }

    // ... work, but only if i == 5

    for ( i--; i >= 0; i-- ) {
       close_channel( channels[i] );
    }

    free( channels );
    return 0;
}