www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How is this an "Access Violation"

reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
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))"); } } }
Oct 26 2020
next sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Tuesday, 27 October 2020 at 02:05:37 UTC, Ruby The Roobster 
wrote:
 Following function when called throws an access violation. I 
 think it has to do with the assert statements, but I don't know 
 why.

 [...]
OT, but why do you have a function that immediately replaces what you passed?
Oct 27 2020
prev sibling next sibling parent Rene Zwanenburg <renezwanenburg gmail.com> writes:
On Tuesday, 27 October 2020 at 02:05:37 UTC, Ruby The Roobster 
wrote:
        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);
         (...)
             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.");
Going by the assert this is a member function of a class or struct with base and bonded members. I think you meant to assign the newly created atoms to those fields instead of overwriting your arguments.
Oct 27 2020
prev sibling parent reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
On Tuesday, 27 October 2020 at 02:05:37 UTC, Ruby The Roobster 
wrote:
 Following function when called throws an access violation. I 
 think it has to do with the assert statements, but I don't know 
 why.
[...]
Okay. I messed up here. It should be: this.base = new atom(...) . I corrected that. The assert statements still don't work though.
Oct 27 2020
parent reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
On Wednesday, 28 October 2020 at 00:48:36 UTC, Ruby The Roobster 
wrote:
 On Tuesday, 27 October 2020 at 02:05:37 UTC, Ruby The Roobster 
 wrote:
 Following function when called throws an access violation. I 
 think it has to do with the assert statements, but I don't 
 know why.
[...]
Okay. I messed up here. It should be: this.base = new atom(...) . I corrected that. The assert statements still don't work though.
Turns out all attempts to access member variables of that class fail: atom a = new atom(...); a.valence = 5; Output: Object.Error (0): Access Violation
Oct 28 2020
parent Ruby The Roobster <michaeleverestc79 gmail.com> writes:
On Wednesday, 28 October 2020 at 12:33:18 UTC, Ruby The Roobster 
wrote:
 On Wednesday, 28 October 2020 at 00:48:36 UTC, Ruby The 
 Roobster wrote:
 On Tuesday, 27 October 2020 at 02:05:37 UTC, Ruby The Roobster 
 wrote:
 Following function when called throws an access violation. I 
 think it has to do with the assert statements, but I don't 
 know why.
[...]
Okay. I messed up here. It should be: this.base = new atom(...) . I corrected that. The assert statements still don't work though.
Turns out all attempts to access member variables of that class fail: atom a = new atom(...); a.valence = 5; Output: Object.Error (0): Access Violation
Nevermind. I should just have done my research and paid more attention.
Oct 28 2020