views:

120

answers:

8

Suppose I have a class

class Foo {

:
:

}

I have another function

void getf( Foo &f) {

:
:

std::cout<<sizeof f<<std::endl;
}

After I process the data and assign a lot of data to f (vector included in Foo members), I need the size of f object

However, as what I did above, I always get 16, which is the size of a reference here.

Did I do anything wrong? How to do that?

Thanks!

+8  A: 

sizeof returns the size of a type. Often time, as in the case of a Vector, the size of the type does not necessarily include the cumulative size of everything the type may point to. For example, the type:

class Foo {
   char* chars
}

... will exhibit the same sizeof results whether chars points to a single byte or it points to a 40Kb string.

kbrimington
+3  A: 

According to this IBM reference, sizeof applied to a reference returns the size of the referenced object:

The result is the size of the referenced object.

So, I believe the problem is not that sizeof is returning the size of the reference itself, but instead that Foo holds pointers to other types.

Also keep in mind that sizeof will not tell you the size of data inside a vector (or any other container that uses the heap). Consider the following example:

struct Foo {
    std::vector<int> v;
}

int main(int argc, char **argv) {
    Foo foo;
    std::cout << sizeof(foo) << std::endl;
    foo.v.push_back(1);
    std::cout << sizeof(foo) << std::endl;
}

Output:
24
24
Justin Ardini
From the C++ Standard: `5.3.3/2 sizeof: When applied to a reference or a reference type, the result is the size of the referenced type.`
Abhay
Thanks for digging that up Abhay. :)
Justin Ardini
+4  A: 

After I process the data and assign a lot of data to f (vector included in Foo members), I need the size of f object

sizeof is compile-time operation. It gives you the fixed size of the just variable or object itself. If your object has pointers to memory elsewhere (that's what vector uses internally), sizeof will never delve into them to determine the size of what they point to.

If you need some measure of the total number of bytes associated with an object an runtime, then you can't just use a simple sizeof. You would have to add up all the pieces. For instance: sizeof(f) + f.vector_member.size() * sizeof(whaterver_type_that_vector_contains).

TheUndeadFish
A: 

Can you list the structure of your class F. I am suspecting that you are getting the right size, sizeof is going to tell you the size of the class, which is the size of all members of the class (this is based on the alignment and ordering as well as your compiler). It will not tell you the size of the vector including its elements, just the size of the reference to the vector.

pstrjds
Just noticed that kbrimington posted basically the same thing. I am a slower typer :)
pstrjds
A: 

Using this test case:

class Foo 
{ 
    char x [100];  
} ;

void getf( Foo &f) 
{ 
    std::cout<<"sizeof f:" << sizeof f<<std::endl; 
} 

I am getting sizeof f:100 printed. So, you code is correct; you are just interpreting it wrong.

James Curran
+1  A: 

sizeof in this case is returning the size of your class. You say you assigned data to Foo, but you didn't define Foo. I suspect it looks somethign like this:

class Foo
{
public:
  string my_str;
  int* my_ints;
};

And then you do something like this:

Foo f;
f.my_str = "Hello, Foo.";
f.my_ints = new int[1000];

...and expect that sizeof(f) will now be the size of Foo, plus the size of the string, plus the size of the int array. But it's not. Foo never changes in size because it doesn't "have" a char array or an int array. Rather it has a string object (which in turn probably has a pointer to a char array), and a pointer to int. The memory allocated for the string and the int array isn't "in" Foo, it is pointed to by Foo.

John Dibling
A: 

If you have a class like this:

class Foo
{
    std::vector<int> intVec;

    // ....
};

the sizeof operator, which works at compile-time, will only include the size of the vector's instance data members. It will not give you the total size of the elements of the vector, which are stored on the heap somewhere and pointed to by some private member of the vector.

If you are really trying to figure out the total amount of memory used by the vector, I don't think there is a portable way to do that. The vector implementation may allocate memory it is not using, so without looking at the implementation, you don't really know what it's doing.

Kristopher Johnson
The `capacity()` method should give you a pretty good idea how much memory the vector has in store.
UncleBens
A: 

Just an additional note:

$8.3.2/3 states- "It is unspecified whether or not a reference requires storage (3.7)."

Therefore one should not always assume that reference occupies storage. sizeof when applied to a reference operand, returns the static compile time sizeof of the original referand.

Chubsdad