sizeof

C语言程序设计(二)

爷,独闯天下 提交于 2019-11-27 18:05:58
第二章 C数据类型 八进制整数由数字0开头,后跟0~7的数字序列组成。 十六进制整数由数字0加字母x(或X)开头,后跟0~9,a~f(或A~F)的数字序列组成。 整型常量: 默认的int型定义为有符号整数,因此对int型无需使用signed 无符号整型常量:U 长整型常量:L 无符号长整型常量:LU 十进制小数形式 指数形式:3.45e-6=0.00000345 实型常量: 单精度:F 双精度(实型常量默认) 长双精度:L 变量在使用前必须先定义,需要声明变量的类型和变量名。 //L2-1a-1 main()//以main()作为开头指定了C程序执行的起点 //一个C程序必须有且只能有一个用main()作为名称的函数,称为主函数 { int a; float b; char c; a = 1; b = 2.5; c = 'A'; } 变量名(标识符)是 大小写敏感的 所有变量必须在第一条可执行语句之前定义 空格是为了增强程序的 可读性 定义变量的同时可以对变量进行初始化,如int a=1; //L2-1a-2 main() { int a = 1; /* 定义整型变量a并对其初始化 */ float b = 2.5; /* 定义实型变量b并对其初始化 */ char c = 'A'; /* 定义字符型变量c并对其初始化 */ } 可同时定义多个相同的变量,如int a,b,c;

How does sizeof calculate the size of structures

戏子无情 提交于 2019-11-27 17:56:22
问题 I know that a char and an int are calculated as being 8 bytes on 32 bit architectures due to alignment, but I recently came across a situation where a structure with 3 shorts was reported as being 6 bytes by the sizeof operator. Code is as follows: #include <iostream> using namespace std ; struct IntAndChar { int a ; unsigned char b ; }; struct ThreeShorts { unsigned short a ; unsigned short b ; unsigned short c ; }; int main() { cout<<sizeof(IntAndChar)<<endl; // outputs '8' cout<<sizeof

Why sizeof int is wrong, while sizeof(int) is right?

守給你的承諾、 提交于 2019-11-27 17:28:45
We know that sizeof is an operator used for calculating the size of any datatype and expression, and when the operand is an expression, the parentheses can be omitted. int main() { int a; sizeof int; sizeof( int ); sizeof a; sizeof( a ); return 0; } the first usage of sizeof is wrong, while others are right. When it is compiled using gcc, the following error message will be given: main.c:5:9: error: expected expression before ‘int’ My question is why the C standard does not allow this kind of operation. Will sizeof int cause any ambiguity? The following could be ambiguous: sizeof int * + 1 Is

sizeof()计算结构体的大小

