级联结构指针测试代码中的分段错误
以下虚拟测试代码在执行结束时给出分段错误(更具体地说,在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能使用
malloc
或calloc
为非 POD 的类或结构分配内存,例如vector
、foo
,in_foo
。一旦你这样做了,所有的赌注都消失了,你的程序显示的任何行为都是合理的。将
new
与智能指针一起使用,或者更好的是,如果可能的话,使用组合。指针与new
一起使用。You can't use
malloc
orcalloc
to allocate memory for a class or struct that is non-POD, for examplevector
,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 withnew
.