Access Violation

This is a discussion on Access Violation within the C Programming forums, part of the General Programming Boards category; ...Okay... So how do I fix it?...

  1. #76
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    ...Okay... So how do I fix it?

  2. #77
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Which of the several corrections is it that you do not understand?

    --
    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.

  3. #78
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by matsp View Post
    Which of the several corrections is it that you do not understand?

    --
    Mats
    Isn't the problem I'm dealing with now a bit different?

  4. #79
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Graham Aker View Post
    Isn't the problem I'm dealing with now a bit different?
    Yes, I'm sure it is different from your original question on the 6th page of the thread.

    But instead of asking a wide-ranging "how do I fix that" as a reply to someone (vart in this case) suggesting several things that are wrong in your code, ask a more specific question about what you don't understand - unless you completely do not understand the statement, in which case perhaps you should ask "I don't understand what you mean, can you explain what XYZ means".

    --
    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.

  5. #80
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Oh. Well, yeah, I guess I should have been more specific...

    How do I make sure the value I want is returned in that function?

  6. #81
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, so the first question is what you actually intend to do - since you are storing data in a local array of integers, you return the last element of that integer array [actually element -1, since that is when the loop is terminated] - the return type, however, is a pointer to struct integer. I bet you have a warning of "making pointer from integer without a cast" or some such on the last line.

    I haven't read enough of your code to know how you go from an integer array to a struct integer, but I suspect it involves yet another loop.

    I think vart also implies that if the input is not a digit, it simply leaves whatever rubbish happens to be in the intarray variable there when the input value is not a digit. You may want to do something slightly different for that.

    --
    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.

  7. #82
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    After a great deal of help from some of my friends, I think I fixed most of the problem in that function. However, somehow, I've gotten myself another Access Violation. Here's the new code:

    Code:
    struct integer* read_integer(char* stringInt){
          int arraysize = strlen(stringInt);      
          struct integer *read;
          read = (struct integer*)malloc(sizeof(struct integer));
          read->size = arraysize;
          read->digits = (int*)malloc(sizeof(int)*arraysize);
    //Move the integer into the array      
          for(i=0, j=0; i<arraysize; i++){
               read->digits[arraysize-1-i]=(int)stringInt[i] - '0';
          }
          return read;
    };
    Where exactly am I going wrong here?

  8. #83
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Graham Aker View Post
    Oh. Well, yeah, I guess I should have been more specific...

    How do I make sure the value I want is returned in that function?
    I cannot read your mind to understand "what you want to return"

    But mostly - when function returns a pointer - it should allocate memory with malloc and return pointer to this memory.

    Alternative solution - pass a struct integer* as a parameter (allocated in the calling function using malloc or on stack), and work in this function with that parameter
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #84
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Graham Aker View Post

    Where exactly am I going wrong here?
    This function seems to be ok
    you do not need j
    but you need to check the return value of malloc
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #85
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    How would I check the return value of malloc?

    Also, if it can help uncover where the Access Violation is, I'll post the full code again (Minus the extra functions)

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define big 200
    
    struct integer {
    	  int* digits;
    	  int size;
    };
    
    struct integer* read_integer(char* stringInt);
    
    int main(){        
          FILE *ifp;
          char numbr1[big];
          char numbr2[big];
          struct integer *num1;
          struct integer *num2;
          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);
               num1 = read_integer(numbr1);
               num2 = read_integer(numbr2);
          }
                           
    //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');
               x--;
          }
          return read;
    }

  11. #86
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,276
    Quote Originally Posted by Graham Aker
    How would I check the return value of malloc?
    You would check if it is a null pointer (e.g., you would check the value of read). If so, you would need to decide how you want to handle it, e.g. clean up by freeing the memory that has already been dynamically allocated and terminating the program, or in some perhaps more graceful way.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #87
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    for(i = read->size - 1; i>0; i++){
    assuming read->size >= 1, this loop will go on for quite some time - like 2 billion iterations or so, if you don't stomp outside your allocated memory.

    --
    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.

  13. #88
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    I slapped a printf in there to check the value and I got a screenful of junk. So, how exactly would I free up that memory that's causing all of this?

    EDIT: Wait, nevermind, it was indeed that loop. Thanks.
    Last edited by Graham Aker; 01-26-2009 at 11:34 AM.

  14. #89
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Graham Aker View Post
    I slapped a printf in there to check the value and I got a screenful of junk. So, how exactly would I free up that memory that's causing all of this?
    Where did you add your printf? In the loop I pointed at? If so, it is definitely a badly written loop - no case of "freeing up" anything.

    --
    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.

  15. #90
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Yeah, it was that one. I flopped the > to a < and it compiled perfectly. Thanks for that!

    Now the problem is the addition, subtraction, and comparison functions that come next...

Page 6 of 7 FirstFirst 1234567 LastLast
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, 11:52 AM
  3. access violation in int array
    By George2 in forum C Programming
    Replies: 2
    Last Post: 08-03-2007, 12:28 AM
  4. FtpFileFind access violation with MS VC++ 6.0
    By earth_angel in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2005, 08: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21