结构数组的malloc()具有不同尺寸结构 [英] malloc() of struct array with varying size structs

查看:107
本文介绍了结构数组的malloc()具有不同尺寸结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个人怎么的malloc结构的数组如果正确每个结构包含一个字符串数组,大小不同?

因此​​,每个结构体可能有不同的尺寸,并会使得不可能


  

的realloc(numberOfStructs *的sizeof(structName))



  

的malloc(INITIALSIZE *的sizeof(structName)


怎样才能为这个分配内存,并跟踪到底是怎么回事呢?


解决方案

我在做一些猜测在这里,根据您提供的信息。我可以看到想要 realloc的唯一的原因结构的数组是,如果你想更多的结构添加到该数组。这很酷。有充分的理由要那种动态存储。来处理它,尤其是在结构本身是动态的,最好的办法,就是保持的指针的数组这些结构。例如:


  

1。数据结构为:


  typedef结构{
    INT numberOfStrings;
    字符的字符串**;
}
的StringHolder;typedef结构{
    诠释numberOfStructs;
    **的StringHolder结构;
}
structList;


  

2。管理字符串动态数组:


 无效createNewStringHolder(**的StringHolder持有人){
    (*持有人)=的ma​​lloc(sizeof的(的StringHolder));
    (*持有者) - > numberOfStrings = 0;
    (*持有者) - >字符串= NULL;
}无效destroyStringHolder(**的StringHolder持有人){
    //首先,释放每个串
    INT stringIndex;
    为(stringIndex = 0; stringIndex≤(*持有者) - > numberOfStrings; stringIndex ++)
    {免费((*持有人) - GT;字符串[stringIndex]); }
    //接下来,释放字符串[]数组
    免费((*持有人) - GT;字符串);
    //最后释放支架本身
    免费((*持有人));
}无效addStringToHolder(*的StringHolder持有人,为const char *字符串){
    INT newStringCount =持有人> numberOfStrings + 1;
    焦炭** newStrings = realloc的(持有人>字符串,newStringCount * sizeof的(字符*));
    如果(newStrings!= NULL){
        持有人> numberOfStrings = newStringCount;
        持有人>字符串= newStrings;
        newStrings [newStringCount - 1] =的malloc((strlen的(字符串)+ 1)* sizeof的(炭));
        的strcpy(newStrings [newStringCount - 1],字符串);
    }
}


  

3。管理结构的动态数组:


 无效createNewStructList(structList **名单,诠释INITIALSIZE){
    //创建一个新的列表
    (*名单)=的ma​​lloc(sizeof的(structList));
    //创建结构指针的新名单
    (*名单) - GT; numberOfStructs = INITIALSIZE;
    (*名单) - GT;结构=的malloc(INITIALSIZE * sizeof的(*的StringHolder));
    //初始化新结构
    INT structIndex;
    为(structIndex = 0; structIndex&下; INITIALSIZE; structIndex ++)
    {createNewStringHolder(及((*列表) - >结构[structIndex])); }
}无效destroyStructList(structList **名单){
    //销毁列表中的每个结构
    INT structIndex;
    为(structIndex = 0; structIndex≤(*列表) - > numberOfStructs; structIndex ++)
    {destroyStringHolder(及((*列表) - >结构[structIndex])); }
    //破坏列表本身
    免费((*列表));
}*的StringHolder addNewStructToList(structList *名单){
    INT newStructCount =列表 - > numberOfStructs + 1;
    为size_t newSize = newStructCount * sizeof的(*的StringHolder);
    **的StringHolder = newList的realloc(列表 - >结构,newSize);
    如果(newList!= NULL){
        列表 - > numberOfStructs = newStructCount;
        列表 - >结构= newList;
        createNewStringHolder(及(newList [newStructCount - 1]));
        返回newList [newStructCount - 1];
    }
    返回NULL;
}


  

4。主程序:


 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;INT主(INT ARGC,CHAR *的argv []){
    structList * allHolders;
    createNewStructList(安培; allHolders,10);    addStringToHolder(allHolders->结构[4],风把它);
    addStringToHolder(allHolders->结构[4],我是不是仁慈?);
    addStringToHolder(allHolders->结构[7],阿齐兹,光!);    的printf(%S \\ n,allHolders->结构[4] - >字符串[0]); //风把它
    的printf(%S \\ n,allHolders->结构[4] - >字符串[1]); //我不是仁慈?
    的printf(%S \\ n,allHolders->结构[7] - >字符串[0]); //阿齐兹,光!    *的StringHolder = newHolder addNewStructToList(allHolders);
    addStringToHolder(newHolder,你不能过去!);
    的printf(%S \\ n,newHolder->字符串[0]); // 你不可以过去!
    的printf(%S \\ N,allHolders->结构[10] - GT;串[0]); // 你不可以过去!    destroyStructList(安培; allHolders);
    返回0;
}

How does one malloc an array of structs correctly if each struct contains an array of strings which vary in size?

So each struct might have a different size and would make it impossible to

realloc(numberOfStructs * sizeof(structName))

after

malloc(initialSize * sizeof(structName)

How does one allocate memory for this and keep track of what is going on?

解决方案

I am making some guesses here, based on the information you have provided. The only reason I can see for wanting to realloc an array of structs is if you want to add more structs to that array. That's cool. There are plenty of reasons to want that kind of dynamic storage. The best way to handle it, especially if the structures are themselves dynamic, is to keep an array of pointers to these structures. Example:

1. Data structure:

typedef struct {
    int numberOfStrings;
    char ** strings;
}
stringHolder;

typedef struct {
    int numberOfStructs;
    stringHolder ** structs;
}
structList;

2. Managing dynamic arrays of strings:

void createNewStringHolder(stringHolder ** holder) {
    (*holder) = malloc(sizeof(stringHolder));
    (*holder)->numberOfStrings = 0;
    (*holder)->strings = NULL;
}

void destroyStringHolder(stringHolder ** holder) {
    // first, free each individual string
    int stringIndex;
    for (stringIndex = 0; stringIndex < (*holder)->numberOfStrings; stringIndex++)
    { free((*holder)->strings[stringIndex]); }
    // next, free the strings[] array
    free((*holder)->strings);
    // finally, free the holder itself
    free((*holder));
}

void addStringToHolder(stringHolder * holder, const char * string) {
    int newStringCount = holder->numberOfStrings + 1;
    char ** newStrings = realloc(holder->strings, newStringCount * sizeof(char *));
    if (newStrings != NULL) {
        holder->numberOfStrings = newStringCount;
        holder->strings = newStrings;
        newStrings[newStringCount - 1] = malloc((strlen(string) + 1) * sizeof(char));
        strcpy(newStrings[newStringCount - 1], string);
    }
}

3. Managing a dynamic array of structures:

void createNewStructList(structList ** list, int initialSize) {
    // create a new list
    (*list) = malloc(sizeof(structList));
    // create a new list of struct pointers
    (*list)->numberOfStructs = initialSize;
    (*list)->structs = malloc(initialSize * sizeof(stringHolder *));
    // initialize new structs
    int structIndex;
    for (structIndex = 0; structIndex < initialSize; structIndex++)
    { createNewStringHolder(&((*list)->structs[structIndex])); }
}

void destroyStructList(structList ** list) {
    // destroy each struct in the list
    int structIndex;
    for (structIndex = 0; structIndex < (*list)->numberOfStructs; structIndex++)
    { destroyStringHolder(&((*list)->structs[structIndex])); }
    // destroy the list itself
    free((*list));
}

stringHolder * addNewStructToList(structList * list) {
    int newStructCount = list->numberOfStructs + 1;
    size_t newSize = newStructCount * sizeof(stringHolder *);
    stringHolder ** newList = realloc(list->structs, newSize);
    if (newList != NULL) {
        list->numberOfStructs = newStructCount;
        list->structs = newList;
        createNewStringHolder(&(newList[newStructCount - 1]));
        return newList[newStructCount - 1];
    }
    return NULL;
}

4. Main program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char * argv[]) {
    structList * allHolders;
    createNewStructList(&allHolders, 10);

    addStringToHolder(allHolders->structs[4], "The wind took it");
    addStringToHolder(allHolders->structs[4], "Am I not merciful?");
    addStringToHolder(allHolders->structs[7], "Aziz, Light!");

    printf("%s\n", allHolders->structs[4]->strings[0]);  // The wind took it
    printf("%s\n", allHolders->structs[4]->strings[1]);  // Am I not merciful?
    printf("%s\n", allHolders->structs[7]->strings[0]);  // Aziz, Light!

    stringHolder * newHolder = addNewStructToList(allHolders);
    addStringToHolder(newHolder, "You shall not pass!");
    printf("%s\n", newHolder->strings[0]);               // You shall not pass!
    printf("%s\n", allHolders->structs[10]->strings[0]); // You shall not pass!

    destroyStructList(&allHolders);
    return 0;
}

这篇关于结构数组的malloc()具有不同尺寸结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