sizeof

C语言中的内存对齐

二次信任 提交于 2019-11-27 06:22:00
什么是内存对齐 内存对齐”应该是编译器的“管辖范围”。编译器为程序中的每个数据单元安排在适当的位置上。 出现原因( 老生常谈的两句话 ) 平台原因(移植原因):不是所有的硬件平台都能访问任意地址上的任意数据的;某些硬件平台只能在某些地址处取某些特定类型的数据,否则抛出硬件异常。 性能原因:数据结构(尤其是栈)应该尽可能地在自然边界上对齐。原因在于,为了访问未对齐的内存,处理器需要作两次内存访问;而对齐的内存访问仅需要一次访问。 对齐规则 第一个成员放在偏移量(offset)为0的地方。 以后的每个成员的偏移量: min(#pragma pack()指定的数值,当前成员的大小) 的倍数中。 每个成员对齐后,本身也要对齐,整体的偏移量:min(#pragma pack()指定的数值,结构体最大数据成员的大小)。 关于 #pragma pack 通过 #pragma pack(n) 来设定变量以n字节对齐方式 n字节对齐就是说变量存放的起始地址的偏移量有两种情况 如果n大于等于该变量所占用的字节数,那么偏移量必须满足默认的对齐方式 。 果n小于该变量的类型所占用的字节数,那么偏移量为n的倍数,不用满足默认的对齐方式 。 32位系统默认为4,64位系统默认为8。 举个例子🌰 #include<stdio.h> #pragma pack(4) struct Node{ char a;

Getsavefilename()样例代码片段

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 06:04:02
Getsavefilename()样例代码片段 OPENFILENAME ofn ; TCHAR szFile[MAX_PATH] ; ZeroMemory(&ofn,sizeof(ofn)) ; ofn .lStructSize = sizeof(ofn) ; ofn .hwndOwner = m_hWnd ; ofn .lpstrFile = szFile ; ofn .lpstrFile [ 0 ] = _T( '\0' ) ; ofn .nMaxFile = sizeof(szFile) ; ofn .lpstrFilter = _T( "Text\0*.txt\0" ) ; ofn .nFilterIndex = 1 ; ofn .lpstrFileTitle = NULL ; ofn .nMaxFileTitle = 0 ; ofn .Flags = OFN_SHOWHELP | OFN_OVERWRITEPROMPT ; CString strFile ; if (GetSaveFileName(&ofn)) { strFile .Format (_T( "%s" ),szFile) ; } else return ; if (strFile .Find (_T( '.' ))!= - 1 ) { strFile = strFile .Left (strFile

变量地址举例

℡╲_俬逩灬. 提交于 2019-11-27 06:02:40
1 # include <stdio.h> 2 3 void outArr( int * pArr, int len) 4 { 5 int i; 6 pArr[ 1 ] = 100 ; // 此时pArr[1]等价于*(pArr+1)等价于a[1]等价于*(a+1); 7 8 for (i= 0 ; i<len; i++) 9 printf( " %d\n " , pArr[i]); 10 11 } 12 13 int main( void ) 14 { 15 int a[ 3 ] = { 1 , 2 , 3 }; 16 outArr(a, 3 ); 17 18 return 0 ; 19 } 20 /* 21 在Vc++6.0中显示的结果是: 22 =================================================== 23 1 24 100 25 3 26 =================================================== 27 */ 1 # include <stdio.h> 2 // sizeof(数据类型) 功能:返回值就是该数据类型所占的字节数 3 // sizeof(变量名) 功能:返回值就是该变量所占的字节数 4 int main( void ) 5 { 6 int i = 2 ; 7 char j

sizeof single struct member in C

一曲冷凌霜 提交于 2019-11-27 06:00:15
I am trying to declare a struct that is dependent upon another struct. I want to use sizeof to be safe/pedantic. typedef struct _parent { float calc ; char text[255] ; int used ; } parent_t ; Now I want to declare a struct child_t that has the same size as parent_t.text . How can I do this? (Pseudo-code below.) typedef struct _child { char flag ; char text[sizeof(parent_t.text)] ; int used ; } child_t ; I tried a few different ways with parent_t and struct _parent , but my compiler will not accept. As a trick, this seems to work: parent_t* dummy ; typedef struct _child { char flag ; char text

Do parentheses make a difference when determining the size of an array?

余生长醉 提交于 2019-11-27 05:48:43
问题 The following program prints the same number twice on gcc 4.8.2: #include <stdio.h> int main() { char a[13]; printf("sizeof a is %zu\n", sizeof a ); printf("sizeof(a) is %zu\n", sizeof(a)); } According to this reddit post, gcc is not standard-conformant in this respect, because a parenthesized expression is not on the list of exceptions for when array-to-pointer decay does not happen. Is this guy correct? Here is the relevant standard quote: Except when it is the operand of the sizeof

动态规划23题解析

末鹿安然 提交于 2019-11-27 05:45:37
最近两周做了动态规划的23道经典好题,涉及到区间、树形、数位等三种动态规划类型,现在将这23道题的题解写在下面,方便大家借鉴以及我加深记忆。 upd at:20190815 13:41.T14周年纪念晚会 1、石子合并 经典的区间DP问题,枚举合并的堆数作为阶段,设f[i][j]表示i->j这段区间内的最优方案,考虑在这段区间内枚举断点k,不难得到f[i][j]=min(f[i][k]+f[k+1][j]+sum(i,j))(最大值同理)。破环为链后直接进行DP即可。 #include<iostream> #include<cstring> #include<cstdio> using namespace std; int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=0;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();} if(f)return x;return -x; } int n,a[1005],f[505][505],g[505][505],prefix[505],minn=21374404,maxn; int main() { //freopen("A.in","r

Sizeof arrays and pointers

老子叫甜甜 提交于 2019-11-27 05:38:23
Here is my sample code #include<stdio.h> void main() { int arr[]={1,2,3,4,5,6}; char *ptr,a; a='c'; ptr=&a; int *ptr1,a1; a1=4; ptr1=&a1; printf("%d %d %d",sizeof(arr), sizeof(ptr1), sizeof(ptr)); } Now, as far as I understand, size of will tell me the size required to store the variable, now the output for this one is 24 4 4 Why is the size of arr=24 , after all it's just a pointer and it should be having size =4 ? Thanks. "...after all it's just a pointer..."? No. Array is not a pointer. Array is an array object: a solid continuous block of memory that stores the array elements, no pointers

Why is −1 > sizeof(int)?

∥☆過路亽.° 提交于 2019-11-27 05:34:05
Consider the following code: template<bool> class StaticAssert; template<> class StaticAssert<true> {}; StaticAssert< (-1 < sizeof(int)) > xyz1; // Compile error StaticAssert< (-1 > sizeof(int)) > xyz2; // OK Why is -1 > sizeof(int) true? Is it true that -1 is promoted to unsigned(-1) and then unsigned(-1) > sizeof(int) . Is it true that -1 > sizeof(int) is equivalent to -1 > size_t(4) if sizeof(int) is 4. If this is so why -1 > size_t(4) is false? Is this C++ standard comformant? The following is how standard (ISO 14882) explains abort -1 > sizeof(int) Relational operator `>' is defined in 5

sizeof java object

陌路散爱 提交于 2019-11-27 05:21:29
How can we find out the size of a java object?? Example: class Person{ String name; int age; public Person(String n, int a){ name = n; age = a; } } Person person = new Person("Andy", 30); now how can I know the size of person object?? Thank you The question is not meaningful, at least not without further context. The notion of "size" in Java is only reasonably well defined for primitives: A byte is 8 bit (unsurprisingly) an int is 32 bit, a long 64bit, etc. (see e.g. http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html for a full list). For object instances, it's more

Access struct members as if they are a single array?

荒凉一梦 提交于 2019-11-27 05:11:56
I have two structures, with values that should compute a pondered average, like this simplified version: typedef struct { int v_move, v_read, v_suck, v_flush, v_nop, v_call; } values; typedef struct { int qtt_move, qtt_read, qtt_suck, qtd_flush, qtd_nop, qtt_call; } quantities; And then I use them to calculate: average = v_move*qtt_move + v_read*qtt_read + v_suck*qtt_suck + v_flush*qtd_flush + v_nop*qtd_nop + v_call*qtt_call; Every now and them I need to include another variable. Now, for instance, I need to include v_clean and qtt_clean . I can't change the structures to arrays: typedef