简体   繁体   中英

Initialization of a vector inside a struct

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 ?

You could simplify your code to

#include <iostream>
#include <string>
#include <vector>

struct geopoint {
  double x;
  double y;
  std::string description;
};

struct georectangle {
  double left_x;
  double bottom_y;
  double right_x;
  double top_y;
  std::string description;
};

struct geomap {
  std::vector<geopoint> geopointList;
  std::vector<georectangle> georectangleList;
};

int main () {
    geomap m;
    std::cout << "m.geopointList.size(): " << m.geopointList.size() << '\n';
    std::cout << "m.georectangleList.size(): " << m.georectangleList.size() << '\n';
    m.geopointList.push_back({1, 2, "Description"});
    m.georectangleList.push_back({1, 2, 3, 4, "Description"});
    std::cout << "m.geopointList.size(): " << m.geopointList.size() << '\n';
    std::cout << "m.georectangleList.size(): " << m.georectangleList.size() << '\n';
}

to avoid such problems. Avoid dynamic memory allocation and deallocation. Don't use malloc , free , new and delete .

"How do I initialize these two vectors to be fresh new empty vectors?" The default constructor does this for you.

"Is it possible that their size will become bigger than the size of the struct itself? Is the struct going to ```realloc``" The struct has a fixed size and contains two vectors. Both vectors contain a reference/pointer to dynamic memory outside of the struct. The struct and both vectors are created on the stack (in my example code) and the dynamic memory of the vectors is on the heap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM