struct geopoint {
double x;
double y;
const char * description;
};
struct georectangle {
double left_x;
double bottom_y;
double right_x;
double top_y;
const char * description;
};
struct geomap {
vector < geopoint * > geopointList;
vector < georectangle * > georectangleList;
};
struct geomap * geomap_new() {
struct geomap * newGeoMap = (struct geomap * ) malloc(sizeof(struct geomap));
return newGeoMap;
}
void geomap_delete(struct geomap * m) {
printf("%lu\n", m->geopointList.size());
for (int i = 0; i < m->geopointList.size(); i++) {
free(m->geopointList[i]);
}
printf("%lu\n", m->georectangleList.size());
for (int i = 0; i < m->georectangleList.size(); i++) {
free(m->georectangleList[i]);
}
free(m);
}
int main () {
struct geomap * m = geomap_new();
assert(m);
geomap_delete(m);
}
I'm new to C++ and I'm super confused about object initialization in this language... In Java you always use the new
keyword when you initialize an object not of a primitive type. In C++, it looks to me that sometimes the default constructor is automatically executed and sometimes it isn't.
In the above snippet of code through the geomap_new()
function I create an instance of struct geomap
which contains two vectors of pointers.
My questions are the following:
How do I initialize these two vectors to be fresh new empty vectors? In Java I would use the new
keyword... Is there such thing also in C++?
I'm asking this question because if I don't initialize them in any way, when I printf the size of these two vectors in the geomap_delete
function, the size of the geopointList
is 0, as it should be, but the size of the georectangleList
is a big random number. It looks like to me that only the first vector is being initialized.
Another question...
If a start adding a lot of stuff in the vectors, these vectors will start growing up. Is it possible that their size will become bigger than the size of the struct itself? Is the struct going to realloc
?