How to properly malloc for array of struct in C

前端 未结 2 1065
粉色の甜心
粉色の甜心 2020-12-08 15:57

I will read in two set of char* (or strings) using strtok, and since those two set of chars are related, (address : command\\n) I deci

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-08 16:40

    For what you have described, You do not need to allocate memory for your struct, rather, you need to allocate memory for the members char *addr;, and char *inst;. If you want to have a single copy of that structure, the first section of code illustrates how to initialize, and assign values. If you want an array, the second code example illustrates the differences.

    This illustrates how to allocate memory for the members of a single struct line:

    typedef struct
    {
        char* addr;
        char* inst;
    }LINE;
    
    LINE line;  
    
    int main(void)
    {   
    
        strcpy(line.addr, "anystring"); //will fail
        line.addr = malloc(80);
        line.inst = malloc(80);
        strcpy(line.addr, "someString");//success;
        strcpy(line.inst, "someOtherString");//success;
    
    }
    

    For array of struct line...

    typedef struct
    {
        char* addr;
        char* inst;
    }LINE;  //same struct definition
    
    LINE line[10]; //but create an array of line here.
    
    int main(void)
    {   
        int i;
        
        for(i=0;i<10;i++)
        {
          line[i].addr = malloc(80);
          line[i].inst = malloc(80);
        }
    
        for(i=0;i<10;i++)
        {
            strcpy(line[i].addr, "someString");
            strcpy(line[i].inst, "someOtherString");
        }
        //when done, free memory
        for(i=0;i<10;i++)
        {
            free(line[i].addr);
            free(line[i].inst);
        }      
    
    
    }
    

    Added to address comment
    Addressing the comment under this answer from @Adam Liss, the following code illustrates the following improvements using strdup(): 1) Uses only memory needed. 2) Performs memory creation and copy operations in one step, so the the following blocks:

    for(i=0;i<10;i++)
    {
      line[i].addr = malloc(80);
      line[i].inst = malloc(80);
    }
    
    for(i=0;i<10;i++)
    {
        strcpy(line[i].addr, "someString");
        strcpy(line[i].inst, "someOtherString");
    }
    

    Become:

    for(i=0;i<10;i++)
    {
      line[i].addr = strdup("someString");
      line[i].inst = strdup("someOtherString");
    }
    

    One more note: Error handling was not included in examples above to avoid muddling up focus on the main concepts: But for the sake of completeness, because both malloc() and strdup() can fail, actual usage for each of these two functions, should include a test before using, eg:

    Rather than

      line[i].addr = strdup("someString");
      line[i].inst = strdup("someOtherString");
    

    The code should include

      line[i].addr = strdup("someString");
      if(!line[i].addr)
      {
          //error handling code here
      }
      line[i].inst = strdup("someOtherString");
      if(!line[i].inst)
      {
          //error handling code here
      }
    

提交回复
热议问题