Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C VC10.0
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:
/*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...
 
#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 10-Apr-11 1:14am
Edited 10-Apr-11 4:00am
v2
Comments
mbue at 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 at 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 at 10-Apr-11 15:50pm
   
browse my comments to the solutions... hope it clarifies a little more...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
Albert Holguin at 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 at 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 at 10-Apr-11 21:50pm
   
no prob, happy to help
StupidSteve at 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 at 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
krmed at 10-Apr-11 22:13pm
   
If your function is working on the entire array it needs the address of thefirst element. That's normally the case.
Glad I could help.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This line is incorrect:
   for(i=0; i<=MAXLINE; ++i)
It should be:
   for(i=0; i<MAXLINE; ++i)
  Permalink  
Comments
StupidSteve at 10-Apr-11 6:34am
   
Thank you very much but I forgot to inform you that my original sentence was for(i=0; i
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Comments
Albert Holguin at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 10 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100