江枫思渺然 提交于 2019-11-27 16:52:22
简要说明:结构体成员按照定义时的顺序依次存储在连续的内存空间,但是结构体的大小并不是简单的把所有成员大小相加,而是遵循一定的规则,需要考虑到系统在存储结构体变量时的地址对齐问题。 一、没有成员的结构体占用的空间是多少个字节? 答案是:1个字节。 这就是实例化的原因(空类同样可以被实例化),每个实例在内存中都有一个独一无二的地址,为了达到这个目的,编译器往往会给一个空类或空结构体(C++中结构体也可看为类)隐含的加一个字节,这样空类或空结构体在实例化后在内存得到了独一无二的地址,所以空类所占的内存大小是1个字节。 二、首先介绍一个相关的概念——偏移量 struct stru { int a; //start address is 0 char b; //start address is 4 int c; //start address is 8 }; 偏移量指的是结构体变量中成员的地址和结构体变量地址的差。结构体大小等于最后一个成员的偏移量加上最后一个成员的大小。显然,结构体变量中第一个成员的地址就是结构体变量的首地址。比如上面的结构体,第一个成员a的偏移量为0。第二个成员b的偏移量是第一个成员的偏移量加上第一个成员的大小(0+4),其值为4;第三个成员c的偏移量是第二个成员的偏移量应该是加上第二个成员的大小(4+1)。 三、在实际中,存储变量时地址要求对齐

Netlink机制详解

爷,独闯天下 提交于 2019-11-27 16:52:08
使用netlink机制在内核与应用程序之间通信 转载:https://blog.csdn.net/zoe6553/article/details/8026033 前一段时间,在开发一个驱动程序的过程中,需要在驱动程序与应用程序之间进行通信。其中驱动程序在接收到一个硬件中断之后通知应用程序进行相应的处理。为 解决此类问题,驱动程序提供了几种机制:(1)使用copy_to_user/copy_from_user方法,缺点是通信响应时间过长(2)使用信 号,但是限于字符设备(3)使用netlink。 在linux2.4之后引入了netlink机制,它将是Linux用户态与内核态交流的主要方法之一。netlink 的特点是对中断过程的支持,也就是说,可以在中断程序中直接调用netlink相关函数。它在内核空间接收用户空间数据时不再需要用户自行启动一个内核线 程,而是通过另一个软中断调用用户事先指定的接收函数。netlink的通信过程如下: 下面分用户空间与内核空间2个部分讲述netlink的基本使用方法 1. 用户空间的程序 用户的应用程序使用标准套接字socket与内核空间进行通讯,标准socket API的函数, socket()、 bind()、sendmsg()、recvmsg() 和 close()很容易地应用到 netlink socket。 程序代码: #define

Why can't I use sizeof in a preprocessor condition?

爷,独闯天下 提交于 2019-11-27 15:29:58
I understand that sizeof is an operator, which is evaluated at compile time to an integer constant. But it seem it can not be used in the #if preprocessor directive like: #if 4 == sizeof(int) typedef int Int32; #endif (cygwin-gcc 3.4.4 as well as Visual C++ 6.0 report compile errors) Why is such usage not allowed? Because sizeof is evaluated at compilation time while directives are evaluated before compilation, and the part that does that is not the compiler, so it won't understand what sizeof means. The sizeof is a C operator. You can't use C code in preprocessor directives. Preprocessor

(英语能力优秀者优先阅读)C——内存管理——摘自wiki

浪子不回头ぞ 提交于 2019-11-27 15:08:21
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

Size of Primitive data types

自作多情 提交于 2019-11-27 15:06:25
On what exactly does the size of a primitive data type like int depend on? Compiler Processor Development Environment Or is it a combination of these or other factors? An explanation on the reason of the same will be really helpful. EDIT: Sorry for the confusion..I meant to ask about Primitive data type like int and not regarding PODs, I do understand PODs can include structure and with structure it is a whole different ball game with padding coming in to the picture. I have corrected the Q, the edit note here should ensure the answers regarding POD don't look irrelevant. Alex B I think there

Different sizeof results

坚强是说给别人听的谎言 提交于 2019-11-27 14:41:11
Why does n not equal to 8 in the following function? void foo(char cvalue[8]) { int n = sizeof (cvalue); } But n does equal to 8 in this version of the function: void bar() { char cvalue[8]; int n = sizeof (cvalue); } Because you can't pass entire arrays as function parameters in C. You're actually passing a pointer to it; the brackets are syntactic sugar. There are no guarantees the array you're pointing to has size 8, since you could pass this function any character pointer you want. // These all do the same thing void foo(char cvalue[8]) void foo(char cvalue[]) void foo(char *cvalue) C and

Is there a bit-equivalent of sizeof() in C?

﹥>﹥吖頭↗ 提交于 2019-11-27 14:27:44
问题 Sizeof() doesn't work when applied to bitfields: # cat p.c #include<stdio.h> int main( int argc, char **argv ) { struct { unsigned int bitfield : 3; } s; fprintf( stdout, "size=%d\n", sizeof(s.bitfield) ); } # gcc p.c -o p p.c: In function ‘main’: p.c:5: error: ‘sizeof’ applied to a bit-field ...obviously, since it can't return a floating point partial size or something. However, it brought up an interesting question. Is there an equivalent, in C, that will tell you the number of bits in a