Click here to Skip to main content
15,896,726 members

Access violation writing location?

StupidSteve asked:

Open original thread
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!
Tags: C++, C, Visual C++ 10.0

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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