Thread: Access violation??

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    Access violation??

    Error is this :

    C:\Program Files\PellesC\Projects\Tekrar denemeler\dene\dene.c(26): fatal error: Internal error: 'Access violation' at 0x00483798.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	
    	typedef struct string {
    	char bir[41];
    	char iki[41];
    	int section;
    } ara;
    	FILE *fp;
    	fp=fopen("liste.txt","r");
          char a[41];
    	int x=0;
    	while((fgets(a,40,fp))!=NULL) {
           x+=1;
    }
    	int b=0;
    	rewind(fp);
    	ara kayit[x];
    	int i=-1;
    	for(b=0;b<x;b++) {
              if((b % 3)==0) {
    			i++;
    			kayit[i].section=(b/3)+1; 
    		 fgets(kayit[i].bir,40,fp); }
    		if((b % 3)==1) {
    			fgets(kayit[i].iki,40,fp); }
    	}
    for(i=0;i<((x+1)/3);i++) {
    		printf("Section %d\n%s\n%s\n",kayit[i].section,kayit[i].bir,kayit[i].iki);
    	}
    return 0;
    }
    What the program does is not important , I just want to know what is my fault here as a fault error?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Most likely, something confused the compiler, and it "falls over".

    --
    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. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    First of all you need a proper code indendation. It very hard to debug the code if it like that. And more ober have you got the liste.txt in the same directory of where the .c file is lcated. Check the return value of the fopen.

    And one more thing. The 'i' should be incremented within the loop not in the if statment. If that make sense. And this should be

    Code:
    int i=-1;
    to
    Code:
    int i=0;
    ssharish

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    First of all , yes I have liste. I told you algorithm is not important coz I did not show you the liste.

    Second of all , to increment i in the if statemtnt is the key of the algorithm.

    I do not need to chect fopen. Because it always opens it . So I dont bother to control it. Besides , if the problem was that , program would crash after compiling.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    This is the liste.txt :

    Tuesday 15:40 17:30 F19
    Thursday 15:40 17:30 F17

    Tuesday 15:40 17:30 FZ19
    Thursday 12:40 14:30 F19

    Tuesday 15:40 17:30 F15
    Thursday 12:40 14:30 FZ19

    Monday 10:40 12:30 F19
    Thursday 12:40 14:30 F15

    Tuesday 15:40 17:30
    Thursday 12:40 14:30 F17

    Monday 08:40 10:30 M04
    Wednesday 10:40 12:30 M04

    Monday 08:40 10:30 F19
    Wednesday 10:40 12:30 F19

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I can't be bothered to read through all that unindented code atm, but heres a random guess thats probably wrong.

    Here:
    Code:
    fp=fopen("liste.txt","r");
    Is the file called liste.txt, not list.txt? If theres an error with the filename or the file dosent exist, you will get a memory access violation.

    [edit]Looks like i got here a bit late...[/edit]

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    And more over you index your struct array at an inappropriate level. Array indexing starts from 0 not -1. Do you understand. That will get get segfault. Or do pre-increment to i before you index.

    >I do not need to chect fopen. Because it always opens it . So I dont bother to control it. Besides , if the problem was that , program would crash after compiling.

    I can understand it always opens the file. But you wont really understand the impact of it when now, unless you use fopen is some big project and not checking for a return and scratching your head where it went wrong. IT is always a good programming practice to do error checking.

    ssharish
    Last edited by ssharish2005; 09-18-2007 at 09:35 AM.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > ara kayit[x];
    I would guess this array is declared one time as:
    Code:
    	ara kayit[1];
    If you need it to grow dynamically you may need to make kayit a pointer, and call realloc().

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    is this
    Code:
    C:\Program Files\PellesC\Projects\Tekrar denemeler\dene\dene.c(26): fatal error: Internal error: 'Access violation' at 0x00483798.
    something the compiler says, or something that happens when you run your code after it has compiled?

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

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    That is something the compiler says. And I am sorry for all other responses. All are wrong. Do not underestimate people like i=-1 wil give a segment error. If you dont bother to read the i=-1 line , I think you could read the bottom lines which includes a i++ line. And for the first time the loop works , that if statement will definitely true.Anyway. Matsp , I am listening to you, what could cause that ?

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    The word underestimate may seem like a little setrimental. But man you nearly all write for just to write. I am sorry , but thanks for responses. I know you have good intentions.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ozumsafa View Post
    That is something the compiler says. And I am sorry for all other responses. All are wrong. Do not underestimate people like i=-1 wil give a segment error. If you dont bother to read the i=-1 line , I think you could read the bottom lines which includes a i++ line. And for the first time the loop works , that if statement will definitely true.Anyway. Matsp , I am listening to you, what could cause that ?
    Basicly some sort of internal bug in the compiler, e.g. that the compiler tries to look past the end of a buffer, tries to use some uninitialized memory or such - try commenting out a portion of the code to see if that changes it.

    What line is line 26.

    --
    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. #13
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    The word underestimate may seem like a little setrimental. But man you nearly all write for just to write. I am sorry , but thanks for responses. I know you have good intentions.
    i don't mean any offense here, but don't expect people to look thoroughly through you code if you can't even be bothered to indent it correctly.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mike_g View Post
    i don't mean any offense here, but don't expect people to look thoroughly through you code if you can't even be bothered to indent it correctly.
    Now, the code isn't how I would write it (it's probably easier to check if the line is empty, rather than to read the file twice and the use every two out of three lines).

    But I don't think that's the problem. gcc compiles it with no problem, and gives this output:
    Code:
    D:\temp>a.exe
    Section 1
    Tuesday 15:40 17:30 F19
    
    Thursday 15:40 17:30 F17
    
    Section 2
    
    
    Tuesday 15:40 17:30 FZ19
    
    Section 3
    Thursday 12:40 14:30 F19
    
    
    
    Section 4
    Tuesday 15:40 17:30 F15
    
    Thursday 12:40 14:30 FZ19
    
    Section 5
    
    
    Monday 10:40 12:30 F19
    
    Section 6
    Thursday 12:40 14:30 F15
    
    
    
    Section 7
    Tuesday 15:40 17:30
    
    Thursday 12:40 14:30 F17
    Compiler output:
    Code:
    D:\temp>gcc -Wall -pedantic bb.c
    bb.c: In function `main':
    bb.c:14: warning: ISO C90 forbids mixed declarations and code
    bb.c:19: warning: ISO C90 forbids mixed declarations and code
    bb.c:21: warning: ISO C90 forbids variable-size array `kayit'
    bb.c:21: warning: ISO C90 forbids mixed declarations and code
    That's using gcc 3.4.2.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 10:52 AM
  2. access violation in int array
    By George2 in forum C Programming
    Replies: 2
    Last Post: 08-02-2007, 11:28 PM
  3. 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
  4. Help! CListCtrl access violation
    By bonkey in forum Windows Programming
    Replies: 4
    Last Post: 11-18-2003, 02:40 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 02:46 PM