sizeof

size guarantee for integral/arithmetic types in C and C++

爱⌒轻易说出口 提交于 2019-11-27 14:18:04
问题 I know that the C++ standard explicitly guarantees the size of only char , signed char and unsigned char . Also it gives guarantees that, say, short is at least as big as char , int as big as short etc. But no explicit guarantees about absolute value of, say, sizeof(int) . This was the info in my head and I lived happily with it. Some time ago, however, I came across a comment in SO (can't find it) that in C long is guaranteed to be at least 4 bytes, and that requirement is "inherited" by C++

C——内存管理1

空扰寡人 提交于 2019-11-27 14:15:48
array limitations: the size of the array must be known beforehand the size of the array cannot be changed in the duration of your program So there comes Dynamic memory allocation : Dynamic memory allocation in C is a way of circumventing these problems. The standard C function malloc function 定义在 stdlib.h or malloc.h 中,取决于你使用的操作系统。 Malloc.h contains only the definitions for the memory allocation functions and not the rest of the other functions defined in stdlib.h.Usually you will not need to be so specific in your program, and if both are supported, you should use <stdlib.h>, since that is

sizeof()用法汇总

筅森魡賤 提交于 2019-11-27 13:52:26
sizeof()功能:计算数据空间的字节数 1.与strlen()比较 strlen()计算字符数组的字符数,以"\0"为结束判断,不计算为'\0'的数组元素。 而sizeof计算数据(包括数组、变量、类型、结构体等)所占内存空间,用字节数表示。 2.指针与静态数组的sizeof操作 指针均可看为变量类型的一种。所有指针变量的sizeof 操作结果均为4。 注意 :int *p; sizeof(p)=4; 但sizeof(*p)相当于sizeof(int); 对于静态数组,sizeof可直接计算数组大小; 例:int a[10];char b[]="hello"; sizeof(a)等于4*10=40; sizeof(b)等于6; 注意 :数组做型参时,数组名称当作指针使用!! void fun(char p[]) {sizeof(p)等于4} 经典问题: double* (*a)[3][6]; cout<<sizeof(a)<<endl; // 4 a为指针 cout<<sizeof(*a)<<endl; // 72 *a为一个有3*6个指针元素的数组 cout<<sizeof(**a)<<endl; // 24 **a为数组一维的6个指针 cout<<sizeof(***a)<<endl; // 4 ***a为一维的第一个指针 cout<<sizeof(****a)<<endl

sizeof()

妖精的绣舞 提交于 2019-11-27 13:50:47
这是初学者问得最多的一个问题,所以这里有必要多费点笔墨。让我们先看一个结构体: struct S1 { char c; int i; }; 问sizeof(s1)等于多少聪明的你开始思考了,char占1个字节,int占4个字节,那么加起来就应该是5。是这样吗你在你机器上试过了吗也许你是对的,但很可能你是错的!VC6中按默认设置得到的结果为8。 Why为什么受伤的总是我 请不要沮丧,我们来好好琢磨一下sizeof的定义——sizeof的结果等于对象或者类型所占的内存字节数,好吧,那就让我们来看看S1的内存分配情况: S1 s1 = { 'a', 0xFFFFFFFF }; 定义上面的变量后,加上断点,运行程序,观察s1所在的内存,你发现了什么 以我的VC6.0为例,s1的地址为0x0012FF78,其数据内容如下: 0012FF78: 61 CC CC CC FF FF FF FF 发现了什么怎么中间夹杂了3个字节的CC看看MSDN上的说明: When applied to a structure type or variable, sizeof returns the actual size, which may include padding bytes inserted for alignment. 原来如此,这就是传说中的字节对齐啊!一个重要的话题出现了。

sizeof(char**),sizeof(char*), sizeof(char)小注

别等时光非礼了梦想. 提交于 2019-11-27 13:50:36
sizeof(char) → 返回char型所占空间:1 (Byte) sizeof(char*) → 返回char*型指针所占空间:4 (Byte) sizeof(数组名) → 返回该字符串指针数组里元素所占空间:n*4(或8)(n为字符串指针数组元素个数,也即数组的字符串个数) 比如 char *strlist[] = {"American", "Germany", "Japan", "China", "France", "Russia"} 此时sizeof(strlist) = 6*4 = 24(Byte),代表着6个char*类型的总大小。 所以想求的字符串指针数组的字符串元素个数就可以用 sizeof(strlist)/sizeof(char*)来得到。 来源: http://www.cnblogs.com/katsu/p/6665956.html

sizeof使用

心不动则不痛 提交于 2019-11-27 13:50:18
  c语言 中判断数据类型长度符的关键字   用法   sizeof(类型说明符, 数组 名或表达式);   或   sizeof 变量名   1. 定义:   sizeof是C/C++中的一个操作符(operator),简单的说其作用就是返回一个对象或者类型所占的内存字节数。   MSDN上的解释为:   The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t.   其返回值类型为size_t,在头文件 stddef.h 中定义。这是一个依赖于编译系统的值,一般定义为   typedef unsigned int size_t;   世上 编译器 林林总总,但作为一个规范,它们都会保证char、signed char和unsigned   char的sizeof值为1,毕竟char是我们编程能用的最小数据类型。   2. 语法:   sizeof有三种语法形式,如下:   1) sizeof( object ); // sizeof( 对象 );   2) sizeof( type_name ); //

