-
sizeof and pointers
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 :p
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 :)
-
- Just use "<count> * <sizeof object>" to allocate space for count objects (like you have it).
- This is wrong: swprintf(items[itemCount-1].originalName, itemName ); originalName is an uninitialized pointer (that goes for all your pointers in Item).
- Make your life alot easier and use std::wstring instead of WCHAR*
- Make your life alot easier and use STL containers instead of fixed arrays that have to be realloc'ed over and over.
gg
-
1) thx
2) the WCHARs are made in the Constructor part
3) I use them because the print function uses WCHAR* otherwise I have to conver them before I print them
4) could you give some more information about the last one?
like a tutorial or something :)
btw this is an xbox application and can only use DirectX
this is the DrawText thingy
Code:
HRESULT CXBFont::DrawText( FLOAT fOriginX, FLOAT fOriginY, DWORD dwColor,
const WCHAR* strText, DWORD dwFlags )
-
about answer 4, searched for it and found lots of stuff about vector, it's looking so cool :)
thx alot will try the code with vectors and lett you know
thx again :D