1

In my class I've got:

private:
    //...
    char** mNumber;
//...

Then I initialize it in constructor:

PhoneBook::PhoneBook()
{   
    mNumber = NULL;

}

I also have method set default:

bool PhoneBook::setDefault()
{
    lock();
    //...

    for (uint8 i = 0; i < 5; ++i)
    {
        mNumber[i] = new char[5];
        for (uint8 k = 0; k < 4; ++k)
        {
            mNumber[i][k] = '0' + k;
        }
        mNumber[i][4] = '\0';
    }
    unlock();
    return true;
}

In my program, when I want to write number (I can write up to five numbers), program should use method:

    bool PhoneBook::write(DataOutputStream& s)

{
    lock();
    //...
    unsigned long checksum = 0;

    for (uint8 j = 0; j <5; j++)
    {
        unsigned short k = 0;
        do
        {
        char number= mUserNo[j][k];
        checksum += 0x000000FF & (number>> 8); //checksum is not problem here I guess
        checksum += 0x000000FF & (number);
        s.write_int8(userNo);
        } while(mNumber[j][k++]=='\0');
    }

    s.write_uint32(checksum);
    s.flush();
    unlock();
    return (s.ok());
}

It crashes at line:

char userNo = mUserNo[j][k];

It also do not set default values (they are not displayed in application window where they are supposed to be).
Visual Studio 2010 prompt:

Unhandled exception at 0x012b6fb8 (main_app.exe) in main_app: 0xC0000005: Access violation reading location 0xfdfdcdcd.

I've tried to debug it, but can't find out what's wrong. I can only guess that it's something wrong with 2d array, since I feel not so good with them and when I've tried something similiar with 1D, it worked fine.
Please help me with code and let better understand multiD arrays in c++.
BTW I'm not allowe to use std::string. Just no, I can't use it here.

2
  • Um. did you intend mNumber[j][k++]=='\0' in your while-loop condition to be != instead, because as near as I can see that will fail on the first element. Second, 0xfdfdcdcd indicates you're accessing part of a boundary page and part of a freed page, which means you had better check all your index logic. Also, you setup mNumber in setDefault(), but use mUserNo in write(). They're different names. Either that is part of your problem, or you need to post real code. Commented Sep 19, 2014 at 7:43
  • I really suggest to use std::vector (or char mNumber[5][5] if really you don't be allowed to use it). Commented Sep 19, 2014 at 7:55

2 Answers 2

1

You only ever set mNumber to NULL so any access to memory via mNumber, mNumber [j][k] for example, is most likely going to crash with an access violation (1). You need to initialise mNumber to be a meaningful value, by using new somewhere! Better still, use a standard container like std::vector:

// the declaration of mNumber, do not initialise mNumber to NULL!
std::vector <char *> mNumber; // only need one * here!

(1) This is the most common thing to happen on Windows and Linux, some (but not many) OSes/systems may silently let you do this!

Sign up to request clarification or add additional context in comments.

Comments

0

if you need "char**" instead std::vector, you should to allocate memory for it before filling default values. Like this,

bool PhoneBook::setDefault()
{
........
mNumber = new char*[5];           // allocating memory for mNumber
for (uint8 i = 0; i < 5; ++i)
{
    mNumber[i] = new char[4];
    for (uint8 k = 0; k < 4; ++k)
    {
        mNumber[i][k] = '0' + k;
    }
    mNumber[i][4] = '\0';
}
unlock();
return true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.