C++ Sizeof( String )

c++ sizeof( string )

It isn't clear from your example what 'string' is. If you have:

#include <string>
using namespace std;

then string is std::string, and sizeof(std::string) gives you the size of the class instance and its data members, not the length of the string. To get that, use:

string s;
cout << s.size();

Sizeof string vs sizeof string pointer

The size of a pointer is always the size of the pointer itself, not what it points to. That's because sizeof is mostly a compile-time operator (the result is evaluated by the compiler) and the compiler can't know what a pointer might point to at run-time.

As for sizeof *title it's the same as sizeof title[0] which is a single char. And the size of a char is 1 (it's specified to always be 1 by the way, no matter the actual bit-width).

Lastly about sizeof "VP". In C all literal strings are really arrays of characters, including the terminating null character. So the literal string "VP" is an array of three characters, hence its size is 3.


To make the answer a little bit more complete, I say that the sizeof operator is mostly compile-time. That of course can't be true for variable-length arrays, where the compiler must insert code to store the actual size of the array in a way that it can be fetched at run-time. If the array decays to a pointer, then all you have is the pointer and again sizeof returns the size of the pointer itself.

And a note about string literal arrays. While they are technically non-constant arrays, they still can't be modified. Attempting to modify a string literal leads to undefined behavior. Literal strings are thus, in effect, read-only.

How to get the string size in bytes?

You can use strlen. Size is determined by the terminating null-character, so passed string should be valid.

If you want to get size of memory buffer, that contains your string, and you have pointer to it:

  • If it is dynamic array(created with malloc), it is impossible to get
    it size, since compiler doesn't know what pointer is pointing at.
    (check this)
  • If it is static array, you can use sizeof to get its size.

If you are confused about difference between dynamic and static arrays, check this.

sizeof for a string in array of strings

It's happening because sizeof(words[27]) is giving the size of a pointer and words[27] is a pointer, and pointers have a fixed size of each machine, mostly 8 bytes on a x86_64 architecture CPU. Also, words is an array of pointers.

each of the character arrays occupy 8 bytes, including the character array "optimization".

No, each word in words is occupying a fixed memory (their length), 8 bytes is the size of pointer which is unsigned long int, it stores the address of the word in words.

const int length = sizeof(words)/sizeof(words[0]);

The above line gives 35 because words is not decayed as a pointer, it is stored in the program's data section, because it's a global variable.

Read More about pointer decaying:

  1. https://www.geeksforgeeks.org/what-is-array-decay-in-c-how-can-it-be-prevented/
  2. https://www.opensourceforu.com/2016/09/decayintopointers/

Size of string vs size of char array in c++

std::string is a class object in C++. The size of it is implementation-defined, but will generally require space for at least both a pointer to a heap-allocated string, and a size. The sizeof(std::string) you are seeing may be specific to your implementation -- but it could be partially due to a small-buffer optimization where small strings are stored directly in the string.

An array of chars is just an array of characters, so sizeof(arr) will always be the the number of characters.


As for whether you can use std::string: it depends on how constrained you are. std::string will often use heap memory for larger strings, which may be hard to justify on extremely constrained systems. However, most std::string implementations also have support for small-buffer-optimized strings, where these strings are stored directly in the std::string rather than indirectly via heap memory

If heap memory is not justifyable, std::string will not be a good solution for you.

However, if that's the case, I would recommend you look into either adopting an existing static_string implementation that encodes the size as a template argument, or at least writing your own. This would be better than using char arrays everywhere, since this becomes hard to manage long-term.

C program - sizeof string with \0 characters

sizeof does not determine the length of the string. It determines how many bytes a structure takes in memory.

In your case, the structure is str, an array of bytes. The compiler knows how many bytes, including the two trailing '\0's, was placed into the array, so it produces the proper size at compile time. sizeof has no idea that str is a null-terminated C string, so it produces 15.

This is in contrast to strlen, which interprets your string as a C string, and returns the count of characters before the first '\0'.

Why strlen() can get the length of string and sizeof cannot in C?

You've invoked undefined behaviour: you're printing a value of type size_t with the format for an int. This is not guaranteed to work. Cast the value:

printf("length is: %d\n, (int)sizeof(greeting));

The result will be one larger than the result from strlen() because strlen() does not count the terminating null byte and sizeof() does.



Related Topics



Leave a reply



Submit