簡體   English   中英

使用malloc和realloc動態重新分配結構數組

[英]Dynamic reallocation an array of structures with malloc and realloc

我真的很難嘗試使用malloc和realloc創建結構數組。 我發布了大部分代碼,或者發布了與以下問題相關的信息。

struct _section {
    char *sectName;
    int start_addr;
    int end_addr;
    char *bytes;
};

struct _section *get_exe_sections(struct _section *exe_sect, Elf *elf, GElf_Ehdr *ehdr, GElf_Shdr *shdr, Elf_Data *data) { 
    exe_sect->sectName = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
    exe_sect->start_addr = shdr->sh_addr;
    exe_sect->end_addr = shdr->sh_addr + shdr->sh_size;
    exe_sect->bytes = (unsigned char *)data->d_buf;

    return exe_sect;  
}

int main(int argc, char *argv[]) {
    Elf *elf;
    int fd;

    //process input file
    int sections_count = count_sections(elf);
    GElf_Ehdr ehdr_mem;
    GElf_Ehdr *ehdr = gelf_getehdr(elf, &ehdr_mem);

    struct _section *exe_sect = (struct _section *)malloc(sizeof(struct _section));
    for(int cnt = 0; cnt < sections_count; cnt++) {
        Elf_Scn *scn = elf_getscn(elf, (size_t)cnt);
        GElf_Shdr shdr_mem;
        GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
        Elf_Data *data = elf_getdata(scn, NULL);

        if(ehdr == NULL || shdr == NULL)
            exit(1);

        if(strcmp(header_name(SECT_TYPE, GELF_ST_TYPE(shdr->sh_type)), "PROGBITS") == 0) {
            if(strcmp(flag_name(SECT_FLAGS, shdr->sh_flags), "ALLOC & EXECUTE") == 0 || \
                strcmp(flag_name(SECT_FLAGS, shdr->sh_flags), "EXECUTE") == 0) {

                exe_sect = get_exe_sections(exe_sect, elf, ehdr, shdr, data);
                struct _section *nxt_sect = (struct _section *)realloc(exe_sect, 2*sizeof(*exe_sect));
                if(nxt_sect != NULL)
                    exe_sect = nxt_sect;
            }
        }
    }
    return 0;
}

我遇到的麻煩是動態創建結構數組,並使用mallocrealloc調整結構的大小以使其適合更多數據。 如果我下的打印報表,少數是底部main輸出會給我的最后的數據,這是輸入結構。 我將如何訪問在每次調用get_exe_section期間所做的單個條目? 從以前的文章和其他資源中,我認為這會起作用,但是我無法以這種方式創建數組。 任何形式的幫助都會很棒。 謝謝。

您可以在結構中添加另一個元素,該元素指向下一部分。 這樣,您可以創建鏈接列表

struct _section {
    char *sectName;
    int start_addr;
    int end_addr;
    char *bytes;
    struct _section *next; // pointer to next section
};

您可以使用另一個Struct指向列表的開頭。

然后代替使用realloc 你可以做

exe_sect->next = (struct _section *)malloc(sizeof(struct _section));

這是我將如何更改主要功能的方法:

int main(int argc, char *argv[]) {
    Elf *elf;
    int fd;

    //process input file
    int sections_count = count_sections(elf);
    GElf_Ehdr ehdr_mem;
    GElf_Ehdr *ehdr = gelf_getehdr(elf, &ehdr_mem);

    struct _section *exe_sect = (struct _section *)malloc(sizeof(struct _section));

    for(int cnt = 0; cnt < sections_count; cnt++) {
        Elf_Scn *scn = elf_getscn(elf, (size_t)cnt);
        GElf_Shdr shdr_mem;
        GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
        Elf_Data *data = elf_getdata(scn, NULL);

        if(ehdr == NULL || shdr == NULL)
            exit(1);

        if(strcmp(header_name(SECT_TYPE, GELF_ST_TYPE(shdr->sh_type)), "PROGBITS") == 0) {
            if(strcmp(flag_name(SECT_FLAGS, shdr->sh_flags), "ALLOC & EXECUTE") == 0 || \
                strcmp(flag_name(SECT_FLAGS, shdr->sh_flags), "EXECUTE") == 0) {

                exe_sect = get_exe_sections(exe_sect, elf, ehdr, shdr, data);
                exe_sect->next = (struct _section *)malloc(sizeof(struct _section));

                if(exe_sect->next != NULL)
                    exe_sect = exe_sect->next;
            }
        }
    }
    return 0;

PS:要訪問所有單個條目,請添加另一個結構,該結構由指向列表開頭的指針和保持計數的變量組成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM