Deleting data in Data Structures

Pages: 12
I know that with classes you have deconstructors, but is there anything I can use for data structures?

EX:

1
2
3
4
5
6
7
8
9
10
struct level{
    double experience = 0;
    long damage = 0;
    long health = 100;
};

struct ship{
    vector<level> levels = vector<level>();
    string ship_name = "NO_ID";
};


lets call this a small psuedo structure. If I used the same structure address (in other words, I never declare a new instance, I only use 1), and I want to delete all the data in this structure, is there any way other than writeing a function to clear it all/declare defaults?

ex code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void load_everything(ship& player_ship)
{
    /*load load load...*/
}

void clear_ship_data(ship& player_ship)
{
    /*Just basically calling all the members of ship
    and setting their default values...*/
    player_ship.ship_name = "NO_ID";
    // the vector of levels will have to be completely re-declared, as each level has different attributes/etc...
}

main()
{
    ship player_ship;
    load_everything(player_ship);
    //now mabey some calculations or whatever
    //new ship??  alright, lets delete the old one now...
    clear_ship_data(player_ship);
    return 0;
}


don't focus on the code errors in that, I pretty much just wrote that on the spot just now to give you an idea of what I'm currently doing to clear data structures.
Last edited on
In c++ structs have constructors and destructors too, but the dtor wont be called until the variable goes out of scope or if you call delete on it (if it was allocated with new). For what your doing just reseting the variables to defaults looks like it will make the most sense. I also want to note you shouldn't do this player_ship.levels = vector<levels>(); clearing the vector should be sufficient, your adding unnecessary overhead with ctor and operator= calls.
Last edited on
alright. ty naraku.

So, how could I allocate/delete the memory for these structures?

I want it to be as efficient as possible.

also, I want to add another question:

What if I have this structure:

1
2
3
struct ship_base_data{
/*ints, floats, bla bla bla*/
} ship_name_base;


I want to call the instance without creating a new one (use the same memory space, so as to not take up more memory), so (I'm trying to confirm what I know... please correct me if im wrong) I would call it like this:

1
2
3
4
5
6
7
8
#include "whatever_header_that_structure_is_in.h"

int main()
{
    //just psuedo bla, so...
    &ship_name_base.health = (&ship_name_base.health - 1);
    return 0;
}


I know that wouldn't be practical, but for other purposes (like program mechanics, such as a bool that stores whether the program is on it's first ever run) I would like to know if this is feasable, so I don't have to pass the structures from function-to-function.
Last edited on
(De)allocating memory on the stack is done automatically. To allocate memory on the heap use new and to free it use delete
@ maeriden

so....

1
2
3
4
5
new struct ship_data{
//blablabla
} ship;

delete &ship;
Last edited on
Nope. Read this and see if you can understand it better
http://www.cplusplus.com/doc/tutorial/dynamic/
so..... i can delete whole datastuctures or only the variables??
You can delete every data type. E.g.
1
2
3
4
5
6
int main()
{
  ship* myship = new ship();
  delete myship;
  return 0;
}
alright... hold on... I'm not completely versed with pinters, so:

myship points to the address of ship, so if we modified myship, we would modify ship as well?

and why are we using ship to declare myship with? I thought we had to use the structure's handle:

ship_data* myship = new ship_data();

Thank you for your help. I really appreciate it.
and why are we using ship to declare myship with? I thought we had to use the structure's handle:
I'm not sure what you mean with handle (english is not my native language), but your struct is called 'ship', so I declared a variable of type 'ship*'. If it was called 'ship_data' then your declaration would be correct

myship points to the address of ship
Not exactly. It point to an instance of the class 'ship' (or "an object of type 'ship'")

so if we modified myship, we would modify ship as well?
If you modify a pointer it means you're making it point to another object. If you mean in the case of using it as argument for a function call then yes, modifying the object pointed by it will modify the same object that exists in the calling function.

I guess this is a bit confusing. Unfortunately to use dynamic memory you have to use pointers, and I'm not probably good enough to explain them properly
Last edited on
@IWishIKnew
I think your over complicating things. If you want to save memory by re-using the object, then just reinitialize the data in the object.
1
2
3
4
5
6
7
8
9
struct ship{
    ship() : ship_name("NO_ID"){}
    void clear(){
        levels.clear();
        ship_name = "NO_ID";
    }
    vector<level> levels;
    string ship_name;
};


1
2
3
4
5
6
int main(){
    ship s;
    //do stuff
    s.clear();//re-init data
    //do more stuff
}
Last edited on
... huh...

so .clear() doesn't actually delete anything? It just re-initializes it?

@maeriden

your struct is called 'ship', so I declared a variable of type 'ship*'


no, my struct is called ship_data with a declaration of ship being of type ship_data.

But, yes, I get it then, if that is what you meant.
Last edited on
so .clear() doesn't actually delete anything? It just re-initializes it?
The clear() member of ship doesn't, the clear member of std::vector deletes the memory for the internally allocated (on the heap).
thank you naraku! Now I don't have to write functions for that anymore. You've saved me a lot of time. :D
hold on... Let me get this right:

if:

1
2
3
4
5
6
7
8
9
struct ship{
    //variables...
};

int main()
{
    ship newship, newship2;
    //does "ship.clear()" reset both of them?
}
No, perhaps you should read up on how structs/classes and objects work.
No, you don't have an instance of the class called ship. In the case of the 2 objects of the ship class you did declare, you would call like this:

1
2
newship.clear();
newship2.clear();


You can only call member functions of instantiated objects by using the name of the object (or through a pointer to the object which has a slightly different syntax.)

Remember, a class definition isn't an object. It's simply a blueprint that tells the compiler how to build and use an object of that type.
@Raezzor

IM NOT USING CLASSES I'm using data structures. This is what I get when I use .clear of an object of type (whatever datat structure):

".clear() not a member os (whetever datastructure)"

It does not work. I'm not using classes.

If it was a class, IT WOULD BE LIKE THIS:

1
2
3
class ship{
    BLABLABLA
};


not like this:

1
2
3
struct ship{
    BLABLABLA
};


And, @ naraku:

.clear() is not a memebr of my data structure. WHY? because I didn't make it one, because functions can not be part of data structures. That would be what classes are for.
Last edited on
IM NOT USING CLASSES I'm using data structures.

In C++, a class and a struct are identical except for default privacy level. So this:
1
2
3
class ship{
    BLABLABLA
};

and this
1
2
3
struct ship{
    BLABLABLA
};

are very nearly the exact same thing, and to claim there's some big difference between them is just plain wrong.

because functions can not be part of data structures
That is wrong. It is not true. In C++, structs have function just like classes.
Last edited on
hold on... Let me get this right:

if:

1
2
3
4
5
6
7
8
9
struct ship{
    //variables...
};

int main()
{
    ship newship, newship2;
    //does "ship.clear()" reset both of them?
}


If clear() isn't a member of ship then why are you using the member operator? To use a non-member function on an object you'll have to pass that object to the function.

Besides, what I said about being a blueprint was for objects of any type, not just classes. Structures operate under the same principal. Even once a struct is defined you have to instantiate it by declaring it like a variable, IE typename objectname;. Trying to access any part of a user-defined data type before you create an object of that type won't work.
Last edited on
Pages: 12