Topic: Total allocated (evaluated at run-time)
Author: Alex Vinokur <alexander.vinokur@telrad.co.il>
Date: 1999/02/08 Raw View
In article <787add$585@transfer.stratus.com>,
"Graham Macrae" <graham_macrae@tcam.stratus.com> wrote:
[snip]
> The sizeof() operator is evaluated at *compile-time* not run-time. (I base
> this statement on my knowledge of C not C++). This will always return you
> the same value regardless of the 'vvv.size()-1' parameter. (Try it...put
> 1234 into the expression instead - you will still get the same value
> returned).
>
[snip]
Hi,
Is there any function/operator
which calculates at run-time the total_allocated of a vector-object
(and objects of other types)?
Thanks in advance,
Alex
###############################################
Please see the following program :
###############################################
########### 1. Program ########################
###############################################
#include <string>
#include <vector>
int total_sizeof; void print_status (int step_i, const vector<string>& v_i,
char * vname_i) { cout << "--------------------------------------" << endl;
cout << "Step#" << step_i << " : " << vname_i << ".size = \t\t" << v_i.size
() << endl; cout << "Step#" << step_i << " : " << vname_i << ".capacity =
\t" << v_i.capacity () << endl; cout << "Step#" << step_i << " : sizeof ("
<< vname_i << ") = \t" << sizeof (v_i) << " \t(evaluated at compile-time)" <<
endl; if (!v_i.empty ()) { cout << "Step#" << step_i << " : sizeof (" <<
vname_i << "[" << (v_i.size () - 1) << "]) = \t" << sizeof (v_i [v_i.size ()
- 1]) << " \t(evaluated at compile-time)" << endl; cout << "Step#" << step_i
<< " : " << vname_i << "[" << (v_i.size () - 1) << "].size = \t\t" << v_i
[v_i.size () - 1].size () << endl; } cout << "Step#" << step_i << " :
total_sizeof = \t" << total_sizeof << " \t(evaluated at compile-time)" <<
endl; cout << "Step#" << step_i << " : total_allocated = \t" << "How to
get?" << " \t(evaluated at run-time)" << endl; }
#define PRINT_STATUS(step, v) print_status (step, v, #v)
//===============
int main()
{
vector<string> vvv;
string current_string;
//====================
total_sizeof = 0;
PRINT_STATUS (1, vvv);
//====================
vvv.push_back ("AAA");
total_sizeof += sizeof (vvv [vvv.size () - 1]);;
PRINT_STATUS (2, vvv);
//====================
vvv.push_back ("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
total_sizeof += sizeof (vvv [vvv.size () - 1]);;
PRINT_STATUS (3, vvv);
//====================
vvv.pop_back ();
total_sizeof -= sizeof (vvv [vvv.size () - 1]);;
PRINT_STATUS (4, vvv);
//====================
//====================
return 0;
}
###############################################
Here is the result of the running.
###############################################
########### 2. The result of the running ######
###############################################
--------------------------------------
Step#1 : vvv.size = 0
Step#1 : vvv.capacity = 0
Step#1 : sizeof (vvv) = 12 (evaluated at compile-time)
Step#1 : total_sizeof = 0 (evaluated at compile-time)
Step#1 : total_allocated = How to get? (evaluated at run-time)
--------------------------------------
Step#2 : vvv.size = 1
Step#2 : vvv.capacity = 1
Step#2 : sizeof (vvv) = 12 (evaluated at compile-time)
Step#2 : sizeof (vvv[0]) = 4 (evaluated at compile-time)
Step#2 : vvv[0].size = 3
Step#2 : total_sizeof = 4 (evaluated at compile-time)
Step#2 : total_allocated = How to get? (evaluated at run-time)
--------------------------------------
Step#3 : vvv.size = 2
Step#3 : vvv.capacity = 2
Step#3 : sizeof (vvv) = 12 (evaluated at compile-time)
Step#3 : sizeof (vvv[1]) = 4 (evaluated at compile-time)
Step#3 : vvv[1].size = 50
Step#3 : total_sizeof = 8 (evaluated at compile-time)
Step#3 : total_allocated = How to get? (evaluated at run-time)
--------------------------------------
Step#4 : vvv.size = 1
Step#4 : vvv.capacity = 2
Step#4 : sizeof (vvv) = 12 (evaluated at compile-time)
Step#4 : sizeof (vvv[0]) = 4 (evaluated at compile-time)
Step#4 : vvv[0].size = 3
Step#4 : total_sizeof = 4 (evaluated at compile-time)
Step#4 : total_allocated = How to get? (evaluated at run-time)
##########################################
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]