zoukankan      html  css  js  c++  java
  • sizeof与strlen

    sizeof与strlen,特别是sizeof,据说是很多公司面试技术岗会问的问题。

    这篇博客写得挺完善的,不过是不是没有错误还需要自己亲手写代码运行下看看结果。

    点击打开链接

    关于sizeof更详细解释的网搜一大堆。

    然后我这里写了几个简单的测试样例:

    // ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    void test(int *a)
    {
    	//结果为4,只会输出头指针所占空间大小,数组实际变成了一个指针
    	cout << sizeof(a) << endl;
    }
    
    class hello
    {
    
    };
    
    int main()
    {
    	//一个字节byte = 8bit
    	char str[20] = "0123456789";
    	int a = strlen(str); //a=10; strlen只返回已存字符数组(串)的长度
    	int b = sizeof(str); //b=20;sizeof会返回预先分配给数组的所有内存空间的大小(单位为字节)
    	cout << a << endl << b << endl;
    
    	int *c;
    	//第一个结果为4,32位编译器指针为4个字节,后面的相当于sizeof(int),int类型为4个字节
    	cout << sizeof(c) << endl << sizeof(*c) << endl;
    
    	cout << "sizeof(char) " << sizeof(char) << endl
    		<< "sizeof(short) " << sizeof(short) << endl
    		<< "sizeof(int) " << sizeof(int) << endl //signed int
    		<< "sizeof(unsigned int) " << sizeof(unsigned int) << endl
    		<< "sizeof(flaot) " << sizeof(float) << endl
    		<< "sizeof(double) " << sizeof(double) << endl;
    
    	int d[5];
    	test(d);
    
    	cout << sizeof(hello) << endl;//=1,空数据结构也会分配1字节的空间
    
        return 0;
    }
    



  • 相关阅读:
    RecyclerView 源码分析(一) —— 绘制流程解析
    sqlserver outer join
    获取最后一个标签 xpath
    pytesseract
    pytesseract代码优化
    sql server recursion
    sql
    sql server
    mssql乱码问题
    SQL
  • 原文地址:https://www.cnblogs.com/lvlang/p/10586476.html
Copyright © 2011-2022 走看看