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.
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 setupmNumberinsetDefault(), but usemUserNoinwrite(). They're different names. Either that is part of your problem, or you need to post real code.std::vector(orchar mNumber[5][5]if really you don't be allowed to use it).