A weird difference between strlen() and sizeof()

夙愿已清 提交于 2020-01-14 07:00:08

问题


I tried to test the difference of sizeof and strlen but I found something strange today. The code is as follow.

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
        char a[]={"I am a boy"};
        char b[]={'I',' ','a','m',' ','a',' ','b','o','y'};
        cout << "sizeof(a) = " << sizeof(a) << endl << "sizeof(b) = " << sizeof(b) <<endl;
        cout << "strlen(a) = "<< strlen(a) << endl << "strlen(b) = " << strlen(b) << endl;
        return 0;

}

The result is as follow:

sizeof(a) = 11
sizeof(b) = 10
strlen(a) = 10
strlen(b) = 11

I know the first three lines of the result, but I do not understand why strlen(b) is 11. Could anyone help me?


回答1:


It is just luck. strlen takes a pointer to char, and assumes it is the beginning of a nul-terminated string. It essentially steps forward until it finds a \0. There happens to be a \0 somewhere soon after the end of array b, but really you are invoking undefined behviour.




回答2:


strlen expects a null-terminated string. b isn't null-terminated. Undefined behavior results.




回答3:


sizeof() tells us what the compiler knows about the size of the object (not necessarily its contents).

strlen() walks through memory at runtime from the address we give it looking for a zero and counts up how many memory locations it passes before it finds one.




回答4:


Argument of strlen has to be a C-style string. Your b is not a C-style string. Passing something that is not a C-style string to strlen triggers undefined behavior. This is what you observed. Whatever result you obtained from strlen(b) (if any) is completely meaningless.

The title of your question is rather misleading as well. "Weird difference between strlen() and sizeof()"? strlen and sizeof are two completely unrelated features of the language with virtually nothing in common. They are naturally different. Which is why referring to any observed differences between them as "weird" is... well, weird.




回答5:


That's not really accurate and can lead to a misunderstanding, especially since sizeof(A) will not give you 13.

sizeof() is a compile-time expression giving you the size of a type or a variable's type. It doesn't care about the value of the variable.

strlen() is a function that takes a pointer to a character, and walks the memory from this character on, looking for a NUL character. It counts the number of characters before it finds the NUL character. In other words, it gives you the length of a C-style NUL-terminated string.

The two are entirely different in purpose and have nearly nothing to do with each other. In C++, you shouldn't need either very much; strlen() is for C-style strings, which should be replaced by C++-style std::strings, whereas the primary application for sizeof() in C is as an argument to functions like malloc(), memcpy() or memset(), all of which you shouldn't use in C++ (use new, std::copy(), and std::fill() or constructors)



来源:https://stackoverflow.com/questions/26151630/a-weird-difference-between-strlen-and-sizeof

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!