简体   繁体   English

结构内向量的初始化

[英]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.我是 C++ 的新手,我对这种语言中的对象初始化感到非常困惑......在 Java 中,当你初始化一个不是原始类型的对象时,你总是使用new关键字。 In C++, it looks to me that sometimes the default constructor is automatically executed and sometimes it isn't.在 C++ 中,在我看来,有时会自动执行默认构造函数,有时则不会。

In the above snippet of code through the geomap_new() function I create an instance of struct geomap which contains two vectors of pointers.在上面通过geomap_new()函数的代码片段中,我创建了一个包含两个指针向量的struct geomap实例。

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++?在 Java 中,我会使用new关键字... 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.我问这个问题是因为如果我不以任何方式初始化它们,当我在geomap_delete函数中geomap_delete这两个向量的大小时, geopointList的大小geopointList是 0,但是georectangleList是一个很大的随机数。 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 ?是结构要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 .不要使用mallocfreenewdelete

"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. “它们的大小有可能会变得比结构本身的大小更大吗?结构是否会```realloc``”该结构具有固定大小并包含两个向量。 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.结构体和两个向量都在堆栈上创建(在我的示例代码中),向量的动态内存在堆上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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