sizeof与lstrlen的差别 32位和64位下sizeof有什么不同

1、何为32位?何为64位?

32位和64位实际上指的是CPU的架构,也就是CPU单条指令能够处理的位数,32位的CPU一次处理32位(也就是4个字节)长度,64位的CPU一次能够处理64位(也就是8个字节),所以理论上来说64位的CPU的运行速度是32位的两倍。当然,二者的指令集完全不同,再加上其他的一些因素,实际上很难达到以上的理论结果。

32位和64位同样代指操作系统,32位的CPU(X86架构)不支持64位的操作系统,也就是说64位的操作系统不能安装在32位的CPU上。64位的操作系统只能安装在64位的CPU上(X64架构),当然64位的CPU是向下兼容的,也就是说可以安装32位的操作系统。另一方面,32位的操作系统只能运行32位的应用程序,而64位的操作系统则可以运行32位和64位的应用程序,虽然运行32位的应用程序会使得CPU大材小用。

以上就是关于32位和64位的简单介绍。


2、sizeof和lstrlen的定义是什么?

2.1、  sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组、指针、类型、对象、函数等。
    它的功能是:获得保证能容纳实现所建立的最大对象的字节大小。
    由于在编译时计算,因此sizeof不能用来返回动态分配的内存空间的大小。实际上,用sizeof来返回类型以及静态分配的对象、结构或数组所占的空间,返回值跟对象、结构、数组所存储的内容没有关系。
    具体而言,当参数分别如下时,sizeof返回的值表示的含义如下:
    数组——编译时分配的数组空间大小;
    指针——存储该指针所用的空间大小(存储该指针的地址的长度,是长整型,应该为4);
    类型——该类型所占的空间大小;
    对象——对象的实际占用空间大小;
    函数——函数的返回类型所占的空间大小。函数的返回类型不能是void。

2.2、strlen
    strlen(...)是函数,要在运行时才能计算。参数必须是字符型指针(char*)。当数组名作为参数传入时,实际上数组就退化成指针了。
    它的功能是:返回字符串的长度。该字符串可能是自己定义的,也可能是内存中随机的,该函数实际完成的功能是从代表该字符串的第一个地址开始遍历,直到遇到结束符NULL。返回的长度大小不包括NULL。


3、小举一例

char * parr = new char[10];
char szTest1[128];
char szTest2[] = "123456789";
int len1 = sizeof(parr);
int len2 = sizeof(*parr);
int len3= sizeof(szTest1);
int len4 = sizeof(szTest2);


运行结果: 

32位:4  1  128  10

64位:8  1  128  10


分析:

int len1 = sizeof(parr);  // 用来获取一个指向字符数组的指针类型的字节大小,32位下其值位4个字节,64位下其值位8个字节,其相当于sizeof(char*)(不管是何种类型的指针类型,其字节大小都是如此)

int len2 = sizeof(*parr); // 用来获取一个字符的字节大小,字符的字节大小是1,只不过32位下其是32位,64位下其是64位,但都是一个字节大小。其相当于sizeof(char).

int len3= sizeof(szTest1);  // 用来获得数组szTest1的大小,其空间大小毫无疑问是128。

int len4 = sizeof(szTest2); // 用来获取数组szTest2的大小,其空间以能够存储字符串“123456789”,加上结束符,正好是10。 


以下是一个基于Win32 API的时钟程序,可以显示当前系统日期时间: ```c++ #include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT("Clock"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppName, TEXT("Clock"), WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static BOOL f24Hour; static HBITMAP hBitmap; static int cxClient, cyClient; static int cxBitmap, cyBitmap; static int xC, yC, cxC, cyC; static int iRadius, iMarker; HDC hdc, hdcMem; PAINTSTRUCT ps; POINT apt[3]; SYSTEMTIME st; TCHAR szBuffer[256]; int i, cx, cy, iSec, iMin, iHour, iAngle; double fAngle; switch (message) { case WM_CREATE: hdc = GetDC(hwnd); hBitmap = LoadBitmap(GetModuleHandle(NULL), TEXT("Clock")); hdcMem = CreateCompatibleDC(hdc); SelectObject(hdcMem, hBitmap); GetObject(hBitmap, sizeof(BITMAP), &bm); cxBitmap = bm.bmWidth; cyBitmap = bm.bmHeight; ReleaseDC(hwnd, hdc); DeleteDC(hdcMem); SetTimer(hwnd, 1, 1000, NULL); f24Hour = TRUE; return 0; case WM_SIZE: cxClient = LOWORD(lParam); cyClient = HIWORD(lParam); iRadius = min(cxClient, cyClient) / 2.5; iMarker = iRadius / 5; xC = cxClient / 2; yC = cyClient / 2; cxC = 7 * iMarker / 4; cyC = iMarker / 2; return 0; case WM_TIMER: InvalidateRect(hwnd, NULL, TRUE); return 0; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); hdcMem = CreateCompatibleDC(hdc); SelectObject(hdcMem, hBitmap); BitBlt(hdc, xC - cxBitmap / 2, yC - cyBitmap / 2, cxBitmap, cyBitmap, hdcMem, 0, 0, SRCCOPY); // Get the current system time. GetLocalTime(&st); // Convert the time into hour, minute, and second. iSec = st.wSecond; iMin = st.wMinute; if (f24Hour) iHour = st.wHour; else iHour = st.wHour % 12; if (iHour == 0) iHour = 12; // Draw the hour hand. iAngle = (iHour * 30) % 360 + iMin / 2; fAngle = iAngle * 3.14159265358979323846 / 180.0; apt[0].x = xC; apt[0].y = yC; apt[1].x = xC + (int) (0.6 * iRadius * sin(fAngle)); apt[1].y = yC - (int) (0.6 * iRadius * cos(fAngle)); apt[2].x = xC - (int) (0.1 * iRadius * sin(fAngle)); apt[2].y = yC + (int) (0.1 * iRadius * cos(fAngle)); Polygon(hdc, apt, 3); // Draw the minute hand. iAngle = iMin * 6; fAngle = iAngle * 3.14159265358979323846 / 180.0; apt[0].x = xC; apt[0].y = yC; apt[1].x = xC + (int) (0.8 * iRadius * sin(fAngle)); apt[1].y = yC - (int) (0.8 * iRadius * cos(fAngle)); apt[2].x = xC - (int) (0.1 * iRadius * sin(fAngle)); apt[2].y = yC + (int) (0.1 * iRadius * cos(fAngle)); Polygon(hdc, apt, 3); // Draw the second hand. iAngle = iSec * 6; fAngle = iAngle * 3.14159265358979323846 / 180.0; MoveToEx(hdc, xC, yC, NULL); LineTo(hdc, xC + (int) (0.8 * iRadius * sin(fAngle)), yC - (int) (0.8 * iRadius * cos(fAngle))); // Draw the tick marks. for (i = 0; i < 60; i++) { if (i % 5 == 0) { MoveToEx(hdc, xC + (int) ((iRadius - iMarker) * sin(i * 6 * 3.14159265358979323846 / 180.0)), yC - (int) ((iRadius - iMarker) * cos(i * 6 * 3.14159265358979323846 / 180.0)), NULL); LineTo(hdc, xC + (int) (iRadius * sin(i * 6 * 3.14159265358979323846 / 180.0)), yC - (int) (iRadius * cos(i * 6 * 3.14159265358979323846 / 180.0))); } else { MoveToEx(hdc, xC + (int) ((iRadius - iMarker / 2) * sin(i * 6 * 3.14159265358979323846 / 180.0)), yC - (int) ((iRadius - iMarker / 2) * cos(i * 6 * 3.14159265358979323846 / 180.0)), NULL); LineTo(hdc, xC + (int) (iRadius * sin(i * 6 * 3.14159265358979323846 / 180.0)), yC - (int) (iRadius * cos(i * 6 * 3.14159265358979323846 / 180.0))); } } // Draw the center ellipse. Ellipse(hdc, xC - cxC, yC - cyC, xC + cxC, yC + cyC); // Draw the date and time. wsprintf(szBuffer, TEXT("%02d:%02d:%02d %s"), f24Hour ? iHour : iHour % 12, iMin, iSec, f24Hour ? TEXT("") : (iHour < 12 ? TEXT("AM") : TEXT("PM"))); cx = GetSystemMetrics(SM_CXSCREEN); cy = GetSystemMetrics(SM_CYSCREEN); SetTextColor(hdc, RGB(255, 0, 0)); SetBkMode(hdc, TRANSPARENT); TextOut(hdc, cx - 5 * cxBitmap / 4, cy - cyBitmap, szBuffer, lstrlen(szBuffer)); // Clean up. DeleteDC(hdcMem); EndPaint(hwnd, &ps); return 0; case WM_DESTROY: KillTimer(hwnd, 1); DeleteObject(hBitmap); PostQuitMessage(0); return 0; case WM_KEYDOWN: switch (wParam) { case 'F': case 'f': f24Hour = !f24Hour; InvalidateRect(hwnd, NULL, TRUE); return 0; case VK_ESCAPE: DestroyWindow(hwnd); return 0; } break; } return DefWindowProc(hwnd, message, wParam, lParam); } ``` 程序中使用了双缓冲技术,可以避免闪烁问题。同时,程序还支持按下“F”键可以在12小时制和24小时制之间切换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值