天天看點

debug, release strlen與sizeof

1, c/c++

如果是字元數組沒有賦初值,則debug和release版的strlen(str)結果是不一樣的.

sizeof怎樣都是配置設定的那樣。

#include <iostream>

using namespace std;

void main()
{
	char str[10];

	cout << str << endl;
	cout << "len is " << strlen(str) << endl;
	cout << "len is " << sizeof(str) << endl;

	getchar();
}           
debug, release strlen與sizeof
debug, release strlen與sizeof

為了debug和release的結果一樣最後定義完變量就初始化,不然可能程式出錯了很難找到那裡有問題。

關于strlen和sizeof的詳細資料看微軟的官方文檔

MSDN: strlen,  sizeof

strlen

strlen所作的僅僅是一個計數器的工作,它從記憶體的某個位置(可以是字元串開頭,中間某個位置,甚至是某個不确定的記憶體區域)開始掃描,直到碰到第一個字元串結束符'\0'為止,然後傳回計數器值(長度不包含'\0')。

2,  sizeof與strlen的差別