• Advertisement

Archived

This topic is now archived and is closed to further replies.

C++ Question: How to retrieve the number of elements of an array of dynamic objects

This topic is 5617 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In Java, this is simply done by using arrayname.size() or so, right? The method I use in C++ is sizeof(arrayname)/sizeof(datatype) where datatype is int. Well, the problem with this method is that it''s not flexible. The sizes of datatypes are defined by the compiler and so differ from system to system (or is it the OS?), right? On my system, running Win98, sizeof(int) is 4. If I compile the ocde on another system, I guess things may go wrong. So my question is, is there any other method (which definitely has to be flexible) to get the number of elements of an array of dynamic objects? NOTE: The size of the array is not predetermined; like an array of values to be loaded from a file (sorta).

Share this post


Link to post
Share on other sites
Advertisement
well, when I want to count the number of chars in a string, I do a simple while(1) loop until an element returns a NULL. This may or may not work for you, since you may want them to be NULL or whatever. There are so many strange things you can do in programming. This works for me though. Hope it helps.

"What is darkness? It doesn''t exist where there''s light, and without light, it''s nothing..."

Share this post


Link to post
Share on other sites
If the array has been allocated with new, malloc or anything like that ... you don''t really have an array, but a pointer to dynamically allocated data. In which case, recovering the size is strictly impossible. sizeof(pointer) is constant regardless of the number of elements (though the exact size is platform-specific).

HOWEVER, if you''re using a C++ std::vector (standard dynamic array class), then its size is (surprise !) arrayname.size(). std::vector is probably closest to Java arrays ... except they are type-checked (e.g. std::vector<int>).

Note - C99 adds dynamic arrays and fixed-size types (int32_t ...) but that''s probably irrelevant here.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
If you overload operator new, you can do it like this:


  
void* operator new(int nSize)
{
int* pRet;

pRet = (int*)malloc(nSize+sizeof(int));
*pRet = nSize;
return pRet+1;
}
int GetArrayLen(void* pArray)
{
return ((int*)pArray)[-1];
}



Share this post


Link to post
Share on other sites
quote:
Original post by Evil Bill
If you overload operator new

If you overload operator new, it has to be for the serious intent of providing some custom memory allocation schema, not for whimsical fancies such as finding out the length of an array. Abusing a facility in such a manner creates more problems than it solves, which you''ve nicely illustrated for us by overloading the wrong operator, and failing to take account of alignment.

We already have std::vector, which is the modern-day antidote to the "well, it seemed a good idea in the 70''s" C-style array.

Share this post


Link to post
Share on other sites
True, i forgot about alignment, and yes, it does usually cause more problems than it solves. What do you mean i overloaded the wrong operator? I don''t understand ...

Share this post


Link to post
Share on other sites
quote:
Original post by Evil Bill
What do you mean i overloaded the wrong operator? I don''t understand

Big hint: you don''t use operator new to allocate arrays.

Share this post


Link to post
Share on other sites

Gobsmacked me''self - now I''ve split me lip

Use a std:vector for little things that don''t get moved around much and a std:list for whopping great mother-arrays that get chopped and changed a lot. Hell use a dequeue if you want to pop things on\off fronts and backs.

Use STL - you know it makes sense (else you might have to return the size of your object in the first 4 bytes of your array by hacking operator NEW or something equally NASTY like that!).

Share this post


Link to post
Share on other sites
SabreMan: Doh! I'm having a bad day (thats my excuse anyway)

Edit: Well, it should work. If you only want one object. And if you don't want it aligned. Oh, and you don't delete it either... But apart from that, it'll tell you the size of the object (not that its incredibly useful)



[edited by - Evil Bill on December 3, 2002 12:10:25 PM]

Share this post


Link to post
Share on other sites

  • Advertisement