Thread: Access Violation

  1. #1
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62

    Post Access Violation

    So, I'm trying to get this program of mine to work, and I keep getting an access violation in this one chunk of code, and I can't figure out what's causing it. Can anyone help me out?

    The chunk with the error (The debugger stops at "*emp[j++].digits = strtok(numbr[i], " ");"):
    Code:
    int readnum(int stringnum, char* numbr, struct integer *emp){
          char temp[0];
          int arraysize = strlen(numbr);
          int* numtemp = (int*)malloc(sizeof(int)*arraysize);
    
    //Move the integer into the array      
          for(i=arraysize, j=0; i>-1; i--){
               if(isdigit(numbr[i])){
                   *emp[j++].digits = strtok(numbr[i], " ");
               }
          }
    
    };

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    strtok does not take a character for the first argument, but a char *. Also strtok returns a char *, not a char, so hopefully that's what digits is defined to be.

  3. #3
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    digits is supposed to be an integer array, which means I'm WAY off on the funtion I need to split that character string... Any recommendations?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Incidentally, what's with temp? You do not use it in the code snippet, and arrays of zero size are not allowed in standard C (so you may be using a compiler extension).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Walk the character array. Use isdigit to see if you're currently standing on a digit; if so, subtract '0' to turn the character into the value of the character and add it to your digits array. (Hopefully your digits array already exists, as in it's a real array and not just a pointer that will simulate an array; otherwise you'd have to malloc room first.)

  6. #6
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Temp was chopped out. I was using it with Atoi before, and that wouldn't even let the program compile.

    However, it somewhat worked in a previous version, though I'm not sure why it stopped working when it was moved into a side function.

    stdio.h and stdlib.h were both included, if it means anything.
    Last edited by Graham Aker; 01-25-2009 at 01:19 PM. Reason: Uncomplete scentence

  7. #7
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by tabstop View Post
    Walk the character array. Use isdigit to see if you're currently standing on a digit; if so, subtract '0' to turn the character into the value of the character and add it to your digits array. (Hopefully your digits array already exists, as in it's a real array and not just a pointer that will simulate an array; otherwise you'd have to malloc room first.)
    It's already walking through the string with the isdigit, but subtracting 0 doesn't change anything. It still crashes and burns as a access violation.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Graham Aker View Post
    It's already walking through the string with the isdigit, but subtracting 0 doesn't change anything. It still crashes and burns as a access violation.
    Well, but wait. digits isn't assignable is it? I thought you said it was an array. You should be assigning to digits[i] or such.

  10. #10
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Here's the program without the 3 (Unfinished and commented out) extra functions and the bottom printf:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    struct integer {
    	int* digits;
    	int size;
    };
    
    int i, j, k, stringnum;
    
    int readnum(int stringnum, char* numbr, struct integer *emp);
    
    int main(){
          FILE *ifp;
          char numbr[big];
          struct integer *num = (struct integer *)malloc(sizeof(int));
    
    //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, "%s", numbr);  
               readnum(k, numbr, num);
          }
    
          system("PAUSE");
          return 0;
    };
    
    int readnum(int stringnum, char* numbr, struct integer *emp){
    
          int arraysize = strlen(numbr);
          int* numtemp = (int*)malloc(sizeof(int)*arraysize);
    
    //Move the integer into the array      
          for(i=arraysize, j=0; i>-1; i--){
               if(isdigit(numbr[i])){
    
                   *emp[j++].digits = strtok(numbr[i], " ");
               }
          }

  11. #11
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by tabstop View Post
    Well, but wait. digits isn't assignable is it? I thought you said it was an array. You should be assigning to digits[i] or such.
    When I try that, DevC++ won't even compile the program. Oddly enough, it tells me it won't because digits isn't part of a structure even though it obviously is...

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you don't want a numtemp. You should have
    Code:
    emp->digits = malloc(etc);
    so that you have room to put things. Similarly you need emp->digits[j++] on the left side of that statement down below (and for the love of all that is good, get rid of strtok already). You are trying to walk from one structure to another, not move along the digits array. (Hence the reason "Dev-C++ won't do this" earlier -- emp is not a structure but a pointer to a structure, so emp does indeed not have a field called digits (it doesn't have any fields at all). And the * at the front doesn't happen until the very end.)

    If you want to dereference emp (and you do), you could do (*emp).whatever, but it's more normal to use -> instead.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, now the problem becomes clearer

    digits is not an integer array. It is a pointer to an int that you presumably want to point to the first integer of a dynamically allocated integer array.

    Unfortunately, your sample program is not compilable, so it is hard to see where exactly the access violation might lie because there are more important errors to fix, i.e., the compile errors. My guess is that the access violation is directly related to the fact that digits is a pointer, not an array, so you ended up accessing memory that you do not own.

    This is why I suggested that the program that you posted be compilable: when we have a program that is free of compile errors, it is easier to zoom in to the spot that might be the underlying reason for the access violation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    In that case, what should I use in place of strtok? Because Atoi wasn't very helpful, and I need to split that string somehow.

    Also, DevC++ now tells me that "*emp->digits = (int*)malloc(sizeof(int)*arraysize);" makes an integer from a pointer without a cast. What does that mean?
    Quote Originally Posted by laserlight View Post
    Ah, now the problem becomes clearer

    digits is not an integer array. It is a pointer to an int that you presumably want to point to the first integer of a dynamically allocated integer array.

    Unfortunately, your sample program is not compilable, so it is hard to see where exactly the access violation might lie because there are more important errors to fix, i.e., the compile errors. My guess is that the access violation is directly related to the fact that digits is a pointer, not an array, so you ended up accessing memory that you do not own.

    This is why I suggested that the program that you posted be compilable: when we have a program that is free of compile errors, it is easier to zoom in to the spot that might be the underlying reason for the access violation.
    Odd, it didn't compile? *Goes to check why*

    Whoops, I forgot the "#define big 2000" at the top and missed the "};" at the end of the readnum function. Sorry, my bad. :P
    Last edited by Graham Aker; 01-25-2009 at 02:05 PM. Reason: Another message posted during original message

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You subtract '0'. That's all. There's no function, because, " - '0'" was considered easy enough to type, I guess.

    And notice in my examples the complete absence of * in front of emp. Notice how including it leads to bad things. There is a connection here.

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