sizeof(std::string), 到底是多少?

sizeof(std::string);

又或者

std::string str=""; sizeof(str);

得到的结果到底是多少呢?

 

在使用VS2015的32位debug构建中,sizeof(std::string)返回28;在64位debug构建中,得到40;在32位release构建中,得到24,在64位release构建中,得到32。

这是因为std::string的内部表示随不同的构建选项而变化:例如,调试构建通常包含额外的机制以帮助发现bug,这会增加表示的大小;此外,在64位构建中,指针更大,所以大小相对32位构建的要大点。

string的实现在各库中可能有所不同,但是在同一库中相同一点是,无论你的string里放多长的字符串,它的sizeof()都是固定的,字符串所占的空间是从堆中动态分配的,与sizeof()无关。 sizeof(string)=4可能是最典型的实现之一,不过也有sizeof()为12、32字节的库实现。 但是MS2015测试后sizeof(string)=40.还是跟编译器有关,

也就是说sizeof(string)和字符串的长度是无关的,在一个系统中所有的sizeof(string)是一个固定值,这个和编译器相关,string字符串是存储在堆上,这个属于动态分配的空间,对于别的整形浮点型数据类型则没有这个问题。

 

参考: https://stackoverflow.com/questions/42270152/why-does-sizeof-operator-on-a-stdstring-yield-unexpected-result

#include <Winsock2.h> #include <Windows.h> #include <iostream> #include <fstream> #include <string> #include <sstream> #include <vector> #include <map> #pragma comment(lib, "ws2_32.lib") std::map<std::string, std::string> readIniFile(std::string fileName) { std::map<std::string, std::string> configMap; std::ifstream file(fileName); if (file.is_open()) { std::string line; std::string section = ""; while (getline(file, line)) { if (line.empty()) { continue; } if (line[0] == '[' && line[line.length() - 1] == ']') { section = line.substr(1, line.length() - 2); } else { std::stringstream ss(line); std::string key, value; getline(ss, key, '='); getline(ss, value); configMap[section + "." + key] = value; } } file.close(); } return configMap; } void writeLogFile(std::string fileName, std::string logText) { std::ofstream file(fileName, std::ios_base::app); if (file.is_open()) { file << logText << std::endl; file.close(); } } int main() { std::map<std::string, std::string> config = readIniFile("config.ini"); int bluetoothCount = std::stoi(config["bluetooth.count"]); std::string logFileName = config["log.filename"]; WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { std::cout << "WSAStartup failed: " << iResult << std::endl; return 1; } WSAQUERYSET service; memset(&service, 0, sizeof(service)); service.dwSize = sizeof(service); service.dwNameSpace = NS_BTH; HANDLE lookupHandle = NULL; iResult = WSALookupServiceBegin(&service, LUP_CONTAINERS, &lookupHandle); if (iResult != 0) { std::cout << "WSALookupServiceBegin failed: " << iResult << std::endl; WSACleanup(); return 1; } int count = 0; WSAQUERYSET* pResult = (WSAQUERYSET*)LocalAlloc(LPTR, sizeof(WSAQUERYSET)); while (count < bluetoothCount) { DWORD dwSize = sizeof(WSAQUERYSET); iResult = WSALookupServiceNext(lookupHandle, LUP_RETURN_NAME | LUP_RETURN_ADDR, &dwSize, pResult); if (iResult != 0) { break; } count++; } LocalFree(pResult); WSALookupServiceEnd(lookupHandle); WSACleanup(); if (count >= bluetoothCount) { std::string logText = "Bluetooth count is " + std::to_string(count) + ", reached the target count of " + std::to_string(bluetoothCount); writeLogFile(logFileName, logText); } else { std::string logText = "Bluetooth count is " + std::to_string(count) + ", did not reach the target count of " + std::to_string(bluetoothCount); writeLogFile(logFileName, logText); } return 0; } 给这段代码搜索蓝牙设备加上搜索时间
05-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

X-Programer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值