003--sizeof的使用

sizeof用于获取类型或变量的内存大小。可对类型名或变量使用sizeof。对类型(如int)使用sizeof运算符时,应该将名称放在括号中;但对变量名(n_short)使用该运算符,括号是可选的:

 1 #include <iostream>
 2 #include <climits>
 3
 4 int main(){
 5     using namespace std;
 6     int n_int=INT_MAX;
 7     short n_short=SHRT_MAX;
 8     long n_long=LONG_MAX;
 9     long long n_llong=LLONG_MAX;
10
11     cout<<"int is "<<sizeof(int)<<" bytes."<<endl;
12     cout<<"short is "<<sizeof n_short<<" bytes."<<endl;
13     cout<<"long is "<<sizeof n_long<<" bytes."<<endl;
14     cout<<"long long is "<<sizeof n_llong<<" bytes."<<endl;
15     cout<<endl;
16
17     cout<<"Maximum values:"<<endl;
18     cout<<"int: "<<n_int<<endl;
19     cout<<"short: "<<n_short<<endl;
20     cout<<"long: "<<n_long<<endl;
21     cout<<"long long: "<<n_llong<<endl;
22
23     cout<<"Minimum int value:"<<INT_MIN<<endl;
24     cout<<"Bits per byte: "<<CHAR_BIT<<endl;
25
26     return 0;
27 }

时间: 2024-12-16 18:24:19

003--sizeof的使用的相关文章

【LeetCode】003 Longest Substring Without Repeating Characters

题目:LeetCode 003 Longest Substring Without Repeating Characters 题意:给一个字符串,找到一个没有重复字符的最长子串. 样例:”bbbbb” 返回”b”的长度1:”abcabcbb”返回”abc”的长度3. 思路: 动态规划.dp[i]表示以第i个字符为结尾的无重复字符的子串的最长的长度,需要一个辅助数组idx[s[i]]记录字符当前字符s[i]上一次出现的位置,如果未出现过则为-1.所以得到如下的递推公式: 另外由于不确定字符串长度的

003:鸣人的影分身

003:鸣人的影分身 总时间限制: 1000ms 内存限制: 65536kB 描述 在火影忍者的世界里,令敌人捉摸不透是非常关键的.我们的主角漩涡鸣人所拥有的一个招数--多重影分身之术--就是一个很好的例子. 影分身是由鸣人身体的查克拉能量制造的,使用的查克拉越多,制造出的影分身越强. 针对不同的作战情况,鸣人可以选择制造出各种强度的影分身,有的用来佯攻,有的用来发起致命一击. 那么问题来了,假设鸣人的查克拉能量为M,他影分身的个数为N,那么制造影分身时有多少种(用K表示)不同的分配方法?(影分

[LeetCode] 003. Longest Substring Without Repeating Characters (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 003.Longest_Substring_Without_Repeating_Characters (Medium) 链接: 题目:https://oj.leetcode.com/problems/Longest-Substring-Without-Repeating-Characters/ 代码(github)

关于malloc和sizeof的用法

问题1: 1.L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); 2.newbase = (ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType)); 其中L是已经定义的线性表,LIST_INIT_SIZE是线性表存储空间的初始分配量,listsize是当前分配的存储容量(以sizeof(ElemType)为单位) 解释: 第一个句子:用ma

sizeof strlen strncpy

sizeof测类型(数组名除外) strlen测实际长度 strncpy返回指针类型 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 int main() 5 { 6 char *p="wangddd"; 7 printf("%d\n",sizeof(p));//输出4,指针类型 8 9 char x[8]; 10 printf("%d

关于sizeof的那些事

一 结构体 先上代码: struct A { int a; double d; char b; short c; }; 以上代码的sizeof(struct A)为24.分析: a占用4字节,由于d占用8字节,且d需要在8字节处对齐,因此a不仅有4字节存放内容,并且占另外的4个字节使得d位于8字节处.由于c需要在2字节处对齐,所以b占用了2个字节,c占用两个字节,目前总共是20字节,但是整个结构体A根据最大字节对齐,因此为8字节对齐,为了使得类型位struct A的数组的下一个元素处于正确的对齐

C++字符串使用sizeof时注意

char tmp1[20] = {"hello,你好"}; char tmp2[] = {"hello,你好"}; char *tmp3 = new char[20]; sprintf(tmp3,"%s","hello,你好"); string tmp4 = "hello,你好"; printf("%d %d %d %d\n",sizeof(tmp1),sizeof(tmp2),size

含有虚函数的类sizeof大小

#include <iostream> using namespace std; class Base1{ virtual void fun1(){} virtual void fun11(){} public: virtual ~Base1(); }; class Base2{ virtual void fun2(){} }; class DerivedFromOne: public Base2 { virtual void fun2(){} virtual void fun22(){} }

程序猿之---C语言细节24(段错误、类型提升、sizeof &#39;A&#39;)

主要内容:段错误.类型提升.sizeof  'A' #include <stdio.h> int main() { union test{ char a[10]; int b; }u; int *p = (int *)&(u.a[1]); // 没有引起总线错误 *p = 17; printf("%d\n",*p); #if 0 int *q = 0; // 引起段错误,在linux中运行可看到段错误,在windows下运行时直接出错 *q = 1; #endif

sizeof 和类继承 虚继承 求类大小

代码: #include <iostream> using namespace std; /* class a{ float k; // 4字节 virtual void foo(){} //有一个4字节的指针指向自己的虚函数表 }; class b : virtual public a{ virtual void f(){} }; 有这样的一个指针vptr_b_a,这个指针叫虚类指针,也是四个字节:还要包括类a的字节数,所以类b的字节数就求出来了. 运行结果: 8 16 */ /* clas