Thread: sizeof and pointers

  1. #1
    Registered User
    Join Date
    Nov 2003

    sizeof and pointers

    I have a class Item
    class Item
    	DWORD originalValue;
    	DWORD changedValue;
    	DWORD offset;
    	Options *optionSwap; 
    	int optionCount;
    	WCHAR *originalName;
    	WCHAR *changedName;
    	ItemType itemType;
    	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
    class Page  
    	bool AddItem(WCHAR* itemName, DWORD itemValue, DWORD itemOffset);
    	bool ChangeItem(int itemNumber, WCHAR* itemNewName, DWORD itemNewValue); //by Number
    	bool AddItem(WCHAR* itemName, DWORD itemOffset);
    	bool AddOption(int itemNumber, WCHAR* optionName, DWORD optionValue );
    	bool ChangeItem(int itemNumber, int optionNumber);
    	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);
    		itemCount = 0;
    		items = (Item*) malloc(sizeof(Item));
    	virtual ~Page();
    and finally the AddItem function
    bool Page::AddItem(WCHAR *itemName, DWORD itemValue, DWORD itemOffset) //BYTESWAP
    	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
    wont there be problems with the sizeof thing?
    or should I use instead of

    itemCount * sizeof(Item)
    use something like

    sizeof(items) + sizeof(Item)
    and then for the pages the same

    realloc(pages, sizeof(pages) + sizeof(Page))

    if you need any extra information just lemme know

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    - 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.


  3. #3
    Registered User
    Join Date
    Nov 2003
    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
    HRESULT CXBFont::DrawText( FLOAT fOriginX, FLOAT fOriginY, DWORD dwColor,
                               const WCHAR* strText, DWORD dwFlags )

  4. #4
    Registered User
    Join Date
    Nov 2003
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to sort an array of pointers to structure
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 06-30-2008, 02:52 PM
  2. Replies: 2
    Last Post: 06-06-2008, 10:10 AM
  3. Malloc,calloc..Sscanf.
    By ozumsafa in forum C Programming
    Replies: 22
    Last Post: 07-26-2007, 01:09 AM
  4. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 07:23 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM