C ++ strlen(ch)和sizeof(ch)strlen

时间:2012-08-13 08:11:39

标签: c++ c

我有这段代码:

int main()
{
    char ch[15];
    cout<<strlen(ch)<<endl; //7
    cout<<sizeof(ch)<<endl; //15
    return 0;
}

为什么strlen(ch)会给出不同的结果,即使它是空的char数组?

6 个答案:

答案 0 :(得分:6)

您的代码具有未定义的行为,因为您正在使用strlen读取数组的未初始化值。如果您想要strlen的确定结果,则必须初始化(或分配)您的数组。

E.g。

char ch[15] = "Hello, world!";

char ch[15] = {};

sizeof会给出其操作数的大小,因为根据定义char的大小为char[15]的大小始终为15。

strlen给出空终止字符串的长度,该字符串是给定char数组中具有值0的第一个char的偏移量。对于对strlen的调用有效,参数必须实际指向空终止字符串。

答案 1 :(得分:5)

ch是局部变量,并且未初始化局部变量。所以你假设它是一个空字符串是不正确的。它充满了垃圾。在7个垃圾字符之后发现\0字符只是一个共同发生,因此strlen返回7。

您可以执行类似这样的操作以确保空字符串 -

char ch[15]={0};
ch[0]='\0`;
strcpy(ch,"");

这是一个类似的线程,可以阅读更多内容

Variable initialization in C++

答案 2 :(得分:2)

问题在于

strlen(ch);

strlen计算字符数,直到达到\0符号。此处ch未初始化,因此strlen可以返回任何

答案 3 :(得分:1)

至于strlen的结果,在您的情况下,您有一个未初始化的char数组,因此strlen发生以产生 7 :数组元素8必须有一个空字符,但是这段代码每次都会为strlen提供不同的结果。

始终初始化字符串,使用数组很简单:char str[15] = {0};

sizeof是一个运算符,用于获取变量或数据类型的大小,或者数组占用的字节数, C字符串的长度;不要指望strlenstrcpy可以互换,甚至可以用任何有用的方式进行比较。

例如:

int main()
{
    char str[15] = "only 13 chars";

    cout << "strlen: " << strlen(str) << endl;
    cout << "sizeof: " << sizeof(str) << endl;
}

输出结果为:

strlen: 13
sizeof: 15

答案 4 :(得分:0)

  

返回str的长度。

     

C字符串的长度由终止确定   null-character:C字符串与字符数一样长   在字符串的开头和终止的null之间   字符。

sizeof返回字节数(15)。您的数组由垃圾填充,因此,strlen可以返回任何数字。正确的例子是

int main()
{
    char ch[15] = {0};
    cout<<strlen(ch)<<endl; //0
    cout<<sizeof(ch)<<endl; //15
    return 0;
}

答案 5 :(得分:0)

C ++中sizeofstrlen之间的区别:

1)sizeof运算符strlen函数;

2)sizeof的返回类型为size_t,并在其标题中将其定义为(typedef)为unsigned int; 它获取内存分配的字节大小,可以最大化以容纳在内存中创建的此对象;

3)sizeof可以使用类型作为参数,而strlen只能使用char指针(char*)作为指针,它必须是结束为&#39; \0&#39 ;; sizeof也可以使用函数作为参数,例如:

short f() {return 100;} 
std::cout << "sizeof(f()): " << sizeof(f()) << std::endl;
//The result will be sizeof(short), which is 2.

4)如果char数组是一个参数,它不会被sizeof降级,而strlen会将它降级为char指针;

5)strlen的结果将在运行时间中计算,而不是编译时间strlen用于获取真实结果字符串内容的大小(字符串,字符数组,字符指针)直到&#39; \0&#39;,而不是内存分配的实际大小。大多数编译器将在编译时间中计算sizeof的结果,无论参数是类型还是变量,这就是sizeof(x)可用于决定维度的原因数组:

char str[20]="0123456789"; 
int a=strlen(str); //a=10; 
int b=sizeof(str); //while b=20; 

7)如果sizeof的参数是类型,则括号为必需,而如果参数为变量 >,括号可选,因为 sizeof是运算符而不是函数;

8)当您使用结构化类型变量作为参数时,sizeof将返回其实际尺寸,您使用静态数组sizeof将返回数组大小。但sizeof运算符无法返回创建动态外部的数组的大小。因为sizeof是编译时运算符

以下是sizeof和strlen的示例:

#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>

short f1 ()
{
  return 100;
}

int f2 ()
{
  return 1000;
}

