sizeof

C结构体之位域(位段)

狂风中的少年 提交于 2020-01-04 14:37:18
几篇较全面的位域相关的文章: http://www.uplook.cn/blog/9/93362/ C/C++位域(Bit-fields)之我见 C中的位域与大小端问题 内存对齐全攻略–涉及位域的内存对齐原则 本文主要对位域相关知识进行了一下梳理,参考如下: C语言中的位域 史上最全的C位域总结 2 C结构体之位域(位段) C/C++中以一定区域内的位(bit)为单位来表示的数据成为位域,位域必须指明具体的数目。 位域的作用主要是节省内存资源,使数据结构更紧凑。 1. 一个位域必须存储在同一个字节中,不能跨两个字节,故位域的长度不能大于一个字节的长度。 如一个字节所剩空间不够存放另一位域时,应从下一单元起存放该位域。也可以有意使某位域从下一单元开始。例如: struct BitField { unsigned int a:4; //占用4个二进制位; unsigned int :0; //空位域,自动置0; unsigned int b:4; //占用4个二进制位,从下一个存储单元开始存放; unsigned int c:4; //占用4个二进制位; unsigned int d:5; //占用5个二进制位,剩余的4个bit不够存储4个bit的数据,从下一个存储单元开始存放; unsigned int :0; //空位域,自动置0; unsigned int e:4; /

音视频即时通讯开发中音频模式的采集

早过忘川 提交于 2020-01-04 06:43:14
在很多即时通讯应用中,会根据应用场景的不同,需要对音频输入源进行选择,不同的应用场景对应不同的音频工作模式。需要支持多种音频工作(采集)模式,包括: 1 、发言模式(默认) :自动选择麦克风为音频输入源设备,用户说话的声音被麦克风采集,启动音频特效处理(包括:回音消除、静音检测、噪音抑制、自动增溢),该模式通常应用于互动交流,用户发言讨论等场合; 2 、放歌模式 :自动选择立体声混音输入源设备,本地计算机所播放的声音被采集,同时SDK内部会自动屏蔽其它用户的声音(如果不屏蔽,则用户的声音会被采集下来,并回传给用户,用户那边将会听到回音),SDK内部会自动关闭音频特效处理,该模式通常应用于向其他用户放歌,而不用关心其他用户发言的场合; 3 、卡拉OK模式 :自动选择立体声混音和麦克风两个输入源设备(该特性与硬件相关,有些声卡不支持同时采集麦克风和立体声混音),本地计算机所播放的声音和用户说话的声音将会被采集,同时SDK内部会自动屏蔽其它用户的声音,SDK内部会自动关闭音频特效处理,该模式通常应用于向其他用户放歌,同时自己用麦克风伴唱,而不用关心其它用户发言的场合; 4 、线路输入模式 :自动选择线路输入源设备,通过线路输入的声音将被采集(通常是指将外部的DV、DVD、TV等设备的音频输出端子接入声卡的LineIn口的应用),SDK内部会自动关闭音频特效处理

How does sizeof work for int types?

北城以北 提交于 2020-01-04 05:43:29
问题 I have a small program which compares (1) sizeof, (2) numeric_limits::digits, (3) and the results of a loop in an effort to make sure they all report the same thing regarding the size of the "int types" on any C++ implementation. However because I don't know about the internals of sizeof, I have to wonder if it is just reporting numeric_limits::digits. Thanks 回答1: Most likely sizeof() on most compilers causes the compiler to look the given type (or object's type) up in its internal type table

How can I calculate the number of elements in a char array?

我的未来我决定 提交于 2020-01-04 02:06:20
问题 I was trying to calculate the number of elements in an array, and was told that the line int r = sizeof(array) / sizeof(array[0]) would give me the number of elements in the array. And I found the method does work, at least for int arrays. When I try this code, however, things break. #include <iostream> #include <Windows.h> using namespace std; int main() { char binaryPath[MAX_PATH]; GetModuleFileName(NULL, binaryPath, MAX_PATH); cout << "binaryPath: " << binaryPath << endl; cout << "sizeof

sizeof an integer expression in C

白昼怎懂夜的黑 提交于 2020-01-03 18:58:51
问题 To my semi-surprise with Xcode compiling C (gnu11) #include <stdio.h> int main(int argc,char**argv) { short s = 1; printf( "%zd %zd %zd\n", sizeof(s), sizeof(s*s), sizeof(s?s:s)); return 0; } Produces the output 2 4 4 I was expecting 2 2 2 or possibly 2 4 2 Why is this? 回答1: I recalled from K&R that any integer expression smaller than an int is promoted to int. I found this in Standard (C89): 3.2.1.1 Characters and integers A char, a short int, or an int bit-field, or their signed or unsigned

How to get the size of an object via reference?

不想你离开。 提交于 2020-01-03 15:33:10
问题 Suppose I have a class class Foo { : : } I have another function void getf( Foo &f) { : : std::cout<<sizeof f<<std::endl; } After I process the data and assign a lot of data to f (vector included in Foo members), I need the size of f object However, as what I did above, I always get 16, which is the size of a reference here. Did I do anything wrong? How to do that? Thanks! 回答1: sizeof returns the size of a type. Often time, as in the case of a Vector , the size of the type does not

C++: size of a char array using sizeof

自作多情 提交于 2020-01-03 08:30:09
问题 Look at the following piece of code in C++: char a1[] = {'a','b','c'}; char a2[] = "abc"; cout << sizeof(a1) << endl << sizeof(a2) << endl; Though sizeof(char) is 1 byte, why does the output show sizeof(a2) as 4 and not 3 (as in case of a1 )? 回答1: C-strings contain a null terminator, thus adding a character. Essentially this: char a2[] = {'a','b','c','\0'}; 回答2: That's because there's an extra null '\0' character added to the end of the C-string, whereas the first variable, a1 is an array of

What arguments does the sizeof operator take in C?

梦想与她 提交于 2020-01-02 14:39:09
问题 [ Original title referred to 'sizeof function'. ] I tried these and they all worked: char *p; printf("Size of *p is %d\n",sizeof(*p)); //result =1 printf("Size of p is %d\n",sizeof( p)); //result =4 printf("Size of p is %d\n",sizeof(&p)); //result =4 I wonder why the first printf is 1, the 2nd and 3rd is 4? So what arguments can sizeof can actually take? 回答1: It takes a type. sizeof(char) is always one. The variable p itself is a pointer, and on your platform that has a size of 4. Then you do

What's the size of this C# struct?

冷暖自知 提交于 2020-01-02 01:12:08
问题 Is it 12 bytes or 16 bytes when stored in a List<DataPoint> ? public struct DataPoint { DateTime time_utc; float value; } Is there any sizeof function in C#? 回答1: Take a look at @Hans Passant's answer here for interesting background on this issue, esp. with regard to the limitations of Marshal.Sizeof . 回答2: Marshal.SizeOf() http://msdn.microsoft.com/en-us/library/y3ybkfb3.aspx 回答3: The CLR is free to lay out types in memory as it sees fit. So it's not possible to directly give "the" size.