Severienne Bianca
Severienne Bianca

Reputation: 29

Deleting an object from a dynamically allocated array in C++

In the first stage, i've created an object Planet with some attributes, like name, type and distanceToEarth. I've created a Repository then, basically a structure consisting of a dynamic array elems and its length and maximum capacity.

typedef enum {
    NEPTUNE_LIKE, 
    GAS_GIANT, 
    TERRESTRIAL, 
    SUPER_EARTH, 
    UNKNOWN
}PlanetType;

typedef struct {
    char name[30];
    PlanetType type;
    float distanceToEarth;
}Planet;

Planet createPlanet(char name[], PlanetType type, double distance) {
    Planet pl;
    strcpy(pl.name, name);
    pl.distanceToEarth = distance;
    pl.type = type;
    return pl;
}

typedef struct
{
    Planet* elems;      /** dynamic array containing the planets */
    int length;         /**  actual length of the array */
    int capacity;       /**  maximum capacity of the array */
} PlanetRepo;

PlanetRepo createPlanetRepo(int capacity) {
    /// create a new planet repo; the elems field must be dynamically allocated (malloc)
    PlanetRepo r;
    r.capacity = capacity;
    r.length = 0;
    r.elems = (Planet*) malloc(sizeof(Planet)*capacity);
    return r;
}

bool remove(PlanetRepo* repo, Planet pl) {
    /// @todo remove planet pl from the repository 
    /// return false if the planet does not exist in the repository
    return false;
}

My problem is related to the function remove(). I can't figure out how I am supposed to remove that object from a dynamically allocated array.

Of course, this is not the entire code, but I've selected only the relevant parts. If I forgot to include something, let me know.

Upvotes: 1

Views: 110

Answers (3)

Victor Hugo
Victor Hugo

Reputation: 200

Like mentioned before C++ has implemented data structures so you can easily store your planets. But you can do something like:

bool remove(PlanetRepo* repo, Planet pl) {
    PlanetRepo* temp = (PlanetRepo*) malloc(sizeof(Planet)*repo->capacity);
    if(!temp){
        return false;
    }
    temp->capacity = repo->capacity;
    temp->size = repo->size-1;
    for(int i = 0; i < repo->size; ++i){
        if(repo[i] != pl){ //This won't work. How to compare Planets is left as an exercise to the reader
            temp[i] = repo[i];
        }
    }
    free(repo);
    repo = temp;
    return true;
}

Upvotes: 0

walnut
walnut

Reputation: 22152

Since you insisted on tagging C++, rather than C:

In C++ you wouldn't define the PlanetRepo and the associated functions at all. Instead you would simply declare a variable of type

std::vector<Planet>

or maybe depending on the use case (but less likely)

std::list<Planet>

Both of these already have member functions .erase that remove elements from them.


In C++ you also wouldn't write

typedef struct {
    char name[30];
    PlanetType type;
    float distanceToEarth;
}Planet;

but instead

struct Planet {
    char name[30];
    PlanetType type;
    float distanceToEarth;
};

and you would most likely use std::string instead of char[30] as type for name.

Instead of a function Planet createPlanet(char name[], PlanetType type, double distance) you would define a constructor for Planet:

struct Planet {
    std::string name;
    PlanetType type;
    float distanceToEarth;
    Planet(std::string name, PlanetType type, double distance)
      : name(name), type(type), distance(distance)
    {}
};

and probably make the members private.

You also wouldn't define an unscoped enum, but a scoped one instead (since C++11), see Why is enum class preferred over plain enum?.

Upvotes: 4

Blub
Blub

Reputation: 581

Since this is rather a C than a C++ program, you could use a linked list which makes it possible for you to delete elements in a dynamically allocated "array".

This might be of interest.

Upvotes: 0

Related Questions