tags:

views:

215

answers:

7

In the following C++ program:

#include <string>
using namespace std;

int main()
{
    string s = "small";
    s = "bigger";
}

is it more correct to say that the variable s has a fixed size or that the variable s varies in size?

+1  A: 

Neither, exactly. The variable s is referring to a string object.

#include <string>
using namespace std;

int main()
{
    string s = "small"; //s is assigned a reference to a new string object containing "small" 
    s = "bigger"; //s is modified using an overloaded operator
}

Edit, corrected some details and clarified point

See: http://www.cplusplus.com/reference/string/string/ and in particular http://www.cplusplus.com/reference/string/string/operator=/

The assignment results in the original content being dropped and the content of the right side of the operation being copied into the object. similar to doing s.assign("bigger"), but assign has a broader range of acceptable parameters.

To get to your original question, the contents of the object s can have variable size. See http://www.cplusplus.com/reference/string/string/resize/ for more details on this.

Jonathan Fingland
If s contains a reference, then it's fixed size, right?
Tarquila
I don't think I entirely understand your answer. The `s = "bigger";` statement does not create a new `std::string` object in C++, it only reallocates `s`'s internal buffers and copies the new value into them. What did you mean?
Kim Gräsman
OK, after your additional information, I can say that this is just not true. -1.
Kim Gräsman
@Kim: The reallocation may yield an object of different size.
Michael Foukarakis
As I said in my answer, it depends on what you mean :) sizeof(s) will yield the same result for both s's, but the second s will consume more memory from the heap.
Kim Gräsman
Kim, sizeof is only going to tell you the size on the stack, not the heap where the string data is stored as many others have pointed out. See my edit on the resize method. My inarticulate point before was that the contents on the heap are replaced, not modified. The object itself is the same, just the location on the heap is reassigned to the new string value and not simply modified.
Jonathan Fingland
@Jonathan -- yes, that I agree with!
Kim Gräsman
+3  A: 

It depends on what you mean by "size".

The static size of s (as returned by sizeof(s)) will be the same.

However, the size occupied on the heap will vary between the two cases.

What do you want to do with the information?

Kim Gräsman
Right, s is not on the heap. There must be something in s that points to stuff on the heap, but that doesn't affect the size of s - correct?
Tarquila
Nope, s is always the same size (per sizeof) -- it contains a pointer to the "stuff on the heap" but that pointer is always the same size.
Kim Gräsman
@Tarquila: That is correct. The object itself is always the same type. But it contains one or more pointers to dynamically allocated data (on the heap), which may be reallocated to change the size.
jalf
@Tarquila: I would say that Kim's answer is fine except for that "It depends". I don't think it does. "s" (which is a std::string) is of a fixed size. However it manages / references / points to an array of a variable size, but that no more makes "s" variable size than it makes an old fashioned char* pointer of variable size.
AAT
By "It depends" I meant, "It depends on what you mean" -- I'll edit the post to reflect that.
Kim Gräsman
I would also add that of course, sizeof(s) is implementation dependent and may vary from one STL to another (notably if they choose to keep track of the size or recompute it each time or if they try to implement the infamous copy on write).
Matthieu M.
+2  A: 

i'll say yes and no. s will be the same string instance but it's internal buffer (which is preallocated depending on your STL implementation) will contain a copy of the constant string you wanted to affect to it. Should the constant string (or any other char* or string) have a bigger size than the internal preallocated buffer of s, s buffer will be reallocated depending on string buffer reallocation algorithm implemented in your STL implmentation.

dweeves
+2  A: 

This is going to lead to a dangerous discussion because the concept of "size" is not well defined in your question.

The size of a class s is known at compile time, it's simply the sum of the sizes of it's members + whatever extra information needs to be kept for classes (I'll admit I don't know all the details) The important thing to get out of this, however is the sizeof(s) will NOT change between assignments.

HOWEVER, the memory footprint of s can change during runtime through the use of heap allocations. So as you assign the bigger string to s, it's memory footprint will increase because it will probably need more space allocated on the heap. You should probably try and specify what you want.

Falaina
I said the size of the variable s - that's pretty clear I think.
Tarquila
Well the size of the varaible s on the stack is going to be constant. Assigning new values to it's fields aren't going to cause any change in size. HOWEVER, s most likely has internal buffers which may or may not be references to other pieces of memory. This may or may not be on the stack. It's not clear if you're asking for the space s takes up on the stack, or the total amount of space s will take up in your program's memory.
Falaina
I think s will *cause* extra memory to be taken up, but that doesn't mean that s itself is taking it up.
Tarquila
@Tarquila: If s isn't taking it up, then what is?
Bill
The objects which the internals of s point at.
Tarquila
A: 

i would say this is string object , And it has capability to grow dynamically and vice-versa

sat
+1  A: 

The std::string variable never changes its size. It just refers to a different piece of memory with a different size and different data.

sbi
+1  A: 

A variable is an object we refer to by a name. The "physical" size of an object -- sizeof(s) in this case -- doesn't change, ever. They type is still std::string and the size of a std::string is always constant. However, things like strings and vectors (and other containers for that matter) have a "logical size" that tells us how many elements of some type they store. A string "logically" stores characters. I say "logically" because a string object doesn't really contain the characters directly. Usually it has only a couple of pointers as "physical members". Since the string objects manages a dynamically allocated array of characters and provides proper copy semantics and convenient access to the characters we can thing of those characters as members ("logical members"). Since growing a string is a matter of reallocating memory and updating pointers we don't even need sizeof(s) to change.

sellibitze