Thread: Size of an object array

  1. #1
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120

    Size of an object array

    I'm trying to calculate how many members my dynamically allocated object array has and am having difficulty!

    If I was to do it with an array of, say, integers, I would do it like this:

    Code:
    int *i;
    ...
    i = new int[23];
    ...
    int nMembers = sizeof(i) / sizeof(int);
    This does not work with my class object though. Instead, sizeof(MyObject) returns 4 bytes, the size of the pointer itself. My object is defined like this:

    Code:
    class Card
    {
    	int m_pip;
    	SUIT m_suit; // SUIT is an enumerated type
    
    	public:
    		BOOL m_error;
    		Card(int pip = 0, SUIT suit = NO_SUIT);
    		~Card(void) {m_pip = 0; m_suit = NO_SUIT;}
    
    		int GetPip(void){return(m_pip);}
    		SUIT GetSuit(void){return(m_suit);}
    		int SetCard(int pip, SUIT suit);
    		int GetIndex(void);	
    
    		void GetText(char *txt);
                    void GetShortText(char *txt);
    };
    and I am trying to calculate the size of an array of these objects like so:

    Code:
    Card *m_card;
    ...
    m_card = new Card[52];
    ...
    int nMembers = sizeof(m_card) / sizeof(Card);
    Can anyone see where I am going wrong? Or know a decent method?

    Thanks in advance,

    DT
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Will this one work?
    Code:
    int nMembers = sizeof(*m_card) / sizeof(Card);
    otherwise
    Code:
    Card m_card[52];
    int nMembers = sizeof(m_card) / sizeof(Card);
    Last edited by alphaoide; 08-27-2004 at 05:33 PM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Try putting '&' before m_card, that should return the size of it, if not, try * like alpha said.

  4. #4
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Thanks guys, tried both and they don't work. Using '*' returns the size of the first member of the array (the size of the card object, which is closer). Using '&' just returns the size of a pointer to a pointer (4 bytes).

    Also, Card m_card[52] isn't practical as the array needs allocating at runtime (different size arrays etc.)

    I'm guessing C++ can't return the size when there is an array of class objects (as opposed to basic types)? Does anyone know of an entirely alternative method?
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  5. #5
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    I dont know of another way, but I think I may know how to fix the problem. you said * returned the first value's size, try '*m_card[]', like with delete, it may return the whole array. I dont know, I havent tried this before, but it may work.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I'm guessing C++ can't return the size when there is an array of class objects

    that's not true at all. the problem is, sizeof() does not work as expected on dynamically allocated arrays of ANY type (it reports the sizeof a pointer, not the block of memory). unfortunately, normal arrays passed to functions exibit the very same behaviour, so using sizeof() to calculate array dimensions is somewhat limited. you'll just have to keep a tally yourself, or else use an std::vector of Cards - the size() member function will return the number of elements in use.

    btw:

    >> int nMembers = sizeof(*m_card) / sizeof(Card);

    sizeof(*m_card) == sizeof(Card), so...that won't work either.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    >> int nMembers = sizeof(*m_card) / sizeof(Card);

    sizeof(*m_card) == sizeof(Card), so...that won't work either.

    doh, that was a typo, forgot to delete it. But thanks anyway.

    I've actually been keeping a safe tally, I just wanted a double safe check on it for an error checking function, not really neccessary but would have been nice.

    Many thanks, dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I just wanted a double safe check on it for an error checking function
    No biggie:
    Code:
    Card *m_card;
    ...
    const int nMembers = 52;
    m_card = new Card[nMembers];
    Of course, and you should be expecting this if you've been around here for more than half a day, std::vector is much more friendly when it comes to things like this:
    Code:
    std::vector<Card> m_card(52);
    Then you can use m_card.size().
    My best code is written with the delete key.

  9. #9
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Brilliant, many thanks Prelude, very handy indeed.

    So basically, std::vector is the easiest/ safest way for dynamic memory alloc?
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So basically, std::vector is the easiest/ safest way for dynamic memory alloc?
    At the risk of being corrected for lack of accuracy, yes.
    My best code is written with the delete key.

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>>So basically, std::vector is the easiest/ safest way for dynamic memory alloc?
    >At the risk of being corrected for lack of accuracy, yes.

    What she REALLY means is 'no'. There's lots of different containers like vectors that can be used for dynamic alloc. To name the ones I know, in order of importance:

    vector - standard wrapper for a dynamic array, as far as I know - good for random access
    list - standard implementation of a linked list - good for inserting/deleting, looping through
    (multi)map - standard implementation of a binary tree, as far as I know - good for.. well, mapping and searching.
    deque - standard implementation of who knows what, but Prelude likes it
    (multi)set - no idea how it's implemented, but I've used it once in some obscure way...
    queue - standard implementation of a queue - didn't know it existed for a long time...
    stack - standard implementation of a stack - didn't know it existed for a long time...

    There might be more, but those are the ones I can think of right now.
    Last edited by Hunter2; 08-29-2004 at 03:46 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Hunter2
    (multi)set - no idea how it's implemented, but I've used it once in some obscure way...
    A Set allows only one instance in the container. So if you added 3, 4, 5, 4, 3, 5, 5, 3, 4 to your set, it would contain (3, 4, 5)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  2. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  3. Maximum array size?
    By code2big in forum C Programming
    Replies: 2
    Last Post: 05-25-2004, 08:16 AM
  4. Class member array size with constuctor initalizer
    By rafe in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2002, 10:09 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM