I have a class Item
Code:
class Item
{
public:
DWORD originalValue;
DWORD changedValue;
DWORD offset;
Options *optionSwap;
int optionCount;
WCHAR *originalName;
WCHAR *changedName;
ItemType itemType;
Item();
Item(const Item&);
virtual ~Item();
};
I know it's not good to make them all public
then we have the class Page which uses the items
Code:
class Page
{
private:
//BYTESWAP
bool AddItem(WCHAR* itemName, DWORD itemValue, DWORD itemOffset);
bool ChangeItem(int itemNumber, WCHAR* itemNewName, DWORD itemNewValue); //by Number
//OPTIONSWAP
bool AddItem(WCHAR* itemName, DWORD itemOffset);
bool AddOption(int itemNumber, WCHAR* optionName, DWORD optionValue );
bool ChangeItem(int itemNumber, int optionNumber);
public:
Item *items;
int itemCount;
bool ParseItem(WCHAR* itemLine); //AUTO CHECKS BETWEEN BYTE/OPTION
WCHAR* ItemName(int itemNumber);
DWORD ItemValue(int itemNumber);
WCHAR* ItemCurrentName(int itemNumber);
DWORD ItemCurrentValue(int itemNumber);
Page()
{
itemCount = 0;
items = (Item*) malloc(sizeof(Item));
}
virtual ~Page();
};
and finally the AddItem function
Code:
bool Page::AddItem(WCHAR *itemName, DWORD itemValue, DWORD itemOffset) //BYTESWAP
{
itemCount++;
if ( realloc(items, itemCount * sizeof(Item)) == NULL )
{
itemCount--; //MEMORY FULL
return false;
}
swprintf( items[itemCount-1].originalName, itemName );
swprintf( items[itemCount-1].changedName, itemName );
items[itemCount-1].originalValue = itemValue;
items[itemCount-1].changedValue = itemValue;
items[itemCount-1].offset = itemOffset;
items[itemCount-1].itemType = BYTESWAP;
return true;
}
my question was, will there be any problems when reallocating items and in the program I have an array of the Pages type which gets realloced too
if page1 has 3 items
page2 has 5 items
etc
wont there be problems with the sizeof thing?
or should I use instead of
Code:
itemCount * sizeof(Item)
use something like
Code:
sizeof(items) + sizeof(Item)
and then for the pages the same
Code:
realloc(pages, sizeof(pages) + sizeof(Page))
if you need any extra information just lemme know