gpt4 book ai didi

c - 使用 malloc 和 realloc 动态重新分配结构体数组

转载 作者:行者123 更新时间:2023-11-30 16:36:14 31 4
gpt4 key购买 nike

我真的很难尝试使用 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
};

您可以使用另一个结构来指向列表的头部。

然后而不是使用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:要访问所有单独的条目,请添加另一个结构,该结构由指向列表头部的指针和一个用于计数的变量组成。

关于c - 使用 malloc 和 realloc 动态重新分配结构体数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48585003/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com