How is this an "Access Violation"

Ruby The Roobster michaeleverestc79 at gmail.com
Tue Oct 27 02:05:37 UTC 2020


Following function when called throws an access violation. I 
think it has to do with the assert statements, but I don't know 
why.

        void construct(string type,atom base,atom bonded)
         {
         base = new 
atom(base.name.idup,base.mass,base.electro_negativity,base.valence_electrons,base.electrons,base.protons,base.neutrons,base.pos);
         bonded = new 
atom(bonded.name.idup,bonded.mass,bonded.electro_negativity,bonded.valence_electrons,bonded.electrons,bonded.protons,bonded.neutrons,bonded.pos);
         if(type == "single")
         {
             assert(this.base.valence >= 1 && this.bonded.valence 
 >=1 && this.base.electro_negativity >= 0 && 
this.bonded.electro_negativity >= 0,"For a single bond, both 
atoms need to have at least one free electron and have to have 
electro negativity.");
             this.type = "single".dup;
         }
         else if(type == "double")
         {
             assert(this.base.valence >= 2 && this.bonded.valence 
 >=2 && this.base.electro_negativity >= 0 && 
this.bonded.electro_negativity >= 0,"For a double bond, both 
atoms need to have at least one free electron and have to have 
electro negativity.");
             this.type = "double".dup;
         }
         else if(type == "triple")
         {
             assert(this.base.valence >= 3 && this.bonded.valence 
 >=3 && this.base.electro_negativity >= 0 && 
this.bonded.electro_negativity >= 0,"For a triple bond, both 
atoms need to have at least one free electron and have to have 
electro negativity.");
             this.type = "triple".dup;
         }
         else if(type == "ionic")
         {
             bool this_electro_negativity_greater;
             if((this.base.electro_negativity > 
this.bonded.electro_negativity))
                 this_electro_negativity_greater = true;
             else if((this.base.electro_negativity < 
this.bonded.electro_negativity))
                 this_electro_negativity_greater = false;
             if(this_electro_negativity_greater)
             {
                 assert(((this.base._valence_electrons == 
this.base.valence) && (this.bonded._valence_electrons == 
this.bonded.valence)),"Atoms in an ionic bond can't already be 
ions.");
                 int fullValence;
                 if(this.base.electrons < 2)
                     fullValence = 2;
                 else
                     fullValence = 8;
                 assert((fullValence - 
this.base.valence_electrons) == 
(this.bonded.valence_electrons),"The amount valence electrons in 
the atom with less valence electrons must be the same as the 
value of (8 - (the amount of valence electrons in the atom with 
more valence electrons))");
             }
             else
             {
                 assert(((this.base._valence_electrons == 
this.base.valence) && (this.bonded._valence_electrons == 
this.bonded.valence)),"Atoms in an ionic bond can't already be 
ions.");
                 int thatFullValence;
                 if(this.bonded.electrons < 2)
                     thatFullValence = 2;
                 else
                     thatFullValence = 8;
                 assert((thatFullValence - 
this.bonded.valence_electrons) == 
(this.base.valence_electrons),"The amount valence electrons in 
the atom with less valence electrons must be the same as the 
value of (8 - (the amount of valence electrons in the atom with 
more valence electrons))");
             }
         }
         }


More information about the Digitalmars-d-learn mailing list