Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1037789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:53:36+00:00 2026-05-16T14:53:36+00:00

I define this structure: struct s_molecule { std::string res_name; std::vector<t_particle> my_particles; std::vector<t_bond> my_bonds; std::vector<t_angle>

  • 0

I define this structure:

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;
  }
};

and these structures:

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;
  }
};

and then in my code I return a pointer to an s_molecule (typedef’d to t_molecule, but still).

Using this pointer I can get this code to work:

for  (unsigned int i = 0;
      i < current_molecule->my_particles.size();
      i++)
    {
      std::cout << "Particle " 
        << current_molecule->my_particles[i].name << std::endl
            << "Charge: " 
        << current_molecule->my_particles[i].charge << std::endl
        << "Mass: " 
        << current_molecule->my_particles[i].mass << std::endl
        << "Particle Kind Name: " 
        << (*current_molecule->my_particles[i].my_particle_kind_iter).atom_kind_name 
        << std::endl
        << "x: " << current_molecule->my_particles[i].position.x 
        << " y: " << current_molecule->my_particles[i].position.y
    #ifdef USE_3D_GEOM
        << "z: " << current_molecule->my_particles[i].position.z
    #endif
        << std::endl;
    }

If I replace it with:

for  (std::vector<t_particle>::iterator it = current_molecule->my_particles.begin();
      it !=current_molecule->my_particles.end();
      it++)
    {
      std::cout << "Particle " 
        << (*it).name << std::endl
            << "Charge: " 
        << (*it).charge << std::endl
        << "Mass: " 
        << (*it).mass << std::endl
        << "Particle Kind Name: " 
        << (*(*it).my_particle_kind_iter).atom_kind_name 
        << std::endl
        << "x: " << (*it).position.x 
        << " y: " << (*it).position.y
    #ifdef USE_3D_GEOM
        << "z: " << (*it).position.z
    #endif
        << std::endl;
    }

I now get nasty segfaults…

Not to put too much here, but I’m also getting segfaults when I tried to do this:

std::cout << "Bond ATOMS : " 
          << (*current_molecule).my_bonds[0].particle_1->name
          << std::endl

Again, current_molecule is a pointer to a s_molecule structure, which contains arrays of structures, which in turn either directly have vars or are pointers. I can’t get these multiple layers of indirection to work. Suggestions on fixing these segfaults.

FYI I’m compiling on Linux Centos 5.4 with g++ and using a custom makefile system.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:53:37+00:00Added an answer on May 16, 2026 at 2:53 pm

    Again, this issue was answered here:
    Weird Pointer issue in C++
    by DeadMG. Sorry for the double post.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm having trouble trying to define this function-like macro, which takes 4 vector magnitudes
I'm using a struct like this: define struct _Fragment{ int a; char *seq; }Fragment;
I have this complicated structure thingie: #include <stdlib.h> typedef struct { int x; int
I have a structure defined in IDL. This structure has following members: { BSTR
When I define this function, template<class A> set<A> test(const set<A>& input) { return input;
In C++, if you define this function in header.hpp void incAndShow() { static int
I have tried this: #define format(f, ...) \ int size = strlen(f) + (sizeof((int[]){__VA_ARGS__})/sizeof(int))
I occasionally write code something like this: // file1.cpp #define DO_THIS 1 #if DO_THIS
I have some Objective-C code that looks like this: #define myVar 10 float f
I have a library that has several options defined as this: #define shouldShowToolbar YES

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.