-1
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?

1201ProgramAlarm
  • 30,320
  • 7
  • 40
  • 49
Marco
  • 23
  • 4
  • 3
    If you use `std::vector`, code is not C, but C++. So don't use `malloc` but `new` (or even better avoid it also by using correct classes). – Jarod42 Jan 15 '20 at 10:48
  • Can't reproduce, I get `0` for both vector sizes. – Blaze Jan 15 '20 at 10:50
  • 2
    `geomap_new`/`geomap_delete` should be removed. `consr char*` might be replaced by `std::string`, `std::vector` might also be `std::vector`. – Jarod42 Jan 15 '20 at 10:54
  • 2
    Approaching C++ expecting it to be like Java is going to cause you problems. These questions are fundamental to how memory is managed in C++, how objects are stored, and the various forms of initialization. It may be more useful to supplement your experimentation with some reference material covering these topics. – paddy Jan 15 '20 at 10:54
  • 1
    dont try to apply things you know from java to c++. You will fail. For example the use of `new` is a major difference between the two. Read more on this here: https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new – 463035818_is_not_a_number Jan 15 '20 at 10:55
  • 1
    Please try to concentrate on one question per question – 463035818_is_not_a_number Jan 15 '20 at 10:58
  • Which [book](https://stackoverflow.com/q/388242/560648) are you using to learn C++? – Lightness Races in Orbit Jan 15 '20 at 11:23

1 Answers1

0

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.

Thomas Sablik
  • 15,040
  • 7
  • 26
  • 51