Access Violation

This is a discussion on Access Violation within the C Programming forums, part of the General Programming Boards category; Ah, thanks! Sorry about being an ass earlier. >.>;; EDIT: Well, now it's compiling, but it's not saving the strings ...

  1. #46
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Ah, thanks! Sorry about being an ass earlier. >.>;;

    EDIT: Well, now it's compiling, but it's not saving the strings into seperate structs. Anyone have any idea why?
    Last edited by Graham Aker; 01-25-2009 at 04:24 PM.

  2. #47
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Can you post your working code that compiled?
    OS: Linux Mint 13(Maya) LTS 64 bit.

  3. #48
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Sure. Here it is:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define big 2000
    #define length 20
    
    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[length];
    
    //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[k]);
          }
                           
    //Read the array
          printf("\n The arrays are: ");
          for (k=0; k<stringnum; k++){
               for (i=0; i<j; i++)
                   printf ("%d ", num[k]->digits[i]);
          printf("\n");
          }
    
          system("PAUSE");
          return 0;
    };
    //Define functions
    int readnum(int stringnum, char* numbr, struct integer *emp){
    
          int arraysize = strlen(numbr);      
          emp->digits = malloc(sizeof(*emp->digits) * arraysize);
          
    //Move the integer into the array      
          for(i=arraysize, j=0; i>-1; i--){
               if(isdigit(numbr[i])){
                   emp->digits[j++] = numbr[i] - '0';
               }
          }
          emp->size = j;
    };

  4. #49
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    When I compile that code, and try to run the program, it crashes. Does it run at all for you?
    OS: Linux Mint 13(Maya) LTS 64 bit.

  5. #50
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    It was running... odd, I'll have to check out what's going on...

    EDIT: Huh, it's not running anymore... Try running it in Debug mode.

    EDIT 2: It works fine in debug, except the first line gets the first digit chopped off and it ends in yet another Access Violation. any ideas on what's causing this one?
    Last edited by Graham Aker; 01-25-2009 at 05:03 PM.

  6. #51
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    struct integer *num[length];
    This gives you an array of pointers to integer objects, that would point to integer objects if you had any (which you don't). It does not give you any integer objects. Since you want integer objects, you need to not declare an array of pointers, but an array of objects. (This also causes other errors, where you have e.g. num[k]-> when you would really need num[k]. instead. Note that even when you get dynamic array-ness working, you would still need num[k]. instead of num[k]->.

  7. #52
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    I've noticed some strange things with the code

    Code:
    struct integer {
    	int* digits; <===?
    	int size;
    };
    Why did you declare digits as a pointer?

    digits is never initialised(never had any value assigned to it), so it contains garbage. Then when you try to use it here
    Code:
    printf ("%d ", num[k]->digits[i]);
    printf is trying to print out garbage. And you are treating digits[i] as an integer(i think that's what you want to do), when you declared it as a pointer.

    I don't know, I am a newbie myself, so I could be wrong.
    Last edited by happyclown; 01-25-2009 at 05:20 PM.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  8. #53
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by happyclown View Post
    I've noticed some strange things with the code

    Code:
    struct integer {
    	int* digits; <===?
    	int size;
    };
    Why did you declare digits as a pointer?

    digits is never initialised(never had any value assigned to it), so it contains garbage. Then when you try to use it here
    Code:
    printf ("%d ", num[k]->digits[i]);
    printf is trying to print out garbage. And you are treating digits[i] as an integer, when you declared it as a pointer.

    I don't know, I am a newbie myself, so I could be wrong.
    You forgot about the whole "readnum" function, where digits is allocated memory.

  9. #54
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by tabstop View Post
    Code:
    struct integer *num[length];
    This gives you an array of pointers to integer objects, that would point to integer objects if you had any (which you don't). It does not give you any integer objects. Since you want integer objects, you need to not declare an array of pointers, but an array of objects. (This also causes other errors, where you have e.g. num[k]-> when you would really need num[k]. instead. Note that even when you get dynamic array-ness working, you would still need num[k]. instead of num[k]->.
    Okay, then how would I go about turning my array of pointers into an array of integer objects? The num[k]-> problem is easy enough to fix.

  10. #55
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I know it's hard, but you must refrain from typing *.

  11. #56
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by happyclown View Post
    I've noticed some strange things with the code

    Code:
    struct integer {
    	int* digits; <===?
    	int size;
    };
    Why did you declare digits as a pointer?
    I have these limitations my Professor put in place, and in the struct it's very specific:
    Quote Originally Posted by Assignment doc
    Implementation Restrictions
    You must use the following struct:

    Code:
    struct integer {
    	int* digits;
    	int size;
    }
    Whenever you store or return a big integer, always make sure not to return it with any leading zeros. Namely, make sure that the value stored in index size-1 is NOT zero. The only exception to this rule is if 0 is being stored. 0 should be stored in an array of size 1.

    Quote Originally Posted by tabstop View Post
    I know it's hard, but you must refrain from typing *.
    What's that supposed to mean? O_o

  12. #57
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What does it mean? It looks pretty straightforward to me: don't type *. Remember the line we're talking about, notice how it has a * in it, and don't do that.

  13. #58
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    I think tabstop means to stop using an array of pointers, and to just use a normal array.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  14. #59
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Part of the problem with that line is that the professor specifically said it had to be like that. I even posted the excerpt from the assignment instructions.

    So in other words, I need to find a way around that line. Any ideas?

  15. #60
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Which line? int *digits is fine. struct integer *num[length] is most assuredly not.

Page 4 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, 09: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, 01: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