access violation problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JasBascom

    access violation problem


    int main()
    {
    char record_typec = 'c';
    record_typec = toupper(record_ typec);
    * rec->Newcrecord.rec ord_type = record_typec;

    etc
    etc

    \************** *************** *************** *\

    I've run a debug on the program and this is where I'm encountering the access
    violation problem. I've tried the dot operator to access the structure but that
    doesn't appear to work. How can change the syntax and avoid the access
    violation error?
  • John Carson

    #2
    Re: access violation problem

    "JasBascom" <jasbascom@aol. com> wrote in message
    news:2004022008 4617.16190.0000 0041@mb-m11.aol.com[color=blue]
    > int main()
    > {
    > char record_typec = 'c';
    > record_typec = toupper(record_ typec);
    > * rec->Newcrecord.rec ord_type = record_typec;
    >
    > etc
    > etc
    >
    > \************** *************** *************** *\
    >
    > I've run a debug on the program and this is where I'm encountering
    > the access violation problem. I've tried the dot operator to access
    > the structure but that doesn't appear to work. How can change the
    > syntax and avoid the access violation error?[/color]


    Since most people here lack psychic abilities, I suggest that you tell us
    how rec is defined.


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)

    Comment

    • Rolf Magnus

      #3
      Re: access violation problem

      JasBascom wrote:
      [color=blue]
      >
      > int main()
      > {
      > char record_typec = 'c';
      > record_typec = toupper(record_ typec);
      > * rec->Newcrecord.rec ord_type = record_typec;
      >
      > etc
      > etc
      >
      > \************** *************** *************** *\
      >
      > I've run a debug on the program and this is where I'm encountering the
      > access violation problem. I've tried the dot operator to access the
      > structure but that doesn't appear to work. How can change the syntax
      > and avoid the access violation error?[/color]

      How would anyone hwere know? What is rec? Where is it defined? It seems
      to be a pointer, so where did you let it point to something? What is
      the '*' at the beginning of the last line supposed to do?

      Comment

      • JasBascom

        #4
        Re: access violation problem

        rec is the pointer to a union
        it is declared thus union Allrecords* rec.
        Now that you know what rec is does that help you to help me?

        Comment

        • John Carson

          #5
          Re: access violation problem

          "JasBascom" <jasbascom@aol. com> wrote in message
          news:2004022009 3158.16083.0000 0075@mb-m11.aol.com[color=blue]
          > rec is the pointer to a union
          > it is declared thus union Allrecords* rec.
          > Now that you know what rec is does that help you to help me?[/color]

          Yes. A pointer simply stores a memory address. Thus rec is meant to store
          the address of an instance of Allrecords. However, you never create an
          instance of Allrecords (as far as one can tell from the still-limited
          information that you have provided), so rec never gets the chance to perform
          its proper function. Instead, whatever value rec is storing points to memory
          that you don't own and hence you get an access violation when you try to
          write to it. You could create an instance of Allrecords with the following:

          union Allrecords* rec = new Allrecords;

          which allocates memory for an instance of Allrecords and makes rec point to
          it. You could also forget about pointers and just declare:

          union Allrecords rec_obj;

          to create an Allrecords object called rec_obj.

          Whether you are accessing the members of this structure correctly with

          * rec->Newcrecord.rec ord_type = record_typec;

          is again impossible to tell without seeing the definition of Allrecords.


          --
          John Carson
          1. To reply to email address, remove donald
          2. Don't reply to email address (post here instead)

          Comment

          • David Harmon

            #6
            Re: access violation problem

            On 20 Feb 2004 13:46:17 GMT in comp.lang.c++, jasbascom@aol.c om
            (JasBascom) was alleged to have written:[color=blue]
            >int main()
            >{
            >char record_typec = 'c';
            > record_typec = toupper(record_ typec);
            >* rec->Newcrecord.rec ord_type = record_typec;
            >
            >etc[/color]

            Variable "rec" is never declared above.
            "Newrecord" is never declared above.
            etc.

            Post small, simplified but COMPLETE and COMPILEABLE samples so people
            don't have to guess what you are talking about.

            This issue is covered in Marshall Cline's C++ FAQ. See the topic
            "[5.8] How do I post a question about code that doesn't work correctly?"
            It is always good to check the FAQ before posting. You can get the FAQ
            at:


            Comment

            • JasBascom

              #7
              Re: access violation problem

              The full program is follows. Thank you john for pointing out the access
              violation error. i also have a problem with the declaring of an fstream object
              - sort_file. For some reason my debugger is unable to move beyond that point in
              the code.

              I would also like to have rec.Newcrecord. record_type assigned the character 'c'
              and to toupper the record_type. can you please help in both instances.

              #include <iostream>
              #include <iomanip>
              #include <fstream>
              #include <cstring>
              #include <cstdlib>


              using namespace std;

              struct crecord {
              char record_type;
              char customercode[5];
              char customername[21];
              char customeraddress[61];
              char customerbalance ;
              char creditlimit;
              int Totalbalance;
              int Totalcreditlimi t;

              };


              struct irrecord {
              char record_type;
              char customercode[5];
              char partnum[6];
              char issue_rec[5];

              };


              struct drecord {
              char record_type;
              char customercode[5];
              };


              int loop = 200;
              long offset = 1;

              union Allrecords{
              struct crecord Newcrecord;
              struct irrecord Newirrecord;
              struct drecord Newdrecord;

              };
              union Allrecords* rec;




              void sort_function( union Allrecords* rec, fstream& validdata )
              {

              union Allrecords *str_ptr1, *str_ptr2, tempstr;


              for(int i =0; i< loop; i++)
              while( strcmp(str_ptr1[i].Newcrecord.cus tomercode, '\0') ||
              strcmp(str_ptr1[i].Newdrecord.cus tomercode, '\0') ||
              strcmp(str_ptr1[i].Newirrecord.cu stomercode, '\0'))
              {
              str_ptr2 = str_ptr1 + 1;//set to next element.

              for( i=0; i<loop; i++)
              while( strcmp(str_ptr2[i].Newcrecord.cus tomercode, '\0') ||
              strcmp(str_ptr2[i].Newdrecord.cus tomercode, '\0'))
              {
              for(int i=0; i<loop; i++)
              if( strcmp( str_ptr1[i].Newirrecord.cu stomercode,
              str_ptr2[i].Newirrecord.cu stomercode + 1))
              {
              tempstr = *str_ptr1;
              *str_ptr1 = *str_ptr2;
              *str_ptr2 = tempstr;

              }
              *str_ptr1++;//incremented, so that the same code isn't sorted again
              }
              str_ptr2++;
              }

              }









              int main()
              {
              const char sorted_file[] = "A:\\514650SDP2 .txt";
              const char outfile[] = "A:\\514650VDP1 .bin";




              long offset = 1;
              int filesize;
              int reccount;



              fstream sort_file;
              fstream validdata;



              sort_file.open( "A:\\514650SDP2 .txt", ios::out);
              if(!sort_file)
              {
              cout<<"Cannot create file"<< endl;
              return EXIT_FAILURE;
              };

              validdata.open( "A:\\514650VDP1 .bin", ios::in || ios::binary);
              if(!validdata)
              {
              cout<<" Cannot find file"<<endl;
              return EXIT_FAILURE;
              };


              validdata.seekg (-offset, ios::end);
              filesize = validdata.tellg ();
              validdata.seekg (offset, ios::beg);

              reccount = sizeof(filesize )/sizeof(Allrecor ds);
              rec = new(Allrecords[reccount]);


              validdata.read( (char*) rec, filesize);//read the whole file.



              for(int i =0; i <reccount; i++)
              {
              switch(rec[i].Newdrecord.rec ord_type)
              {
              case 'c':
              case 'C':
              case 'i':
              case 'I':
              case 'r':
              case 'R':
              case 'd':
              case 'D':
              sort_function(r ec, validdata);
              default:;
              };

              };

              return 0;

              }




              Comment

              • David Harmon

                #8
                Re: access violation problem

                On 20 Feb 2004 16:57:46 GMT in comp.lang.c++, jasbascom@aol.c om
                (JasBascom) was alleged to have written:[color=blue]
                > while( strcmp(str_ptr1[i].Newcrecord.cus tomercode, '\0') ||[/color]

                Note that '\0', being a const integral expression with a value of zero,
                is a valid C++ null pointer. But not what you wanted. What you wrote
                is equivalent to

                while( strcmp(str_ptr1[i].Newcrecord.cus tomercode, NULL) ||

                while I think what you wanted is

                while( strcmp(str_ptr1[i].Newcrecord.cus tomercode, "") ||


                Comment

                • David Harmon

                  #9
                  Re: access violation problem

                  On 20 Feb 2004 16:57:46 GMT in comp.lang.c++, jasbascom@aol.c om
                  (JasBascom) was alleged to have written:[color=blue]
                  > validdata.open( "A:\\514650VDP1 .bin", ios::in || ios::binary);[/color]

                  Wrong, need bitwise or, not boolean or, should be

                  validdata.open( "A:\\514650VDP1 .bin", ios::in | ios::binary);

                  Comment

                  • David Harmon

                    #10
                    Re: access violation problem

                    On 20 Feb 2004 16:57:46 GMT in comp.lang.c++, jasbascom@aol.c om
                    (JasBascom) was alleged to have written:[color=blue]
                    > for(int i=0; i<loop; i++)
                    > if( strcmp( str_ptr1[i].Newirrecord.cu stomercode,
                    >str_ptr2[i].Newirrecord.cu stomercode + 1))[/color]
                    {
                    OK, I give up on following the logic of your sort function. Especially
                    with all the comparison tests mixed in with the sort logic. How bad
                    would it hurt to replace it all with std::sort() ?

                    bool compare(const Allrecords* left, const Allrecords* right)
                    {
                    return strcmp( left->Newirrecord.cu stomercode,
                    right->Newirrecord.cu stomercode));
                    }

                    Then,

                    std::sort(rec, rec+reccount, compare);

                    This is probably not the exact right code because I gave up on exactly
                    following your logic, but how far off is it?

                    Comment

                    • JasBascom

                      #11
                      Re: access violation problem

                      thank you so far, but i feel the sort is ok, i won't know until I can get the
                      sort file to work. The sort file does create a text file on the floppy disk,
                      but when I go to execute the program there is an access violation error
                      concerning the sort file.
                      Thanks again for your help, can someone run the program rthrough their own
                      compiler just to check.
                      The program is suppose to open a binary file (in this case validdata), and sort
                      the contents of validdata and then write this to sort_file.
                      if anyone is feel brave or a little bored can they actually write a program for
                      me that opens a text file and a binary file and get the contents of the binary
                      file to the sort_file.

                      Comment

                      • John Harrison

                        #12
                        Re: access violation problem

                        Comments follow.

                        I see you didn't follow my earlier advice not to try to do too much at once.
                        Your post is full of code which does nothing like what you hope it does.
                        This isn't surprising, programming is hard and you are new at this.

                        There is a way to avoid wasting your time writing lots of code which will
                        just have to be thrown away. The way is to write small amounts of code at a
                        time (literally 3, 4, 5 lines at most), and get those lines working before
                        you write any more. This is how professionals work, although their
                        experience means that they can write code in bigger chunks than 3 - 5 lines.

                        The way to have you tearing your hair out is to write lots of code without
                        doing any testing at all.

                        Don't worry newbies never follow this advice. You'll find out the hard way.


                        "JasBascom" <jasbascom@aol. com> wrote in message
                        news:2004022011 5746.16083.0000 0078@mb-m11.aol.com...[color=blue]
                        > The full program is follows. Thank you john for pointing out the access
                        > violation error. i also have a problem with the declaring of an fstream[/color]
                        object[color=blue]
                        > - sort_file. For some reason my debugger is unable to move beyond that[/color]
                        point in[color=blue]
                        > the code.
                        >
                        > I would also like to have rec.Newcrecord. record_type assigned the[/color]
                        character 'c'[color=blue]
                        > and to toupper the record_type. can you please help in both instances.
                        >
                        > #include <iostream>
                        > #include <iomanip>
                        > #include <fstream>
                        > #include <cstring>
                        > #include <cstdlib>
                        >
                        >
                        > using namespace std;
                        >
                        > struct crecord {
                        > char record_type;
                        > char customercode[5];
                        > char customername[21];
                        > char customeraddress[61];
                        > char customerbalance ;
                        > char creditlimit;
                        > int Totalbalance;
                        > int Totalcreditlimi t;
                        >
                        > };
                        >
                        >
                        > struct irrecord {
                        > char record_type;
                        > char customercode[5];
                        > char partnum[6];
                        > char issue_rec[5];
                        >
                        > };
                        >
                        >
                        > struct drecord {
                        > char record_type;
                        > char customercode[5];
                        > };
                        >
                        >
                        > int loop = 200;
                        > long offset = 1;
                        >
                        > union Allrecords{
                        > struct crecord Newcrecord;
                        > struct irrecord Newirrecord;
                        > struct drecord Newdrecord;
                        >
                        > };
                        > union Allrecords* rec;
                        >
                        >
                        >
                        >
                        > void sort_function( union Allrecords* rec, fstream& validdata )
                        > {
                        >
                        > union Allrecords *str_ptr1, *str_ptr2, tempstr;
                        >
                        >
                        > for(int i =0; i< loop; i++)
                        > while( strcmp(str_ptr1[i].Newcrecord.cus tomercode, '\0') ||
                        > strcmp(str_ptr1[i].Newdrecord.cus tomercode, '\0') ||
                        > strcmp(str_ptr1[i].Newirrecord.cu stomercode, '\0'))
                        > {
                        > str_ptr2 = str_ptr1 + 1;//set to next element.
                        >
                        > for( i=0; i<loop; i++)
                        > while( strcmp(str_ptr2[i].Newcrecord.cus tomercode, '\0') ||
                        > strcmp(str_ptr2[i].Newdrecord.cus tomercode, '\0'))
                        > {
                        > for(int i=0; i<loop; i++)
                        > if( strcmp( str_ptr1[i].Newirrecord.cu stomercode,
                        > str_ptr2[i].Newirrecord.cu stomercode + 1))
                        > {
                        > tempstr = *str_ptr1;
                        > *str_ptr1 = *str_ptr2;
                        > *str_ptr2 = tempstr;
                        >
                        > }
                        > *str_ptr1++;//incremented, so that the same code isn't sorted again
                        > }
                        > str_ptr2++;
                        > }
                        >
                        > }[/color]

                        This function will crash as soon as it is entered. The problem is the
                        uninitialised pointers str_ptr1 and str_ptr2. This is the same mistake you
                        made in main. More seriously this code performs nothing like a sort, not
                        even close, it should be thrown away.
                        [color=blue]
                        >
                        >
                        >
                        >
                        >
                        >
                        >
                        >
                        >
                        > int main()
                        > {
                        > const char sorted_file[] = "A:\\514650SDP2 .txt";
                        > const char outfile[] = "A:\\514650VDP1 .bin";
                        >
                        >
                        >
                        >
                        > long offset = 1;
                        > int filesize;
                        > int reccount;
                        >
                        >
                        >
                        > fstream sort_file;
                        > fstream validdata;
                        >
                        >
                        >
                        > sort_file.open( "A:\\514650SDP2 .txt", ios::out);
                        > if(!sort_file)
                        > {
                        > cout<<"Cannot create file"<< endl;
                        > return EXIT_FAILURE;
                        > };
                        >
                        > validdata.open( "A:\\514650VDP1 .bin", ios::in || ios::binary);
                        > if(!validdata)
                        > {
                        > cout<<" Cannot find file"<<endl;
                        > return EXIT_FAILURE;
                        > };
                        >
                        >
                        > validdata.seekg (-offset, ios::end);
                        > filesize = validdata.tellg ();
                        > validdata.seekg (offset, ios::beg);[/color]

                        What is this? Why seek to one byte before then end of the file? What do you
                        think that achieves?
                        [color=blue]
                        >
                        > reccount = sizeof(filesize )/sizeof(Allrecor ds);[/color]

                        This is wrong. Should be filesize not sizeof(filesize ). This is a good
                        example of where you are gonig wrong. Obviously you cannot go any further
                        until you have worked out the number of records in the file. Until you can
                        do this the rest of your program isn't going to work. So you should have
                        written the above code, and then STOPPED THERE! Tested your code and seen if
                        you calculated the number of records correctly. Then you can carry on and
                        write some more code. At the moment your code doesn't work, and you have no
                        idea of its the number of records that is wrong, or something completely
                        different.

                        [color=blue]
                        > rec = new(Allrecords[reccount]);[/color]

                        [color=blue]
                        >
                        >
                        > validdata.read( (char*) rec, filesize);//read the whole file.
                        >
                        >
                        >
                        > for(int i =0; i <reccount; i++)
                        > {
                        > switch(rec[i].Newdrecord.rec ord_type)
                        > {
                        > case 'c':
                        > case 'C':
                        > case 'i':
                        > case 'I':
                        > case 'r':
                        > case 'R':
                        > case 'd':
                        > case 'D':
                        > sort_function(r ec, validdata);[/color]

                        This seems a bit confused, are you trying to sort all the records or just
                        one record? I presume you are trying to sort all the records, but then why
                        is the sort_function call in a loop?
                        [color=blue]
                        > default:;
                        > };
                        >
                        > };
                        >
                        > return 0;
                        >
                        > }[/color]

                        Seriously the best advice would be to throw this code away and take my
                        advice to write the program a little bit at a time. You are just heading for
                        weeks and weeks of frustration any other way.

                        john


                        Comment

                        • John Harrison

                          #13
                          Re: access violation problem


                          "JasBascom" <jasbascom@aol. com> wrote in message
                          news:2004022013 2535.16190.0000 0062@mb-m11.aol.com...[color=blue]
                          > thank you so far, but i feel the sort is ok, i won't know until I can get[/color]
                          the[color=blue]
                          > sort file to work.[/color]

                          That's the problem isn't it? Suppose you get the sort file to work, maybe
                          the changes you make as a result of that, will mean that you have to rewrite
                          the sort function.

                          For the record the sort function is not remotely right.
                          [color=blue]
                          > The sort file does create a text file on the floppy disk,
                          > but when I go to execute the program there is an access violation error
                          > concerning the sort file.
                          > Thanks again for your help, can someone run the program rthrough their own
                          > compiler just to check.
                          > The program is suppose to open a binary file (in this case validdata), and[/color]
                          sort[color=blue]
                          > the contents of validdata and then write this to sort_file.
                          > if anyone is feel brave or a little bored can they actually write a[/color]
                          program for[color=blue]
                          > me that opens a text file and a binary file and get the contents of the[/color]
                          binary[color=blue]
                          > file to the sort_file.
                          >[/color]

                          Something like this (untested, but please test it your self!)

                          ifstream validdata("myfi le", ios::in|ios::bi nary);
                          validdata.seekg (0, ios::end);
                          filesize = validdata.tellg ();
                          recount = filesize/sizeof(Allrecor ds);
                          rec = new Allrecords[reccount];
                          validdata.seekg (0, ios::beg);
                          validdata.read( rec, filesize);
                          validdata.close ();

                          john


                          Comment

                          • David Harmon

                            #14
                            Re: access violation problem

                            On Fri, 20 Feb 2004 18:32:29 -0000 in comp.lang.c++, "John Harrison"
                            <john_andronicu s@hotmail.com> was alleged to have written:[color=blue]
                            >
                            >"JasBascom" <jasbascom@aol. com> wrote in message
                            >news:200402201 32535.16190.000 00062@mb-m11.aol.com...[color=green]
                            >> thank you so far, but i feel the sort is ok, i won't know until I can get[/color]
                            >the[color=green]
                            >> sort file to work.[/color]
                            >
                            >That's the problem isn't it? Suppose you get the sort file to work, maybe
                            >the changes you make as a result of that, will mean that you have to rewrite
                            >the sort function.[/color]

                            Ditto. Comment out the sort function and don't even worry about it
                            until the program reads and writes the file without sorting it.

                            Comment

                            Working...