size of vector array

Started by
8 comments, last by Zahlman 16 years, 2 months ago
Is there a way for returning the size of a vector item, or complet vector array? such as class SomeClass { string name; int age; bool doTheyLikeDReamTheater; } vector <someClass> objectArray; someclass item; objectArray.pushBAck(item); sizeOf(objectArray); sizeOf (objectArray[0]; i know that these will not work,and just return the size of all the pointers from the someClass type variable, but is there a way to actually return the size of an object iniated that had a chnaging size, such as it having its own vector varible, or as the sizeOf(objectArray); example, where the size will change, Basically i want to display on the screen the amount of space a vector array is using.
Advertisement
Bookmark yourself a decent Standard C++ Library reference. There are a number of them - this just happens to be the one I use (it isn't 100% per the standard I've heard - but its close). In any case, looking through the various member functions you can see that std::vector, like all the Standard C++ Containers, has a member function size() that gives you the information you want.
vector.size() returns the number of elements there are in that vector array, im not looking for that, im looking for the number of bytes the vector array takes.

for example

class aClass
{
vector<int> numbers;
}


as you can see, an aClass object could be any size of bites, as one of its variables is a vector,

so im looking for somthing like

aClass a;

int sizeOfa = sizeOf(int)*numbers.size();

can you see what i mean now, this works, but as the class has more and more variables, it becomes a lot of code.

Im looking for a function that simply determines the size in bytes of an object,

such as getSizeOfobject(a);

It's impossible to know using standard C++. When C++ runtime allocates memory it may allocate more than you asked for. This can be fore reasons of padding, bookkeeping information, debugging or other reasons. There's no way in standard C++ to ask how much memory was actually allocated. Your implementation may not even know if it delegates memory allocations to another library or operating system functions.
C++ lacks the reflective features required to implement such a function. If you want it badly enough, you'll have to manually write it. An alternative option might be to hook into memory allocations instead.
ok, thankyou
You could always write a custom allocator for the container and track it that way.

10 second google finds this doc:
http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4079/

-me
A custom allocator will only tell you how much memory the container allocated. It won't tell you how much memory is held by the contained objects.
Quote:Original post by Palidine
You could always write a custom allocator for the container and track it that way.

10 second google finds this doc:
http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4079/

-me


I'd stronly suggest to skip that article - it was written for VC6 and using STLPort. Even though gcc 3.2 is better, gcc has known issues WRT to allocators (see below).

Here's a more recent one (PPT).
Quote:Original post by wforl
vector.size() returns the number of elements there are in that vector array, im not looking for that, im looking for the number of bytes the vector array takes.

for example

class aClass
{
vector<int> numbers;
}


as you can see, an aClass object could be any size of bites, as one of its variables is a vector,


No. Objects in C++ always have a specific size that is known at compile time.

Normal implementations of the vector hold data something like:

template <typename T>class vector {  T* allocation;  int size, // number of allocated slots actually in use      capacity; // number of slots that have been allocated  // huge amounts of member function implementation stuff,  // but no more actual data};


The thing that varies in size is the pointed-at, dynamic allocation. Its size is some multiple of sizeof(T), but the vector decides how many elements to allocate. Regardless, the size of the aClass instance is still just the size of a vector, typically 12 bytes.



The whole point of this scheme is that the vector is taking care of the allocation for you. It's not your responsibility, so why should you care, or expect to be able to find out, about the amount of allocated memory? The answer is "enough for what you are storing, and no more than a constant factor times the greatest amount you ever used or requested".

This topic is closed to new replies.

Advertisement