इसलिए मैं अपने एक छोटे से प्रोजेक्ट पर काम कर रहा हूं और मैं इस चरण में फंस गया हूं। मुझे Segfaults मिलते रहते हैं और मैं हताश हो रहा हूँ। मेरे पास 2 संरचनाएं हैं

typedef struct {
    float time;
}Race;

typedef struct {
    char DriverName[50];
    int NoRaces;
    Race *races;
}Driver;

और मुझे प्रत्येक के लिए दौड़ की संख्या दिए जाने के बाद एक ड्राइवर के लिए और फिर ड्राइवरों की एक सरणी के लिए स्मृति आवंटित करनी होगी। यह मेरा अब तक का कोड है

Driver *allocDriver(int noRaces) {
    Driver *driver;
    driver = (Driver *)malloc(sizeof(Driver));
    driver->NoRaces = noRaces;
    driver->races = (Race *)malloc(sizeof(Race));       

    return driver;}


Driver **allocDrivers(int driversNo, int *driversRacesNo) {
    int i;
    Driver **drivers;
    drivers = (Driver **)malloc(driversNo * sizeof(Driver *));
    for(i = 0; i < driversNo; i++)
        drivers[i] = allocDriver(driversRacesNo[i]);

    return drivers;}
1
OscarTheMighty 29 पद 2019, 18:23

1 उत्तर

यहां ubuntu 18.04 पर संकलित संरचनाओं की तालिकाओं के आवंटन के बारे में एक 'विस्तारित' लेकिन सरल उदाहरण दिया गया है। आशा है कि यह स्वयं व्याख्यात्मक और पर्याप्त उदाहरण होगा। भविष्य के विकास और अभ्यास के लिए सरणी के बजाय लिंक्ड सूची का उपयोग करने का प्रयास करें।

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

typedef struct sRace {
    float time;
}Race;

typedef struct sDriver {
    char DriverName[50];
    int NoRaces;
    Race *races;
}Driver;


typedef struct sDriversTable {
    int numdrivers;
    Driver** drivers;
} DriversTable;



Driver *allocDriver(char* name, int numraces)
{
    Driver *driver = calloc(1, sizeof(Driver));
    driver->NoRaces = numraces;
    strcpy(driver->DriverName, name);
    driver->races = calloc(numraces,sizeof(Race));
    return driver;
}

DriversTable* allocDriversTable(int numdrv)
{
     DriversTable* dt = calloc(1,sizeof(DriversTable));
     dt->numdrivers = numdrv;
     dt->drivers = calloc(dt->numdrivers, sizeof(Driver*));
    return dt;
}


void addDriver(DriversTable* drv, char* name, int numraces)
{
    for(int i = 0; i < drv->numdrivers; i++)
    {
        if( drv->drivers[i]  == NULL)
        {
            drv->drivers[i] = allocDriver(name, numraces);
            break;
        }
    }
}

void setDriverTimeForTheRace(DriversTable* drv, char* name, int raceindex, float time)
{

    for(int i = 0; i < drv->numdrivers; i++)
    {
        if( !strcmp(drv->drivers[i]->DriverName,name) )
        {
            if( raceindex < drv->drivers[i]->NoRaces )
            {
               drv->drivers[i]->races[raceindex].time = time;
            }
            else
            {
                printf("invalid race index. %d, (max. for the driver %s is %d\n",raceindex,drv->drivers[i]->DriverName, drv->drivers[i]->NoRaces -1);
            }
            break;
        }
    }
}

void printDriversTable(DriversTable* drv)
{
    for(int i = 0; i < drv->numdrivers; i++)
    {
        if( drv->drivers[i]  != NULL)
        {
            printf("Driver:%s, Races: %2d\n", drv->drivers[i]->DriverName, drv->drivers[i]->NoRaces);
            for(int j = 0; j < drv->drivers[i]->NoRaces; j++)
            {
                if(drv->drivers[i]->races[j].time > 0.0 )
                    printf("{%4.2f}", drv->drivers[i]->races[j].time);
                else
                    printf("{%4s}", "n/a");
            }
            printf("\n");
        }
    }
}

void ReleaseResources(DriversTable** dt)
{
    DriversTable* drv = *dt;
    if( drv == NULL)
    {
      printf("nothing to release!\n");
      return;
    }
    for(int i = 0; i < drv->numdrivers; i++)
    {
        if( drv->drivers[i]  != NULL)
        {
            free(drv->drivers[i]->races);
            drv->drivers[i]->races = NULL;
            free(drv->drivers[i]);
            drv->drivers[i]=NULL;
        }
    }

    free(drv->drivers);
    drv->drivers = NULL;
    free(drv);
    *dt = NULL;
    printf("released!\n");
}

int main()
{
    DriversTable *drv = allocDriversTable(10);
    addDriver(drv,"Emerson", 6);
    setDriverTimeForTheRace(drv,"Emerson",1, 34.5);
    setDriverTimeForTheRace(drv,"Emerson",2, 33.6);
    setDriverTimeForTheRace(drv,"Emerson",7, 34.5);
    addDriver(drv,"Lake",7);
    setDriverTimeForTheRace(drv,"Lake",3, 21.5);
    setDriverTimeForTheRace(drv,"Lake",5, 22.6);
    setDriverTimeForTheRace(drv,"Lake",7, 23.5);
    addDriver(drv,"Palmer",8);
    setDriverTimeForTheRace(drv,"Palmer",4, 21.5);
    setDriverTimeForTheRace(drv,"Palmer",6, 22.6);
    setDriverTimeForTheRace(drv,"Palmer",7, 23.5);
    printDriversTable(drv);
    ReleaseResources(&drv);
    ReleaseResources(&drv);
    return 0;
}
0
risbo 30 पद 2019, 00:55