Click here to Skip to main content
16,015,351 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
When running another piece of my new program, the debugging is okay but after that it reported a "Access violation writing location...".

Using the breakpoint function, I find that the following function has something wrong:
C#
/*variable initialization function*/
void initialize (int temp[MAXLINE], int digit, int nd)
{
    int i;
    for(i=0; i<=MAXLINE; ++i)
    {
        temp[i]=0;
    }
    digit=nd=0;
}


In fact, the error report appears right after the sentence "temp[i]=0;".

Could anybody help me with it? I know that access violation means that the data address is not correct but I just cannot figure out what's the problem...

Here i post all of the code in this program... I hope someone could tell me what's wrong with it...

C#
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 10000

void printarray (int array[], int digitnumber);
void initialize (int array[], int digit, int nd);

main()
 {

    int digit, temp[MAXLINE];
    int nd,i ;

    nd=digit=0;
    for(i=0; i<MAXLINE; ++i)
    {
        temp[i]=0;
    }

    while((nd=getchar())!=EOF)
    {
        if(nd!='\n')
        {
            temp[digit]=nd;
            ++digit;
        }
        else if (digit>=80)
        {
            printarray(temp[MAXLINE], digit);
            initialize(temp[MAXLINE], digit, nd);
        }
        else initialize(temp[MAXLINE], digit, nd);

    }
    return 0;
}

/*printarray function*/
void printarray (int array[MAXLINE], int digitnumber)
{
    int i;

    for(i=0; digitnumber>0; --digitnumber)
    {
        putchar(array[i]);
        ++i;
    }
    printf("\n");
}

/*variable initialization function*/
void initialize (int array[MAXLINE], int digit, int nd)
{
    for(digit; digit>0; --digit)
    {
        array[digit]=0;
    }
    digit=nd=0;
}



I also noticed that the K&R says:
"When the name of an array is used as an argument, the value passed to the function is the location or address of the beginning of the array-- there is no copying of array elements."

Does my mistake has anything to do with the use of array?...Thanks for your help!
Posted
Updated 10-Apr-11 3:00am
v2
Comments
mbue 10-Apr-11 7:22am    
If your initialisation should anything do for you - use pointers or references. All parameters are allocated on stack and freed after return.
Regards
StupidSteve 10-Apr-11 7:35am    
Thank you very much and I know that pointer is more powerful when manipulating data----the problem is i am following the K&R C bible and i am forcing myself to achieve this function using very basic sentences....
Albert Holguin 10-Apr-11 15:50pm    
browse my comments to the solutions... hope it clarifies a little more...

The problem lies in this area:
while((nd=getchar())!=EOF)
{
    if(nd!='\n')
    {
        temp[digit]=nd;
        ++digit;
    }
    else if (digit>=80)
    {
        printarray(temp[MAXLINE], digit);
        initialize(temp[MAXLINE], digit, nd);
    }
    else initialize(temp[MAXLINE], digit, nd);
}
Your call to printarray and initialize are passing the VALUE stored at temp[MAXLINE], but you should be passing the starting address of the array.

Try this instead
while((nd=getchar())!=EOF)
{
    if(nd!='\n')
    {
        temp[digit]=nd;
        ++digit;
    }
    else if (digit&gt;=80)
    {
        printarray(temp, digit);
        initialize(temp, digit, nd);
    }
    else initialize(temp, digit, nd);
}


Hope that helps.
 
Share this answer
 
Comments
Albert Holguin 10-Apr-11 15:41pm    
krmed is correct, and so is Hans, but this error would kick up an error first, the problem that Hans pointed out wouldn't kick up an error until you go out of bounds of your array.
StupidSteve 10-Apr-11 21:42pm    
Thank you Albert! Now i understand your points:)

Thank you so much that you helped me so many times!~
Albert Holguin 10-Apr-11 21:50pm    
no prob, happy to help
StupidSteve 10-Apr-11 21:41pm    
Thank you so much Krmed! I've tried you method and it worked out okay~

After that i improved my program by adding a sentence "printf("\n");"right after defining int in the printarray function.

so next time when I want to call an array into a function i have to pass the address of the first element in that array, am i right?
Albert Holguin 10-Apr-11 21:49pm    
well you pass the address to the array, which is technically the address of the first element of the array, but all you really have to remember is its the address to the array
This line is incorrect:
for(i=0; i<=MAXLINE; ++i)

It should be:
for(i=0; i<MAXLINE; ++i)
 
Share this answer
 
Comments
StupidSteve 10-Apr-11 6:34am    
Thank you very much but I forgot to inform you that my original sentence was for(i=0; i
Hi,
You access the array out of bounds, so it's kind of strange the introducing strictly less condition didn't change anything. Also ignore my previous solution as it wasn't correct.
Regards
 
Share this answer
 
v2
Comments
Albert Holguin 10-Apr-11 15:42pm    
he's passing the pointer to the array incorrectly... see krmeds solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900