C++ 中奇怪的指针问题
我遇到了一个非常令人沮丧的指针问题。我之前在这里发布过: 中处理深度嵌套指针
艰难:在 C ++ 太长并且已经过时,所以我选择重新发布更多细节。
这是定义我的类型的头文件:
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#define USE_3D_GEOM
//#define USE_2D GEOM
#define DEBUG
#ifdef USE_3D_GEOM
#define DIMENSIONS 3
#elif USE_2D_GEOM
#define DIMENSIONS 2
#else
#define DIMENSIONS 1
#endif
#ifndef _COMMON_H
#define _COMMON_H
template<class T>
inline T from_string(const std::string& s)
{
std::istringstream stream (s);
T t;
stream >> t;
return t;
};
template <class T>
inline std::string to_string (const T& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}
enum e_ensemble_kind
{
MICROCANONICAL,
CANONICAL,
NVT,
GRAND_CANONICAL,
NPT,
NVE
};
enum e_potential_kind
{
HARD_SPHERE,
SQUARE_WELL,
LENNARD_JONES
};
enum e_file_types
{
MC_SIMPLE,
NAMD,
GROMACS,
CHARMM
};
#ifdef USE_3D_GEOM
typedef struct s_coordinates t_coordinates;
#endif
#ifdef USE_2D_GEOM
typedef struct s_coordinates t_coordinates;
#endif
typedef struct s_particle t_particle;
typedef struct s_bond t_bond;
typedef struct s_angle t_angle;
typedef struct s_dihedral t_dihedral;
typedef struct s_molecule t_molecule;
typedef struct s_lj_param t_lj_param;
typedef struct s_bond_param t_bond_param;
typedef struct s_angle_param t_angle_param;
typedef struct s_dih_param t_dih_param;
typedef struct s_lookup_tab t_lookup_tab;
#ifdef USE_3D_GEOM
struct s_coordinates
{
double x;
double y;
double z;
s_coordinates& operator+(const s_coordinates &to_add)
{
x += to_add.x;
y += to_add.y;
z += to_add.z;
return *this;
}
s_coordinates& operator-(const s_coordinates &to_subtract)
{
x -= to_subtract.x;
y -= to_subtract.y;
z -= to_subtract.z;
return *this;
}
s_coordinates& operator=(const s_coordinates &to_assign)
{
x = to_assign.x;
y = to_assign.y;
z = to_assign.z;
return *this;
}
bool operator==(const s_coordinates &to_assign)
{
return x == to_assign.x && y == to_assign.y && z == to_assign.z;
}
};
#endif
#ifdef USE_2D_GEOM
struct s_coordinates
{
double x;
double y;
s_coordinates& operator+(const s_coordinates &to_add)
{
x += to_add.x;
y += to_add.y;
return *this;
}
s_coordinates& operator-(const s_coordinates &to_subtract)
{
x -= to_subtract.x;
y -= to_subtract.y;
return *this;
}
s_coordinates& operator=(const s_coordinates &to_assign)
{
x = to_assign.x;
y = to_assign.y;
return *this;
}
bool operator==(const s_coordinates &to_assign)
{
return x == to_assign.x && y == to_assign.y;
}
};
#endif
typedef struct s_particle
{
t_coordinates position;
double charge;
double mass;
std::string name;
std::vector<t_lj_param>::iterator my_particle_kind_iter;
s_particle& operator=(const s_particle &to_assign)
{
position = to_assign.position;
charge = to_assign.charge;
mass = to_assign.mass;
name = to_assign.name;
my_particle_kind_iter = to_assign.my_particle_kind_iter;
return *this;
}
} t_particle;
struct s_bond
{
t_particle * particle_1;
t_particle * particle_2;
std::vector<t_bond_param>::iterator my_bond_kind_iter;
s_bond& operator=(const s_bond &to_assign)
{
particle_1 = to_assign.particle_1;
particle_2 = to_assign.particle_2;
my_bond_kind_iter = to_assign.my_bond_kind_iter;
return *this;
}
};
struct s_angle
{
t_particle * particle_1;
t_particle * particle_2;
t_particle * particle_3;
std::vector<t_angle_param>::iterator my_angle_kind_iter;
s_angle& operator=(const s_angle &to_assign)
{
particle_1 = to_assign.particle_1;
particle_2 = to_assign.particle_2;
particle_3 = to_assign.particle_3;
my_angle_kind_iter = to_assign.my_angle_kind_iter;
return *this;
}
};
struct s_dihedral
{
t_particle * particle_1;
t_particle * particle_2;
t_particle * particle_3;
t_particle * particle_4;
std::vector<t_dih_param>::iterator my_dih_kind_iter;
s_dihedral& operator=(const s_dihedral &to_assign)
{
particle_1 = to_assign.particle_1;
particle_2 = to_assign.particle_2;
particle_3 = to_assign.particle_3;
particle_4 = to_assign.particle_4;
my_dih_kind_iter = to_assign.my_dih_kind_iter;
return *this;
}
};
struct s_molecule
{
std::string res_name;
std::vector<t_particle> my_particles;
std::vector<t_bond> my_bonds;
std::vector<t_angle> my_angles;
std::vector<t_dihedral> my_dihedrals;
s_molecule& operator=(const s_molecule &to_assign)
{
res_name = to_assign.res_name;
my_particles = to_assign.my_particles;
my_bonds = to_assign.my_bonds;
my_angles = to_assign.my_angles;
my_dihedrals = to_assign.my_dihedrals;
return *this;
}
};
struct s_lj_param
{
double epsilon;
double sigma;
std::string atom_kind_name;
};
struct s_bond_param
{
std::string atom_1;
std::string atom_2;
double bond_coeff;
double default_length;
};
struct s_angle_param
{
std::string atom_1;
std::string atom_2;
std::string atom_3;
double angle_coeff;
double default_angle;
};
struct s_dih_param
{
std::string atom_1;
std::string atom_2;
std::string atom_3;
std::string atom_4;
std::vector<double> dih_coeff;
std::vector<unsigned int> n;
std::vector<double> delta;
};
struct s_lookup_tab {
std::string name;
int code;
};
#endif /*_COMMON_H*/
这是我进行的调用,用于将 t_molecule 类型的 var 添加到分子数组中(请参阅上面的头文件以了解 t_molecule 的定义)。
void Molecule_Manager_Main::add_molecule(const t_molecule new_molecule)
{
std::cout << "TYPE :" << new_molecule.res_name << std::endl;
std::cout << "3: BOND PARTICLE 1 : "
<< new_molecule.my_bonds[new_molecule.my_bonds.size()-1].particle_1->name
<< std::endl;
std::cout << "3: BOND PARTICLE 2 : "
<< new_molecule.my_bonds[new_molecule.my_bonds.size()-1].particle_2->name
<< std::endl;
std::cout << "3: BOND ITER CONST : "
<< new_molecule.my_bonds[new_molecule.my_bonds.size()-1].my_bond_kind_iter->bond_coeff
<< " "
<< new_molecule.my_bonds[new_molecule.my_bonds.size()-1].my_bond_kind_iter->default_length
<< std::endl;
my_molecules.push_back(new_molecule);
std::cout << "99: INDEX : " << my_molecules.size()-1 << std::endl;
std::cout << "TYPE :" << my_molecules[my_molecules.size()-1].res_name << std::endl;
std::cout << "4: BOND PARTICLE 1 : "
<< my_molecules[my_molecules.size()-1].my_bonds[my_molecules[my_molecules.size()-1].my_bonds.size()-1].particle_1->name
<< std::endl;
std::cout << "4: BOND PARTICLE 2 : "
<< my_molecules[my_molecules.size()-1].my_bonds[my_molecules[my_molecules.size()-1].my_bonds.size()-1].particle_2->name
<< std::endl;
std::cout << "4: BOND ITER CONST : "
<< my_molecules[my_molecules.size()-1].my_bonds[my_molecules[my_molecules.size()-1].my_bonds.size()-1].my_bond_kind_iter->bond_coeff
<< " "
<< my_molecules[my_molecules.size()-1].my_bonds[my_molecules[my_molecules.size()-1].my_bonds.size()-1].my_bond_kind_iter->default_length
<< std::endl;
add_performed = true;
}
这完美地工作了......重命名字符串打印,并且键矢量打印中最后一个键的信息。然后一旦我添加了所有分子。我称之为:
t_molecule * Molecule_Manager_Main::get_molecule(unsigned int index)
{
std::cout << "TYPE :" << my_molecules[index].res_name << std::endl;
std::cout << "5: BOND PARTICLE 1 : "
<< my_molecules[index].my_bonds[my_molecules[index].my_bonds.size()-1].particle_1->name
<< std::endl;
std::cout << "5: BOND PARTICLE 2 : "
<< my_molecules[index].my_bonds[my_molecules[index].my_bonds.size()-1].particle_2->name
<< std::endl;
std::cout << "5: BOND ITER CONST : "
<< my_molecules[index].my_bonds[my_molecules[index].my_bonds.size()-1].my_bond_kind_iter->bond_coeff
<< " "
<< my_molecules[index].my_bonds[my_molecules[index].my_bonds.size()-1].my_bond_kind_iter->default_length
<< std::endl;
return &(my_molecules[index]);
}
债券线上的段错误。
我可以从添加步骤中打印的索引看出,我没有覆盖我推到向量上的分子(大小正在增长)。
换句话说,似乎正在发生的事情是: 读取子向量(作品)->在父向量中添加更多项目 ->重读子向量(seg-faults)
这些函数是分子变量添加到向量的唯一方法,并且分子变量仅添加一次并且在我当前的测试中不会在死后修改。
有什么想法吗????先感谢您!!
I'm running into a VERY frustrating pointer issue. I previously posted here:
TOUGH: Dealing with deeply nested pointers in C++
But that post got overly long and is stale, so I chose to repost with more details.
Here is my header file that defines my types:
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#define USE_3D_GEOM
//#define USE_2D GEOM
#define DEBUG
#ifdef USE_3D_GEOM
#define DIMENSIONS 3
#elif USE_2D_GEOM
#define DIMENSIONS 2
#else
#define DIMENSIONS 1
#endif
#ifndef _COMMON_H
#define _COMMON_H
template<class T>
inline T from_string(const std::string& s)
{
std::istringstream stream (s);
T t;
stream >> t;
return t;
};
template <class T>
inline std::string to_string (const T& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}
enum e_ensemble_kind
{
MICROCANONICAL,
CANONICAL,
NVT,
GRAND_CANONICAL,
NPT,
NVE
};
enum e_potential_kind
{
HARD_SPHERE,
SQUARE_WELL,
LENNARD_JONES
};
enum e_file_types
{
MC_SIMPLE,
NAMD,
GROMACS,
CHARMM
};
#ifdef USE_3D_GEOM
typedef struct s_coordinates t_coordinates;
#endif
#ifdef USE_2D_GEOM
typedef struct s_coordinates t_coordinates;
#endif
typedef struct s_particle t_particle;
typedef struct s_bond t_bond;
typedef struct s_angle t_angle;
typedef struct s_dihedral t_dihedral;
typedef struct s_molecule t_molecule;
typedef struct s_lj_param t_lj_param;
typedef struct s_bond_param t_bond_param;
typedef struct s_angle_param t_angle_param;
typedef struct s_dih_param t_dih_param;
typedef struct s_lookup_tab t_lookup_tab;
#ifdef USE_3D_GEOM
struct s_coordinates
{
double x;
double y;
double z;
s_coordinates& operator+(const s_coordinates &to_add)
{
x += to_add.x;
y += to_add.y;
z += to_add.z;
return *this;
}
s_coordinates& operator-(const s_coordinates &to_subtract)
{
x -= to_subtract.x;
y -= to_subtract.y;
z -= to_subtract.z;
return *this;
}
s_coordinates& operator=(const s_coordinates &to_assign)
{
x = to_assign.x;
y = to_assign.y;
z = to_assign.z;
return *this;
}
bool operator==(const s_coordinates &to_assign)
{
return x == to_assign.x && y == to_assign.y && z == to_assign.z;
}
};
#endif
#ifdef USE_2D_GEOM
struct s_coordinates
{
double x;
double y;
s_coordinates& operator+(const s_coordinates &to_add)
{
x += to_add.x;
y += to_add.y;
return *this;
}
s_coordinates& operator-(const s_coordinates &to_subtract)
{
x -= to_subtract.x;
y -= to_subtract.y;
return *this;
}
s_coordinates& operator=(const s_coordinates &to_assign)
{
x = to_assign.x;
y = to_assign.y;
return *this;
}
bool operator==(const s_coordinates &to_assign)
{
return x == to_assign.x && y == to_assign.y;
}
};
#endif
typedef struct s_particle
{
t_coordinates position;
double charge;
double mass;
std::string name;
std::vector<t_lj_param>::iterator my_particle_kind_iter;
s_particle& operator=(const s_particle &to_assign)
{
position = to_assign.position;
charge = to_assign.charge;
mass = to_assign.mass;
name = to_assign.name;
my_particle_kind_iter = to_assign.my_particle_kind_iter;
return *this;
}
} t_particle;
struct s_bond
{
t_particle * particle_1;
t_particle * particle_2;
std::vector<t_bond_param>::iterator my_bond_kind_iter;
s_bond& operator=(const s_bond &to_assign)
{
particle_1 = to_assign.particle_1;
particle_2 = to_assign.particle_2;
my_bond_kind_iter = to_assign.my_bond_kind_iter;
return *this;
}
};
struct s_angle
{
t_particle * particle_1;
t_particle * particle_2;
t_particle * particle_3;
std::vector<t_angle_param>::iterator my_angle_kind_iter;
s_angle& operator=(const s_angle &to_assign)
{
particle_1 = to_assign.particle_1;
particle_2 = to_assign.particle_2;
particle_3 = to_assign.particle_3;
my_angle_kind_iter = to_assign.my_angle_kind_iter;
return *this;
}
};
struct s_dihedral
{
t_particle * particle_1;
t_particle * particle_2;
t_particle * particle_3;
t_particle * particle_4;
std::vector<t_dih_param>::iterator my_dih_kind_iter;
s_dihedral& operator=(const s_dihedral &to_assign)
{
particle_1 = to_assign.particle_1;
particle_2 = to_assign.particle_2;
particle_3 = to_assign.particle_3;
particle_4 = to_assign.particle_4;
my_dih_kind_iter = to_assign.my_dih_kind_iter;
return *this;
}
};
struct s_molecule
{
std::string res_name;
std::vector<t_particle> my_particles;
std::vector<t_bond> my_bonds;
std::vector<t_angle> my_angles;
std::vector<t_dihedral> my_dihedrals;
s_molecule& operator=(const s_molecule &to_assign)
{
res_name = to_assign.res_name;
my_particles = to_assign.my_particles;
my_bonds = to_assign.my_bonds;
my_angles = to_assign.my_angles;
my_dihedrals = to_assign.my_dihedrals;
return *this;
}
};
struct s_lj_param
{
double epsilon;
double sigma;
std::string atom_kind_name;
};
struct s_bond_param
{
std::string atom_1;
std::string atom_2;
double bond_coeff;
double default_length;
};
struct s_angle_param
{
std::string atom_1;
std::string atom_2;
std::string atom_3;
double angle_coeff;
double default_angle;
};
struct s_dih_param
{
std::string atom_1;
std::string atom_2;
std::string atom_3;
std::string atom_4;
std::vector<double> dih_coeff;
std::vector<unsigned int> n;
std::vector<double> delta;
};
struct s_lookup_tab {
std::string name;
int code;
};
#endif /*_COMMON_H*/
And here is a call that I make to add a var of type t_molecule (see above header for t_molecule's definition) to an array of molecules.
void Molecule_Manager_Main::add_molecule(const t_molecule new_molecule)
{
std::cout << "TYPE :" << new_molecule.res_name << std::endl;
std::cout << "3: BOND PARTICLE 1 : "
<< new_molecule.my_bonds[new_molecule.my_bonds.size()-1].particle_1->name
<< std::endl;
std::cout << "3: BOND PARTICLE 2 : "
<< new_molecule.my_bonds[new_molecule.my_bonds.size()-1].particle_2->name
<< std::endl;
std::cout << "3: BOND ITER CONST : "
<< new_molecule.my_bonds[new_molecule.my_bonds.size()-1].my_bond_kind_iter->bond_coeff
<< " "
<< new_molecule.my_bonds[new_molecule.my_bonds.size()-1].my_bond_kind_iter->default_length
<< std::endl;
my_molecules.push_back(new_molecule);
std::cout << "99: INDEX : " << my_molecules.size()-1 << std::endl;
std::cout << "TYPE :" << my_molecules[my_molecules.size()-1].res_name << std::endl;
std::cout << "4: BOND PARTICLE 1 : "
<< my_molecules[my_molecules.size()-1].my_bonds[my_molecules[my_molecules.size()-1].my_bonds.size()-1].particle_1->name
<< std::endl;
std::cout << "4: BOND PARTICLE 2 : "
<< my_molecules[my_molecules.size()-1].my_bonds[my_molecules[my_molecules.size()-1].my_bonds.size()-1].particle_2->name
<< std::endl;
std::cout << "4: BOND ITER CONST : "
<< my_molecules[my_molecules.size()-1].my_bonds[my_molecules[my_molecules.size()-1].my_bonds.size()-1].my_bond_kind_iter->bond_coeff
<< " "
<< my_molecules[my_molecules.size()-1].my_bonds[my_molecules[my_molecules.size()-1].my_bonds.size()-1].my_bond_kind_iter->default_length
<< std::endl;
add_performed = true;
}
This works perfectly... the resname string prints, and the info on the last bond in the bonds vector prints. Then once I've added all my molecules. I call this:
t_molecule * Molecule_Manager_Main::get_molecule(unsigned int index)
{
std::cout << "TYPE :" << my_molecules[index].res_name << std::endl;
std::cout << "5: BOND PARTICLE 1 : "
<< my_molecules[index].my_bonds[my_molecules[index].my_bonds.size()-1].particle_1->name
<< std::endl;
std::cout << "5: BOND PARTICLE 2 : "
<< my_molecules[index].my_bonds[my_molecules[index].my_bonds.size()-1].particle_2->name
<< std::endl;
std::cout << "5: BOND ITER CONST : "
<< my_molecules[index].my_bonds[my_molecules[index].my_bonds.size()-1].my_bond_kind_iter->bond_coeff
<< " "
<< my_molecules[index].my_bonds[my_molecules[index].my_bonds.size()-1].my_bond_kind_iter->default_length
<< std::endl;
return &(my_molecules[index]);
}
This segfaults on the bonds line.
I can tell from the indices I print in the add step that I'm not overwriting the molecules I'm pushing onto the vector (the size is growing)..
In other words what appears to be happening is:
Read sub-vector (works) -> add some more items in parent vector -> reread sub-vector (seg-faults)
These functions are the ONLY means for molecules vars to be added to the vector, and molecules vars are only added once and not modified posthumously in my current test.
Any ideas???? Thank you in advance!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
刚读到您访问了一个名为 my_bond_kind_iter 的变量。在父向量中添加更多项目后,它将调整大小。这意味着(假设您没有 C++0x 右值感知容器)子向量也将被复制,从而使所有现有指针和引用无效。因此,当您尝试访问这个现在完全无效的旧迭代器时,您好分段错误。如果您向子向量添加更多内容,这当然也会发生。
向量迭代器并不安全,你不能保留它们并稍后访问它们,因为向量调整大小意味着移动内存,而这是在实现时突发奇想发生的。
Just reading that you access a varaible called my_bond_kind_iter. After you add some more items in the parent vector, it will resize. This means (assuming that you don't have C++0x rvalue-aware containers) that the child vector will also be copied, invalidating all existing pointers and references into it. So when you try to access this old iterator that is now thoroughly invalid, hello segmentation fault. This will of course also happen if you add more into the child vector.
Vector iterators are not safe, you cannot keep them around and access them later, because vectors resize which means moving memory, and this happens at the whim of the implementation.
在不同的对象中,您存储迭代器来访问某些向量中的元素。当底层向量被修改时,例如添加新元素,这些迭代器就会失效。取消引用此类迭代器是未定义的行为。
当您添加新分子时,可能会修改已存储迭代器的向量,并且稍后使用这些迭代器会导致分段错误。
In the different objects you store iterators to access the elements in some vectors. These iterators get invalidated when the underlying vector is modified, for example by adding new elements. Dereferencing such an iterator is undefined behavior.
Probably you modify the vectors for which you have stored iterators when you add new molecules and using these iterators later on leads to the segmentation faults.
我会是你,我会大量重构这段代码。
大多数时候,当我遇到像你这样的问题时(而且这种情况变得非常罕见),我会重构代码,直到清楚地看到问题为止。这里有太多明显可以避免的重复。使用 typedef、引用、const 来避免重复。
重构可以让你重新组织你的代码和你的思想,简化,让问题变得显而易见。花点时间这样做,你就会找到问题的根源。
这可能超出了这段代码。
(关于重构,我建议阅读以下内容: http://sourcemaking.com/refactoring )
I would be you, I would heavily refactor this code.
Most of the time when I get a problem like yours (and it's becoming very rare) I refactor the code until I see clearly the problem. Here, there is too much repetitions that can clearly be avoided. Use typedefs, references, consts to avoid repetitions.
Refactoring allow you to reorganise your code and your thought, simplify, make problems obvious. Take time to do this and you'll find the source of the problem.
That might be out of this code.
(about refactoring, i recommend reading this : http://sourcemaking.com/refactoring )
我建议在编写跟踪代码(但不限于跟踪代码)时避免下标运算符(例如 my_molecules[index]),更喜欢 at() 成员函数。
I will recommend to avoid subscript operator (p. ex. my_molecules[index]) while writing trace code (but not restrictive to trace code), prefer at() member function.