Results 1 to 4 of 4

Thread: Access violation or not

  1. #1
    Join Date
    Jun 2009
    Posts
    105

    Access violation or not

    Here's my program:

    Description:

    The program asks you to enter a number in decimal, and then it begins to divide that number by number basis. It begins to divide and we stop when the quotient is equal to 0, while setting the table in the result, and displaying so reversed, we get the number correctly. It has decremented the counter, because the last element of the array containing n elements is indexed (n-1).

    Code:
    #include <stdio.h>   
    int main()   
    {   
    int base[14][20],counter[14],rest=0,i,division,decimal;   
    for(i=0;i<=14;i++)   
    counter[i] = 0;   
    printf("Enter a number in decimal :\n");   
    scanf("%d",&decimal);    
    for(i=2;i<=16;i++)   
    {   
    division = decimal;   
             while(division != 0)    
             {   
             rest = division%i;    
             division = division/i;   
             base[i-2][counter[i-2]++] = rest;    
             }   
    counter[i-2]--; //last element of tab is indexed n-1
    printf("\nBase %d : ",i);   
                    while(counter[i-2] >= 0)    
                    {   
                    if (base[i-2][counter[i-2]] >= 10)   
                    printf("%c",base[i-2][counter[i-2]--]+55); //10+55 = 65(A)   
                    else   
                    printf("%d",base[i-2][counter[i-2]--]);   
                    }
    }   
    printf("\n");   
    system("pause");   
    return 0;       
    }
    All goes well, except for an access violation that comes out of nowhere, especially with relatively large numbers, like 10,000 will be a problem, although the conversion is the longest one in binary, and it does not exceed the limit of the table ie 20.

    Can you tell me where is the problem, though I fumbled with the debugger in vain?

  2. #2
    Join Date
    May 2008
    Posts
    685

    Re: Access violation or not

    Code:
    #include <stdio.h> 
    int main() 
    { 
    int base[14][20],counter[14],rest=0,i,division,decimal; 
    for(i=0;i<=14;i++) 
    counter[i] = 0; 
    printf("Enter a number in decimal :\n" ); 
    scanf("%d",&decimal); 
    for(i=2;i<=16;i++) 
    { 
    division = decimal; 
    while(division != 0) 
    { 
    rest = division%i; 
    division = division/i; 
    base[i-2][counter[i-2]++] = rest; 
    } 
    counter[i-2]--; //last element of tab is indexed n-1
    printf("\nBase %d : ",i); 
    while(counter[i-2] >= 0) 
    { 
    if (base[i-2][counter[i-2]] >= 10) 
    printf("%c",base[i-2][counter[i-2]--]+55); //10+55 = 65(A)   
    else 
    printf("%d",base[i-2][counter[i-2]--]); 
    } 
    } 
    printf("\n" ); 
    system("pause" ); 
    return 0;     
    }
    base, counter => 14 items, on i [2, 16] => base[i-2][counter [i-2] => explosion

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: Access violation or not

    You iterates in line 9 from 2 to 16. Then you'll tap in counter [i - 2]. Counter is an array of 14. So when you pass the last time you tap in meter [14], the 15th box.

  4. #4
    Join Date
    Jun 2009
    Posts
    105

    Re: Access violation or not

    Ah yes, 16-2+1 that is 15, then base [15] [20] and counter [15]!

    Thank you very much to two of you!

Similar Threads

  1. Skype 4.2.0.169 access violation
    By Shin-Chen in forum Technology & Internet
    Replies: 6
    Last Post: 12-08-2010, 11:03 PM
  2. PHP has encountered an Access Violation at 01B9AD16
    By cornto in forum Software Development
    Replies: 4
    Last Post: 02-01-2010, 12:49 PM
  3. Access violation ... in module ntdll.dll
    By James O''''Reilly in forum Vista Help
    Replies: 4
    Last Post: 15-07-2009, 09:54 PM
  4. 0xC0000005 Access Violation Error
    By Farley in forum Operating Systems
    Replies: 5
    Last Post: 14-03-2009, 09:21 AM
  5. Access violation at address 0000702b
    By Aliya in forum Operating Systems
    Replies: 5
    Last Post: 05-01-2009, 10:25 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,062,877.13480 seconds with 17 queries