Can C ++ variables vary in size?

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 is the variable s

resized?

+2


source to share


7 replies


It depends on what you mean by "size".

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



However, the heap size will vary depending on two cases.

What do you want to do with the information?

+3


source


I will say yes and no. s will be the same string instance, but the internal buffer (which is preallocated depending on your STL implementation) will contain a copy of the constant string that you want to influence it. If the constant string (or any other char * or string) is larger than the internal preallocated buffer s, the s buffer will be reallocated depending on the string buffer reallocation algorithm implemented in your STL implantation.



+2


source


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

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

HOWEVER, s memory size can change at runtime using heap allocations. Since you are assigning a large string to s, its memory size will increase because more heap allocated space is likely to be required. You should probably try and specify what you want.

+2


source


And exactly, exactly. The s variable refers 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
}

      

Change, correct some details and clarify the point

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

The assignment causes the original content to be discarded and the content on the right side of the operation is copied into the object. similar to doing s.assign ("greater than"), but assignment has a wider range of parameters.

To get the original question, the contents of the s object can be of variable size. See http://www.cplusplus.com/reference/string/string/resize/ for details .

+1


source


The variable std::string

never changes its size. It just refers to a different piece of memory with different sizes and different data.

+1


source


A variable is an object that we refer to by name. The "physical" size of the object - sizeof (s) in this case - never changes. They are still std :: string, and the size of std :: string is always constant. However, things like strings and vectors (and other containers, for that matter) have a "boolean size" that tells us how many elements of some type they hold. The string "logically" stores characters. I say "logically" because the string object contains no actual characters. It usually only has a couple of pointers as "physical members". Since string objects manage a dynamically allocated array of characters and provide correct copy semantics and easy access to characters, we can use these characters as members ("gates "). Since growing a row is a matter of memory reallocation and update pointers, we don't even need to resize (s).

+1


source


I would say it is a string object and it has the ability to grow dynamically and vice versa

0


source







All Articles