sizeof和sizeof(string)的问题

╄→尐↘猪︶ㄣ 提交于 2019-11-27 13:50:01
今天看《程序员面试宝典》一书(为了应付将要到来的微软笔试),看到了sizeof(string)这个问题。在Dev C++上测试的结果是4,很不明白。上网搜了一下,得到如下结果: string strArr1[]={"Trend", "Micro", "Soft"}; sizeof(strArr1)=12 转自: http://apps.hi.baidu.com/share/detail/30398570 关于sizeof(string),今天看那本面试宝典的时候看到这个表达式,有点吃惊,书上写着sizeof(string)=4;当时很纳闷,难道分配4个字节大小的内存给string吗?查阅了相关资料得出结论: string的实现在各库中可能有所不同,但是在同一库中相同一点是,无论你的string里放多长的字符串,它的sizeof()都是固定的,字符串所占的空间是从堆中动态分配的,与sizeof()无关。 sizeof(string)=4可能是最典型的实现之一,不过也有sizeof()为12、32字节的库实现。 但是VC6.0测试后sizeof(string)=16.还是跟编译器有关 #include<iostream> using namespace std; void main(void) { string a[] = {"aaaaa","bbbb","ccc"}; int x =

Find size of array without using sizeof

谁说胖子不能爱 提交于 2019-11-27 13:38:47
I was searching for a way to find the size of an array in C without using sizeof and I found the following code: int main () { int arr[100]; printf ("%d\n", (&arr)[1] - arr); return 0; } Can anyone please explain to me how is it working? &arr is a pointer to an array of 100 int s. The [1] means "add the size of the thing that is pointed to", which is an array of 100 int s. So the difference between (&arr)[1] and arr is 100 int s. (Note that this trick will only work in places where sizeof would have worked anyway.) &arr gives you a pointer to the array. (&arr)[1] is equivalent to *(&arr + 1) .

Is sizeof(void()) a legal expression?

社会主义新天地 提交于 2019-11-27 13:24:15
问题 From [5.3.3/1], I found that: The sizeof operator shall not be applied to an expression that has function or incomplete type From [3.9/5] I found that: Incompletely-defined object types and cv void are incomplete types Anyway, for sizeof does not evaluate it's operands, I would have said that sizeof(void()) was a legal expression (actually GCC compiles it and the result is 1). On the other side, from here, void is not mentioned while discussing sizeof , neither when the types having size 1

Iperf 源代码分析(四)

▼魔方 西西 提交于 2019-11-27 12:57:26
Socket 类 Socket的定义和实现分别在文件Socket.hpp和 Socket.cpp中。它的主要功能是 封装了socket文件描述符、此socket对应的端口号,以及socket接口中的listen, accept, connect和close等函数 ,为用户提供了一个简单易用而又统一的接口。同时 作为其他派生类的基类。 Socket类的定义如下: #ifndef SOCKET_H #define SOCKET_H #include "headers.h" #include "SocketAddr.hpp" /* ------------------------------------------------------------------- */ class Socket { public: // stores server port and TCP/UDP mode Socket( unsigned short inPort, bool inUDP = false ); // destructor virtual ~Socket(); protected: // get local address SocketAddr getLocalAddress( void ); // get remote address SocketAddr