0
$\begingroup$

I'm playing around with C++ and was looking at how to loop through an array of unknown length. The following code was suggested and works perfectly from the little testing I've done:

std::string words[5] = {"Lorem ipsum dolor sit amet orci aliquam.", "Hi", "Welcome", "World", "LoopyDoopyDoo"};
for (int i = 0; i < sizeof(words)/sizeof(words[0]); i++){
     DO STUFF
}

From what I've read the sizeof() operator returns the size of a variable in bytes.

What I don't understand is how this isn't affected by the number of characters in each element of the array. I printed out the sizeof() value to check and the array size is always evaluated at 160 bytes and element 0 as 32 no matter how many characters are present. I thought that it might be allocating 32 bytes but not using them all so I stored some text that should use 40 bytes, but still no change. Am I missing something here? Is text stored differently in C++?

Any help in understanding how this actually works would be appreciated, thanks!

$\endgroup$
4
  • 1
    $\begingroup$ Each std::string doesn't contain the string itself. They contain a pointer to the first element of the array of char. $\endgroup$ – plop May 5 at 16:05
  • $\begingroup$ I'm still a noob when it comes to C++, it's lower level than other languages I've used so I'm not used to a lot of the syntax, etc. Are you saying that each element in the array isn't actually a string of characters, but rather a pointer to the string in memory and as such each pointer is 32 bytes irrespective of the actual values size? $\endgroup$ – TheArrowhead 984 May 5 at 16:13
  • 1
    $\begingroup$ Each element is an instance from the class std::string. They are not themselves pointers, but among the members that its instances manage there is a pointer to the actual array of char that contains the string. $\endgroup$ – plop May 5 at 16:44
  • $\begingroup$ @plop Make that an answer? $\endgroup$ – Juho May 5 at 18:17
1
$\begingroup$

C and C++ are low-level languages that give the programmer precise control over the memory layout. Each object takes a constant amount of memory which is fully determined (on a given implementation) by its type. The sizeof operator lets programs know this size.

An object may contain pointers. The data that the pointers points to, if any, is not part of the object. The size of an object is what some higher-level language might call the “shallow” size: it only considers the object itself, not other objects that the object references.

For example, an object of type std::string has a certain size. This size is independent of the content of the string. This means that the only way to implement std::string for strings of unbounded length is for the std::string object to contain a pointer to the actual content of the string. sizeof(std::string) is the size of this pointer plus other data that the std::string type keeps track of (that would typically be the length of the string, the size allocated for the string, a pointer to the class's method table). It has no relation with the length of the string.

An array is an object which consists of several objects of the same type. Since the elements of the array all have the same type, they all have the same size.

$\endgroup$
1
  • $\begingroup$ Ah, that makes sense. Thanks for the time you spent answering, it is much appreciated! $\endgroup$ – TheArrowhead 984 May 7 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.