Error incrementing integer value

Posted on 2005-04-17
Last Modified: 2013-11-20
I have an array of objects, where the object has a private variable defined in the header file as

int _no_of_points;

In the constructor i initialise it:

_no_of_points = 0;

But then in a later function where I say


I get an error saying

Access violation reading location xxxxxxx

What on earth am I doing wrong?  I can post more code if necessary, but I would prefer it if someone could just say "you're probably locking the memory somewhere else with code like xxxxxxxx, try looking for yyyyyyyyyy" :-)


Question by:FatBoyTim
    LVL 9

    Accepted Solution

    I'd say you're calling the method that increments _no_of_points with a NULL instance pointer. Stop in debug and look at this.

    LVL 30

    Expert Comment

    Hi FatBoyTim,
    >> int _no_of_points;
    It should not be declared like that in the header file.
    It should be declared extern in the header file, and in one of your *.cpp files it should be declared without the extern.

    David Maisonave :-)
    LVL 30

    Expert Comment

    Disregard previous post.
    I didn't realize that it was been declared inside a class.
    LVL 30

    Expert Comment

    Please post your code.

    You shouldn't start your variable names with an underscore.
    Names that start with an underscore are reserved for the implementation IAW C++ standard.

    Author Comment

    Bang on Radu, thank you - I was accessing array element -14.  God, how stupid!  It's so obvious when you know the answer :-)

    Thanks for your help guys

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
    Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now