SImple question about structure and linked list

J

John

Thanks a lot.

How about "Vector"? If I put a Vector in a structure, the size of the
structure may dynamically change, right?

John
 
D

Daniel T.

How about "Vector"? If I put a Vector in a structure, the size of the
structure may dynamically change, right?

int main() {
vector< int > v;
unsigned initial_size = sizeof( v );
for ( int x = 0; x < 1000; ++x )
v.push_back( x );
assert( initial_size == sizeof( v ) );
}

Since the vector itself doesn't change size, I would expect that any
structure that contains a vector would not change size either.
 
G

Gregg

(e-mail address removed) (John) wrote in
How about "Vector"? If I put a Vector in a structure, the size of the
structure may dynamically change, right?

I assume you mean the standard C++ class "vector" (C++ is case-sensitive).
The answer is still no, but you must be precise in what you mean by size of
the structure. Do you mean the size of the overall data structure in memory
or just the size of the struct containing the vector? I assume you mean the
latter.

The reason these can be different is because the memory managed by a vector
"v" does not have to be directly contained within the variable "v". THe
variable "v" has only to have enough space to store information required
for the bookkeeping of this memory, stored elsewhere.

As a result, the memory managed by the vector is also not generally
contained wihtin the struct that has the vector as a field.

Gregg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,463
Latest member
FinleyMoye

Latest Threads

Top