int main()
{
  char* char_star = "0123456789";
  // char_star is a char pointer, sizeof will return the pointer size allocated in memory: depends on your machine
  std::cout << "sizeof(char_star):" << sizeof(char_star) << std::endl;
  // *char_star is the first element of the string, it is a char, sizeof will return the char size allocated in memory: depends on your machine, normally is 1
  std::cout << "sizeof(*char_star):" << sizeof(*char_star) << std::endl;
  // char_star is a char pointer, strlen will return the real size of the string until '\0': 10
  std::cout << "strlen(char_star):" << strlen(char_star) << std::endl;
  std::cout << std::endl;

  char char_array[] = "0123456789";
  // char_array is a char array, sizeof will return the array size allocated in memory, with a '\0' at the end: 10 + 1
  std::cout << "sizeof(char_array):" << sizeof(char_array) << std::endl;
  // *char_array is the first element of the array, it is a char, sizeof will return the char size allocated in memory: depends on your machine, normally is 1
  std::cout << "sizeof(*char_array):" << sizeof(*char_array) << std::endl;
  // char_array is a char array, strlen will return the real size of the string until '\0': 10
  std::cout << "strlen(char_array):" << strlen(char_array) << std::endl;
  std::cout << std::endl;

  char_array_fixed[100] = "0123456789";
  // char_array_fixed is a char array with fixed size, sizeof will return the array size allocated in memory: 100
  std::cout << "sizeof(char_array_fixed):" << sizeof(char_array_fixed) << std::endl;
  // *char_array_fixed is the first element of the array, it is a char, sizeof will return the char size allocated in memory: depends on your machine, normally is 1
  std::cout << "sizeof(*char_array_fixed):" << sizeof(*char_array_fixed) << std::endl;
  // *char_array_fixed is a char array with fixed size, strlen will return the real content size of the string until '\0': 10
  std::cout << "strlen(char_array_fixed):" << strlen(char_array_fixed) << std::endl;
  std::cout << std::endl;

  int int_array[100] = {0,1,2,3,4,5,6,7,8,9};
  // int_array is a int array with fixed size, sizeof will return the array size allocated in memory: 100
  std::cout << "sizeof(int_array):" << sizeof(int_array) << std::endl;
  // *int_array is the first element of the array, it is an int, sizeof will return the int size allocated in memory: depends on your machine, normally is 4
  std::cout << "sizeof(*int_array):" << sizeof(*int_array) << std::endl;
  // int_array is a int array with fixed size, strlen will throw exception 
  //std::cout << "strlen(int_array):" << strlen(int_array) << std::endl;
  std::cout << std::endl;

  char char_array2[] = {'a', 'b', '3'};
  // char_array2 is a char array, sizeof will return the array size allocated in memory: 3
  std::cout << "sizeof(char_array2):" << sizeof(char_array2) << std::endl;
  // *char_array2 is the first element of the array, it is a char, sizeof will return the char size allocated in memory: depends on your machine, normally is 1
  std::cout << "sizeof(*char_array2):" << sizeof(*char_array2) << std::endl;
  // *char_array2 is a char array, strlen will return the real content size of the string until '\0': 3
  std::cout << "strlen(char_array2):" << strlen(char_array2) << std::endl;
  std::cout << std::endl;

  char char_array3[] = {"abc"};
  // char_array3 is a char array, sizeof will return the array size allocated in memory, with a '\0' at the end : 3 + 1
  std::cout << "sizeof(char_array3):" << sizeof(char_array3) << std::endl;
  // *char_array3 is the first element of the array, it is a char, sizeof will return the char size allocated in memory: depends on your machine, normally is 1
  std::cout << "sizeof(*char_array3):" << sizeof(*char_array3) << std::endl;
  // *char_array3 is a char array, strlen will return the real content size of the string until '\0': 3
  std::cout << "strlen(char_array3):" << strlen(char_array3) << std::endl;
  std::cout << std::endl;

  std::string str = {'a', 'b', '3', '\0', 'X'};
  // str is a string, sizeof will return the string size allocated in memory (string is a wrapper, can be considered as a special structure with a pointer to the real content): depends on your machine, normally is 32
  std::cout << "str:" << str << std::endl;
  std::cout << "sizeof(str):" << sizeof(str) << std::endl;
  // *str means nothing, sizeof will throw exeption
  //std::cout << "sizeof(*str):" << sizeof(*str) << std::endl;
  // str is a string, strlen will return the real content size of the string until '\0': 3
  std::cout << "strlen(str):" << strlen(str.c_str()) << std::endl;
  std::cout << std::endl;

  // sizeof is an operation, if the parameter is a type, parentheses are mandatory
  std::cout << "sizof(int):" << sizeof(int) << std::endl;
  // sizeof is an operation, if the parameter is a variable, parentheses are optional
  std::cout << "sizof char_star:" << sizeof char_star << std::endl;
  std::cout << "sizof char_array:" << sizeof char_array << std::endl;
  // sizeof is an operation, can take a function as parameter
  std::cout << "sizeof(f()): " << sizeof(f1()) << std::endl;
  std::cout << "sizeof(f()): " << sizeof(f2()) << std::endl;
}