GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 31-Jan-2008, 08:27
boschow boschow is offline
Junior Member
 
Join Date: Feb 2007
Location: Slovenia
Posts: 47
boschow can only hope to improve

An Access Violation Segmentation Fault


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.

CPP / C++ / C 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.
Last edited by LuciWiz : 02-Feb-2008 at 14:52. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 31-Jan-2008, 09:12
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: An Access Violation Segmentation Fault


[quote=boschow]i made some code.../QUOTE]
That code won't compile because you had commented out the declaration for MM.

If you had left the declaration uncommented, then the effect would have been the following:

1. You declare MM to be a pointer to a struct of type MainModule.
2. You don't give the pointer a value, so its value is something arbitrary. I don't know what the value is, but one thing for sure: the pointer is not guaranteed to be pointing to any memory that your program can use.
3. Then you try to store stuff into whatever memory is pointed to by the unitialized pointer. The result is "undefined behavior," and can definitely cause an access violation.

Solution:
Declare a variable of type MainModule. Then you can access the various memory locations within the block of memory that is defined.

CPP / C++ / C Code:
int main()
{   
    MainModule MM;
    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;
}

Or, if you really want to use a (or need to use) a pointer:
CPP / C++ / C Code:
int main()
{   
    MainModule MM;   // this is the object
    MainModule *pMM; // This is a pointer to a struct of type MainModule
    int shade_time = 0;
    pMM = &MM;       // Now it points to your object

    cout << "Digital output 1: " << endl;
    cout << "insert 1 - manual mode" << endl; 
    cout << "insert 0 - auto mode" << endl;  
    cin >> pMM->digOutN[0].wm_bc;
    
    cout << "Digital output 2: " << endl;
    cout << "insert 1 - manual mode" << endl; 
    cout << "insert 0 - auto mode" << endl; 
    cin >> pMM->digOutN[1].wm_bc;
      
    shadeM01(&pMM->digOutN[0],  //NVAR_DigitalOutputOnOff *digOut1N 
             &pMM->digOutN[1],  //NVAR_DigitalOutputOnOff *digOut2N
             &pMM->digOutV[0],  //VAR_DigitalOutputOnOff *digOut1V
             &pMM->digOutV[1],  //VAR_DigitalOutputOnOff *digOut2V
             &pMM->digInpN[0],  //NVAR_DigitalInput *digInp3
             &pMM->digInpN[1],  //NVAR_DigitalInput *digInp4
             &pMM->m01_var[0],  //NVAR_ShadeVar *shadeVar
             shade_time);      //int shade_time
    
    cout << "State of first output" << pMM->digOutN[0].do_bs << endl;
    cout << "State of second output" << pMM->digOutN[1].do_bs << endl;
    
    return 0;
}



Regards,

Dave
  #3  
Old 31-Jan-2008, 12:39
boschow boschow is offline
Junior Member
 
Join Date: Feb 2007
Location: Slovenia
Posts: 47
boschow can only hope to improve

Re: An Access Violation Segmentation Fault


Thanks for the info, it worked successfully. I also came across another method to use pointer this is :
Code:
MM = new MainModule();

but i think you solution is better.

Best regards,
BoSCHoW.
  #4  
Old 31-Jan-2008, 14:34
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: An Access Violation Segmentation Fault


Quote:
Originally Posted by boschow
...better...

There are times when declaring a pointer and then using new to get memory for the object is "better." Unless I have a reason to do it with dynamically allocated memory, I usually just declare the object. I think it is important to understand what is happening in either case and, therefore, to be comfortable with being able to use either method as circumstances might impel.

Regards,

Dave
 
 

Recent GIDBlogI?m Home by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
segmentation fault error vermin1302 C Programming Language 7 25-Feb-2006 14:38
Another Segmentation Fault Problem SergeDrago C++ Forum 2 30-Nov-2005 15:16
Please help segmentation fault problem robsmith C Programming Language 1 08-May-2005 21:34
segmentation fault in c++ rushman8282 C++ Forum 2 26-Jan-2005 04:38
access violation gmn MS Visual C++ / MFC Forum 3 04-Aug-2004 07:19

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:33.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.