级联结构指针测试代码中的分段错误

发布于 2024-12-08 07:21:37 字数 2436 浏览 1 评论 0原文

以下虚拟测试代码在执行结束时给出分段错误(更具体地说,在 main 中返回 0 时)。我想知道这种行为的原因。是因为它无法释放虚拟变量吗?我使用的是 g++ 4.4,没有用于测试的优化标志。

#include <vector>
#include <boost/multi_array.hpp>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using std::vector;

typedef boost::multi_array<float, 1> DVec;

class Point{
  public:
    int x, y;
    double *dist;
    DVec dir;
};

struct another_struct {
  vector <Point *>c;
};

struct in_foo{
  vector <another_struct *>aVec;
  char *aname;
  float b;
};

struct foo {
  DVec b;
  vector<in_foo *> mVec;
};

int main(){

  DVec c(boost::extents[4]);
  foo **dummy = (foo **) calloc(4, sizeof(*dummy));
  vector <in_foo *>test_var(5);

  for(int i =0; i < 6; i++){
    test_var[i] = (in_foo *) malloc(sizeof(in_foo));
    memset(test_var[i], 0, sizeof(*test_var[i]));
    test_var[i]->aname = "42!\n";
    test_var[i]->b = (float) i;
  }

  for (int i = 0 ; i < 4; i++) {
    dummy[i] = (foo *) malloc(sizeof(*dummy[i]));
    (dummy[i]->b).resize(boost::extents[2]);
    (dummy[i]->mVec) = test_var;
  }

  for (int i = 0 ; i < 4; i++) {
    for(int j = 0; j < 5; j++){
      (dummy[i]->mVec[j]->aVec).resize(5);
      for (int n = 0; n < 6; n++) {
        dummy[i]->mVec[j]->aVec[n] = new another_struct();
        (dummy[i]->mVec[j]->aVec[n])->c.resize(3);
        for (int m = 0; m < 4; m++) {
          (dummy[i]->mVec[j]->aVec[n]->c[m]) = new Point();
          (dummy[i]->mVec[j]->aVec[n]->c[m])->x = 100 * n;
          (dummy[i]->mVec[j]->aVec[n]->c[m])->y = 11000 * m;
          (dummy[i]->mVec[j]->aVec[n]->c[m])->dist = new double[2];
          (dummy[i]->mVec[j]->aVec[n]->c[m])->dist[0] =  11200.123;
          (dummy[i]->mVec[j]->aVec[n]->c[m])->dist[1] =  66503.131;
          printf("x: %d, y: %d, dist 0: %f, dist 1: %f \n", (dummy[i]->mVec[j]->aVec[n]->c[m])->x, (dummy[i]->mVec[j]->aVec[n]->c[m])->y, (dummy[i]->mVec[j]->aVec[n]->c[m])->dist[0], (dummy[i]->mVec[j]->aVec[n]->c[m])->dist[1]);
        }
      }
      printf("b: %f aname: %s \n", dummy[i]->mVec[j]->b, dummy[i]->mVec[j]->aname);
    }
  }

  if (NULL != dummy) {
    for(int i = 0; i < 4; i++)
    {
      free(dummy[i]);
    }
    free(dummy);
  }
  return 0;
}

The following dummy test code gives segmentation fault at the end of execution (to be more specific in main at return 0). I wondered the reason of this behavior. Would it be because it couldn't free the dummy variable? I'm using g++ 4.4 with no optimization flags for the tests.

#include <vector>
#include <boost/multi_array.hpp>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using std::vector;

typedef boost::multi_array<float, 1> DVec;

class Point{
  public:
    int x, y;
    double *dist;
    DVec dir;
};

struct another_struct {
  vector <Point *>c;
};

struct in_foo{
  vector <another_struct *>aVec;
  char *aname;
  float b;
};

struct foo {
  DVec b;
  vector<in_foo *> mVec;
};

int main(){

  DVec c(boost::extents[4]);
  foo **dummy = (foo **) calloc(4, sizeof(*dummy));
  vector <in_foo *>test_var(5);

  for(int i =0; i < 6; i++){
    test_var[i] = (in_foo *) malloc(sizeof(in_foo));
    memset(test_var[i], 0, sizeof(*test_var[i]));
    test_var[i]->aname = "42!\n";
    test_var[i]->b = (float) i;
  }

  for (int i = 0 ; i < 4; i++) {
    dummy[i] = (foo *) malloc(sizeof(*dummy[i]));
    (dummy[i]->b).resize(boost::extents[2]);
    (dummy[i]->mVec) = test_var;
  }

  for (int i = 0 ; i < 4; i++) {
    for(int j = 0; j < 5; j++){
      (dummy[i]->mVec[j]->aVec).resize(5);
      for (int n = 0; n < 6; n++) {
        dummy[i]->mVec[j]->aVec[n] = new another_struct();
        (dummy[i]->mVec[j]->aVec[n])->c.resize(3);
        for (int m = 0; m < 4; m++) {
          (dummy[i]->mVec[j]->aVec[n]->c[m]) = new Point();
          (dummy[i]->mVec[j]->aVec[n]->c[m])->x = 100 * n;
          (dummy[i]->mVec[j]->aVec[n]->c[m])->y = 11000 * m;
          (dummy[i]->mVec[j]->aVec[n]->c[m])->dist = new double[2];
          (dummy[i]->mVec[j]->aVec[n]->c[m])->dist[0] =  11200.123;
          (dummy[i]->mVec[j]->aVec[n]->c[m])->dist[1] =  66503.131;
          printf("x: %d, y: %d, dist 0: %f, dist 1: %f \n", (dummy[i]->mVec[j]->aVec[n]->c[m])->x, (dummy[i]->mVec[j]->aVec[n]->c[m])->y, (dummy[i]->mVec[j]->aVec[n]->c[m])->dist[0], (dummy[i]->mVec[j]->aVec[n]->c[m])->dist[1]);
        }
      }
      printf("b: %f aname: %s \n", dummy[i]->mVec[j]->b, dummy[i]->mVec[j]->aname);
    }
  }

  if (NULL != dummy) {
    for(int i = 0; i < 4; i++)
    {
      free(dummy[i]);
    }
    free(dummy);
  }
  return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里的微风 2024-12-15 07:21:37

您不能使用 malloccalloc 为非 POD 的类或结构分配内存,例如 vectorfooin_foo。一旦你这样做了,所有的赌注都消失了,你的程序显示的任何行为都是合理的。

new 与智能指针一起使用,或者更好的是,如果可能的话,使用组合。指针与 new 一起使用。

You can't use malloc or calloc to allocate memory for a class or struct that is non-POD, for example vector, foo, in_foo. Once you do that all bets are off and any behavior your program displays is within reason.

Use new with smart pointers or better yet use composition if possible.pointers with new.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文