An Access Violation Segmentation Fault

boschow's Avatar, Join Date: Feb 2007
Go4Expert Member
Hello,
i everyone, i made some code to simulate the working of a shade motor and but it dosent work because an error appear which states An Access Violation Segmentation Fault raised in your program, i dont know what to do, perhaps you guys can figure that out.

Code:
#include <iostream>
using namespace std;

struct NVAR_DigitalOutputOnOff
{
        bool wm_bc;
        bool do_bc;
        bool do_bs; 
};

struct VAR_DigitalOutputOnOff
{
        bool dom_bs;
        bool doa_bs;          
};
struct NVAR_DigitalInput
{
        bool di_bs;
};

struct NVAR_ShadeVar
{
        int shadeVar;
        int shadeTime;
        int shadeRef;
        int shadeOff;
        int temp_time;
        char flag_shade;
};

struct MainModule
{
    NVAR_DigitalOutputOnOff         digOutN[2];
    VAR_DigitalOutputOnOff          digOutV[2];
    NVAR_DigitalInput               digInpN[2];
    NVAR_ShadeVar                   m01_var[1];   
};

 void shadeM01(
               NVAR_DigitalOutputOnOff *digOut1N, 
               NVAR_DigitalOutputOnOff *digOut2N,
               VAR_DigitalOutputOnOff *digOut1V, 
               VAR_DigitalOutputOnOff *digOut2V,
               NVAR_DigitalInput *digInp3, 
               NVAR_DigitalInput *digInp4, 
               NVAR_ShadeVar *shadeVar, 
               int shade_time) 
 {  
 																						 
  if (digOut1N->wm_bc  // Q1 MANUAL
      && digOut2N->wm_bc) // Q2 MANUAL
      { 
        if (shadeVar->flag_shade == 0) // ZALUZIJE MIRUJEJO
        {
          if ((digInp3->di_bs
              ^ digOut1N->do_bc)
              && !digInp4->di_bs
              && !digOut2N->do_bc) // KOMANDA TOUCHA SPUSCANJA NI AKTIVNA 
          { 
              shadeVar->flag_shade = 1;
              // digOut2->dom_bs = 0;
          } else digOut1V->dom_bs = 0; 
                        	    
          if ((digInp4->di_bs
          		^digOut2N->do_bc)
            && !digInp3->di_bs
            && !digOut1N->do_bc)
          { 
              shadeVar->flag_shade = 2;
             // digOut1->dom_bs = 0;
          } else digOut2V->dom_bs = 0;
        }               	    
        else if (shadeVar->flag_shade == 1) // ODPIRANJE ZALUZIJ
        { 
          if (shadeVar->shadeTime < shade_time)
          {
            if (digInp3->di_bs
              ^digOut1N->do_bc)
            {
              if (shadeVar->temp_time < 833)
              {
                shadeVar->temp_time++;
                digOut1V->dom_bs = 1;
              } 
              else if (shadeVar->temp_time == 833)
              {
                shadeVar->temp_time = 0;
                shadeVar->shadeTime++;
              }
            } 
            else 
            {
              if (shadeVar->temp_time < 833)
              {
                shadeVar->temp_time++;
                digOut1V->dom_bs = 1;
              } 
              else if (shadeVar->temp_time == 833)
              {
                digOut1V->dom_bs = 0;
                  if (shadeVar->shadeOff < 255)
                  {
                    shadeVar->shadeOff++;
                  } 
                  else if (shadeVar->shadeOff == 255)
                  {
                    shadeVar->shadeTime++;
                    shadeVar->flag_shade = 0;
                    shadeVar->shadeOff = 0;
                    shadeVar->temp_time = 0;
                  }
              }
            }
          }
          else 
          { // ZALUZIJE ODPRTE
            digOut1V->dom_bs = 0;
            digOut1N->do_bc = 0;
            shadeVar->shadeTime = shade_time;
            shadeVar->flag_shade = 0;
          }		
        }
        else if (shadeVar->flag_shade == 2) // ZAPIRANJE ZALUZIJ 
        { 
          if (shade_time > 0)
          {
            if (digInp4->di_bs
                ^digOut2N->do_bc)
            {
              if (shadeVar->temp_time < 833) 
              {
                shadeVar->temp_time++;
                digOut2V->dom_bs = 1;
              } 
              else if (shadeVar->temp_time == 833)
              {
                shadeVar->temp_time = 0;
                shadeVar->shadeTime--;
              }
            } 
            else 
            {
              if (shadeVar->temp_time < 833)
              {
                shadeVar->temp_time++;
                digOut2V->dom_bs = 1;
              } 
              else if (shadeVar->temp_time == 833)
              {
                digOut2V->dom_bs = 0;
                  if (shadeVar->shadeOff < 255)
                  {
                    shadeVar->shadeOff++;
                  } 
                  else if (shadeVar->shadeOff == 255)
                  {
                    shadeVar->shadeTime--;
                    shadeVar->flag_shade = 0;
                    shadeVar->shadeOff = 0;
                    shadeVar->temp_time = 0;
                  }
              }
            }
          }
          else 
          { // ZALUZIJE ZAPRTE
            digOut2V->dom_bs = 0;
            digOut2N->do_bc = 0;
            shadeVar->shadeTime = 0;
            shadeVar->flag_shade = 0;
          }		
        }
        //vpisi8(nvvar,shadeRef, shadeVar->shade_time);                       	 
      }
  }

//MainModule *MM;

int main()
{   
    int shade_time = 0;
    cout << "Digital output 1: " << endl;
    cout << "insert 1 - manual mode" << endl; 
    cout << "insert 0 - auto mode" << endl;  
    cin >> MM->digOutN[0].wm_bc;
    
    cout << "Digital output 2: " << endl;
    cout << "insert 1 - manual mode" << endl; 
    cout << "insert 0 - auto mode" << endl; 
    cin >> MM->digOutN[1].wm_bc;
      
    shadeM01(&MM->digOutN[0],  //NVAR_DigitalOutputOnOff *digOut1N 
             &MM->digOutN[1],  //NVAR_DigitalOutputOnOff *digOut2N
             &MM->digOutV[0],  //VAR_DigitalOutputOnOff *digOut1V
             &MM->digOutV[1],  //VAR_DigitalOutputOnOff *digOut2V
             &MM->digInpN[0],  //NVAR_DigitalInput *digInp3
             &MM->digInpN[1],  //NVAR_DigitalInput *digInp4
             &MM->m01_var[0],  //NVAR_ShadeVar *shadeVar
             shade_time);      //int shade_time
    
    cout << "State of first output" << MM->digOutN[0].do_bs << endl;
    cout << "State of second output" << MM->digOutN[1].do_bs << endl;
    
    return 0;
}
Thanks for your time and help.
Best Regards,
Boschow.
Salem's Avatar, Join Date: Nov 2007
Ambitious contributor
Aside from the fact that it doesn't compile since you commented out
//MainModule *MM;

You also seem to have removed (or it was never there to begin with) the statement which assigns MM to point to some real memory. As an uninitialised global, it will just be NULL and that'll get you a seg fault every time.
boschow's Avatar, Join Date: Feb 2007
Go4Expert Member
Thanks for the advice, i come up with some solutions, which of this you think is the best choice ?

1.solution
Code:
 MainModule MM;   // this is the object
 MainModule *pMM; // This is a pointer to a struct of type MainModule
2.solution
Code:
MM = new MainModule();
Best regards,
BoSCHoW.