Thread: Access Violation

  1. #91
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Graham Aker View Post
    Yeah, it was that one. I flopped the > to a < and it compiled perfectly. Thanks for that!
    Ehm, I'm not sure what your new code looks like, but I'm fairly convinced takes more than a flip between < and > to "fix" your code - it may prevent it from crashing, but I doubt it does the right thing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #92
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Here's the code of the running program:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define big 200
    
    struct integer {
    	  int* digits;
    	  int size;
    };
    
    struct integer* read_integer(char* stringInt);
    void print(struct integer *p);
    struct integer* add(struct integer *p, struct integer *q);
    struct integer* subtract(struct integer *p, struct integer *q);
    int compare(struct integer *p, struct integer *q);
    
    int main(){        
          FILE *ifp;
          char numbr1[big];
          char numbr2[big];
          struct integer *num1;
          struct integer *num2;
          struct integer *num3;
          int stringnum;
          int i;
          int j;
          int k;
          int comp;            
          int intarray[big];
          int option;      
          
    //Read in the file      
          ifp = fopen("bigint.txt", "r");
    
    //Scan for the number of strings
          fscanf(ifp, "%d", &stringnum);
          
    //Read in the numbers
          for (k=0; k<stringnum; k++){
               fscanf(ifp, "%d, %s, %s", option, numbr1, numbr2);
               printf("%d\n", option);           
               num1 = read_integer(numbr1);
               num2 = read_integer(numbr2);
               if(option==1){
                    num3 = add(num1, num2);       
               }
               else if(option==2){
                    num3 = subtract(num1, num2);     
               }
               else if(option==3){
                    comp = compare(num1, num2);    
               }
          }
                           
    //Read the array
          printf("The arrays are: \n");
          system("PAUSE");
          return 0;
    };
    //Define functions
    struct integer* read_integer(char* stringInt){
          int i;
          struct integer *read;
          read = (struct integer*)malloc(sizeof(struct integer));
          read->size = strlen(stringInt);
          read->digits = (int*)(malloc(read->size*sizeof(int)));
    //Move the integer into the array
          int x = read->size - 1;
          for(i = read->size - 1; i < 0; i++){
               read->digits[i]=(int)(stringInt[x] - '0');
               printf("%d", read->digits[i]);
               x--;
          }
          return read;
    }
    struct integer* add(struct integer *p, struct integer *q){
         
    }
    struct integer* subtract(struct integer *p, struct integer *q){
          
    }
    int compare(struct integer *p, struct integer *q){
         int i;
         if(p->size < q->size){
               printf("Integer 2 is greater than Integer 1");
               return -1;
         }                             
         else if(p->size < q->size){
               printf("Integer 1 is greater than Integer 2");
               return 1;                             
         }
         else if(p->size = q->size){
               for(i = 0; i > p->size; i++){
                    if(*q[i].digits > *p[i].digits){
                           printf("Integer 2 is greater than Integer 1");
                           return -1;
                    }
                    else if(*q[i].digits < *p[i].digits){
                           printf("Integer 1 is greater than Integer 2");
                           return 1;
                    }
               }
               return 0;              
         } 
    }

  3. #93
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Does your compiler allow you to enable warnings?
    gcc -Wall gives this:
    Code:
    number.c: In function `main':
    number.c:40: warning: format argument is not a pointer (arg 3)
    number.c:25: warning: unused variable `i'
    number.c:26: warning: unused variable `j'
    number.c:29: warning: unused variable `intarray'
    number.c: In function `read_integer':
    number.c:65: warning: implicit declaration of function `strlen'
    number.c: In function `add':
    number.c:76: warning: control reaches end of non-void function
    number.c: In function `subtract':
    number.c:79: warning: control reaches end of non-void function
    number.c: In function `compare':
    number.c:92: warning: suggest parentheses around assignment used as truth value
    number.c:105: warning: control reaches end of non-void function
    Some of those are pretty grave, as "implict declaration of" means you didn't include the header file of the function you use, and "reached end of non-void function" indicates that you may return rubbish from that function. Even if the function is a "stub" (meaning a nearly empty function waiting to be filled in) it should return SOMETHING.
    Code:
         if(p->size < q->size){
               printf("Integer 2 is greater than Integer 1");
               return -1;
         }                             
         else if(p->size < q->size){
               printf("Integer 1 is greater than Integer 2");
               return 1;                             
         }
         else if(p->size = q->size){
    The third if is redundant because you have only got three alternatives: a > b, a < b or a = b. So if you have already tested for the first two, the third MUST be the answer - there is no need to check it (it just makes the code bigger, which is NEVER a good idea).

    Also, I doubt you get past your fscanf() line.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #94
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by matsp View Post
    Does your compiler allow you to enable warnings?
    gcc -Wall gives this:
    Code:
    number.c: In function `main':
    number.c:40: warning: format argument is not a pointer (arg 3)
    number.c:25: warning: unused variable `i'
    number.c:26: warning: unused variable `j'
    number.c:29: warning: unused variable `intarray'
    number.c: In function `read_integer':
    number.c:65: warning: implicit declaration of function `strlen'
    number.c: In function `add':
    number.c:76: warning: control reaches end of non-void function
    number.c: In function `subtract':
    number.c:79: warning: control reaches end of non-void function
    number.c: In function `compare':
    number.c:92: warning: suggest parentheses around assignment used as truth value
    number.c:105: warning: control reaches end of non-void function
    Some of those are pretty grave, as "implict declaration of" means you didn't include the header file of the function you use, and "reached end of non-void function" indicates that you may return rubbish from that function. Even if the function is a "stub" (meaning a nearly empty function waiting to be filled in) it should return SOMETHING.
    Code:
         if(p->size < q->size){
               printf("Integer 2 is greater than Integer 1");
               return -1;
         }                             
         else if(p->size < q->size){
               printf("Integer 1 is greater than Integer 2");
               return 1;                             
         }
         else if(p->size = q->size){
    The third if is redundant because you have only got three alternatives: a > b, a < b or a = b. So if you have already tested for the first two, the third MUST be the answer - there is no need to check it (it just makes the code bigger, which is NEVER a good idea).

    Also, I doubt you get past your fscanf() line.


    --
    Mats
    My compiler does indeed enable warnings. However, even with them enabled, I never got ANY of those warnings from Dev-C++.

    And what do you mean you doubt I get past my fscanf() line?

    EDIT: Is that running with the input file I gave earlier?

  5. #95
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Graham Aker View Post
    My compiler does indeed enable warnings. However, even with them enabled, I never got ANY of those warnings from Dev-C++.

    And what do you mean you doubt I get past my fscanf() line?

    EDIT: Is that running with the input file I gave earlier?
    Seeing as Dev-C++ normally uses gcc-mingw 3.4.2, which is EXACTLY the version I'm using (in fact, I just added the dev-cpp/bin path to make it compile with gcc before compiling your code), I doubt that you have actually enabled warnings - seeing as I get them and you don't [or you simply don't look in the right place? - I don't actually use dev-cpp, just Emacs and command line for simple stuff like this].

    And my compiled code crashes inside fscanf() when trying the input file you gave earlier in the thread - with two 27-or-so digit numbers, one starting with 74 and the second starting with 29. Not that what input file is given matters, as the fault is in the call to fscanf, not in the rest of the code. Also, the warning for line 40 is the one we are talking about - I missed mentioning it in the "I get these warnings" post, but it's definitely an important one.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #96
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by matsp View Post
    Seeing as Dev-C++ normally uses gcc-mingw 3.4.2, which is EXACTLY the version I'm using (in fact, I just added the dev-cpp/bin path to make it compile with gcc before compiling your code), I doubt that you have actually enabled warnings - seeing as I get them and you don't [or you simply don't look in the right place? - I don't actually use dev-cpp, just Emacs and command line for simple stuff like this].

    And my compiled code crashes inside fscanf() when trying the input file you gave earlier in the thread - with two 27-or-so digit numbers, one starting with 74 and the second starting with 29. Not that what input file is given matters, as the fault is in the call to fscanf, not in the rest of the code. Also, the warning for line 40 is the one we are talking about - I missed mentioning it in the "I get these warnings" post, but it's definitely an important one.

    --
    Mats
    What makes this odd is the program doesn't crash at all on my end. It won't read the option right, but it technically compiles and runs fine, which makes me think there's a logic error somewhere in there...

  7. #97
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, unless you posted a different version of the code than you are actually running, it is VERY LIKELY that you will crash on the line of fscanf(), but it is POSSIBLE that you don't under some circumstances - because it is what is called "undefined behaviour". Particularly since "option" is not initialized, your runtime environment may end up with a different value than I do - try setting option to zero before reading it - that ought to crash it.

    However, I also checked the return value from fscanf(), and each fscanf() reads ONE element, not the three you expect. That's probably caused by the commas in the format string, which are not matched by commas in the input file - unless you are using a different input file than what I have:

    Code:
    2
    7432968527294356297648975290
    2986783975917945029875942976
    By the way, is this actually correct for your input - you read a stringNum first, then an option and two numbers - but there is a total of only 3 numbers in that input file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #98
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by matsp View Post
    Well, unless you posted a different version of the code than you are actually running, it is VERY LIKELY that you will crash on the line of fscanf(), but it is POSSIBLE that you don't under some circumstances - because it is what is called "undefined behaviour". Particularly since "option" is not initialized, your runtime environment may end up with a different value than I do - try setting option to zero before reading it - that ought to crash it.

    However, I also checked the return value from fscanf(), and each fscanf() reads ONE element, not the three you expect. That's probably caused by the commas in the format string, which are not matched by commas in the input file - unless you are using a different input file than what I have:

    Code:
    2
    7432968527294356297648975290
    2986783975917945029875942976
    By the way, is this actually correct for your input - you read a stringNum first, then an option and two numbers - but there is a total of only 3 numbers in that input file.

    --
    Mats
    Oh, right, sorry. I made a post earlier talking about the fact that I screwed up the original input file. Here's the real correct test input file:
    Code:
    3
    1 8888888888 2222222222
    2 9999999999 10000000000
    2 10000000000 9999999999
    Oh, yeah, and I initialized Option and the program started crashing again. Once again, it's an Access Violation somewhere... Probably at that fscanf(), now that I think about it... What should I do to fix that?

  9. #99
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Hi Graham.

    Someone else has started a thread for a problem which appears similar to yours.

    http://cboard.cprogramming.com/showthread.php?t=111542
    Last edited by happyclown; 01-26-2009 at 05:14 PM. Reason: changed some words
    OS: Linux Mint 13(Maya) LTS 64 bit.

  10. #100
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by happyclown View Post
    Hi Graham.

    Someone else has started a thread for a problem which appears almost identical to yours.

    http://cboard.cprogramming.com/showthread.php?t=111542
    That's because it's the exact same assignment. >_o

    I'll definitely need to take a look at that...

    EDIT: Lucky guy, he is. He didn't have to do the extra options...
    Last edited by Graham Aker; 01-26-2009 at 05:17 PM.

  11. #101
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Aright, I've done a little more work on the program, and it's running, but Option, numbr1, and numbr2 are spitting junk back at me. Can anyone help me fix this?

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define big 200
    
    struct integer {
    	  int* digits;
    	  int size;
    };
    
    struct integer* read_integer(char* stringInt);
    void print(struct integer *p);
    struct integer* add(struct integer *p, struct integer *q);
    struct integer* subtract(struct integer *p, struct integer *q);
    int compare(struct integer *p, struct integer *q);
    
    int main(){        
          FILE *ifp;
          char numbr1[big];
          char numbr2[big];
          struct integer *num1;
          struct integer *num2;
          struct integer *num3;
          int stringnum;
          int i;
          int j;
          int k;
          int comp;            
          int intarray[big];
          int option = 0;      
          
    //Read in the file      
          ifp = fopen("bigint.txt", "r");
    
    //Scan for the number of strings
          fscanf(ifp, "%d", &stringnum);
          printf("%d\n", stringnum);
          
    //Read in the numbers
          for (k=0; k<stringnum; k++){
               printf("%d\n", k);
               fscanf(ifp, "%d, %s, %s", &option, numbr1, numbr2);
               printf("%d, %s, %s\n", option, numbr1, numbr2);           
               num1 = read_integer(numbr1);
               num2 = read_integer(numbr2);
               if(option==1){
                    num3 = add(num1, num2);       
               }
               else if(option==2){
                    num3 = subtract(num1, num2);     
               }
               else if(option==3){
                    comp = compare(num1, num2);    
               }
          }
                           
    //Read the array
          printf("The arrays are: \n");
          system("PAUSE");
          return 0;
    };
    //Define functions
    struct integer* read_integer(char* stringInt){
          int i;
          struct integer *read;
          read = (struct integer*)malloc(sizeof(struct integer));
          read->size = strlen(stringInt);
          read->digits = (int*)(malloc(read->size*sizeof(int)));
    //Move the integer into the array
          int x = read->size - 1;
          for(i = read->size - 1; i < 0; i++){
               read->digits[i]=(int)(stringInt[x] - '0');
               printf("%d", read->digits[i]);
               x--;
          }
          return read;
    }
    void print(struct integer *p){
          int i;
        
               printf("%d\n", p->size);
        
    //this print loop prints out wrong even though the other one is right
               for(i = p->size -1 ; i >= 0; i--){
                     printf("%d\n", p->digits[i]);
               }
    
    }
    struct integer* add(struct integer *p, struct integer *q){
         
    }
    struct integer* subtract(struct integer *p, struct integer *q){
          
    }
    int compare(struct integer *p, struct integer *q){
         int i;
         if(p->size < q->size){
               printf("Integer 2 is greater than Integer 1");
               return -1;
         }                             
         else if(p->size < q->size){
               printf("Integer 1 is greater than Integer 2");
               return 1;                             
         }
         else {
               for(i = 0; i > p->size; i++){
                    if(*q[i].digits > *p[i].digits){
                           printf("Integer 2 is greater than Integer 1");
                           return -1;
                    }
                    else if(*q[i].digits < *p[i].digits){
                           printf("Integer 1 is greater than Integer 2");
                           return 1;
                    }
               }
               return 0;              
         } 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Istream::Release access violation? [C++]
    By A10 in forum Windows Programming
    Replies: 10
    Last Post: 01-13-2009, 10:56 PM
  2. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 10:52 AM
  3. access violation in int array
    By George2 in forum C Programming
    Replies: 2
    Last Post: 08-02-2007, 11:28 PM
  4. FtpFileFind access violation with MS VC++ 6.0
    By earth_angel in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2005, 07:02 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 02:46 PM

Tags for this Thread