gcc 变量类型大小 练习

/* testmini.c -- very simple test program for the miniLZO library

 */


#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>

#ifndef uint8_t
#define uint8_t  unsigned char
#endif
     typedef unsigned __int64   lzo_1;
/*	 unsigned __int64 就是个无符号 64 整形。可能比你前面用的那些类型位数长一些。*/
     typedef  __int64          lzo_2;
	      typedef signed __int64          lzo_3;
/*     typedef lzo_ullong_t       lzo_3;
       typedef lzo_llong_t        lzo_4;*/
     typedef unsigned int       lzo_5;
     typedef int          lzo_6;
     typedef unsigned long      lzo_7;
     typedef long          lzo_8;
	 
    typedef struct {
        lzo_1 lzo_11[1]; 

    } _context1; 
	    typedef struct {

        lzo_2 lzo_12[1]; 

    } _context2; 
	    typedef struct {

        lzo_5 lzo_15[1];

    } _context5; 
	    typedef struct {

		lzo_6 lzo_16[1];

    } _context6; 
	    typedef struct {

		lzo_7 lzo_17[1];

    } _context7; 
	    typedef struct {
		lzo_8 lzo_18[1];
    } _context8; 

/* 96 */	
	 
    typedef struct {
        uint8_t key[32]; 
        uint8_t enckey[32]; 
        uint8_t deckey[32];
    } aes256_context; 
/* 96 */	

struct student
{
    char name[7];
    int id; // 类型出错
    char subject[5];
} student __attribute__ ((aligned(4))); 
/*   20
 ---------8+4+8==20------
*/
struct student1
{
    char name[7];
    int id; // 类型出错
    char subject[5];
} student1 __attribute__ ((packed));
/*
test.c:56:1: warning: 'packed' attribute ignored [-Wattributes]
 } student1 __attribute__ ((packed));
*/
/*  20
-------7+4+5==16---------
*/

#pragma pack(push)
#pragma pack(1)
struct a1
{
    short a;
    int b;
    char c;
}A1;
#pragma pack(pop)/*7 */

struct a2
{
    short a;
    int b;
    char c;
}A2;              /*12*/
#pragma pack(2)  
struct c 
{ 
     char b; 
     int      a; 
     short c; 
}C; 
#pragma pack() /*8 */

#pragma pack(1) 
struct d 
{ 
      char b; 
      int      a; 
      short c; 
}D; 
#pragma pack()/* 7 */
/*
struct stu3
                {
                short i;
                struct 
                {
                   char c;
                   int j;
                } stu3_1; 
                int k;
          }*/
/*
stu3_1最大成员为j,大小为4.因此c的偏移量为4的整数倍。所以实际上c的偏移量不为2,编译器会在i的后面补2字节,使c的偏移量为4.
总结:在定义结构体类型时需要考虑到字节对齐的情况,不同的顺序会影响到结构体的大小。
--------------------- 
作者:w狸猫 
来源:CSDN 
原文:https://blog.csdn.net/wangtong95/article/details/51451452 
版权声明:本文为博主原创文章,转载请附上博文链接!
*/
/*
#ifndef PACK
#define PACK __attribute__ ((packed))
#endif

struct a
{
     char a;
     int b;
}PACK A;
	*/
int main(int argc, char *argv[])
{ /*
If your compiler isn't C99 compliant, get a different compiler. (Yes, I'm looking at you, Visual Studio.)

PS: If you are worried about portability, don't use %lld. That's for long long, but there are no guarantees that long long actually is the same as _int64 (POSIX) or int64_t (C99).

Edit: Mea culpa - I more or less brainlessly "search & replace"d the _int64 with int64_t without really looking at what I am doing. Thanks for the comments pointing out that it's uint64_t, not unsigned int64_t. Corrected.
_Long128, int128_t and uint128_t
could use uint64_t u64; sprintf( buf, "%llu", (unsigned long long) u64); as unsigned long long is at least 64 bits.
You need to use %I64u with Visual C++.

However, on most C/C++ compiler, 64 bit integer is long long. Therefore, adopt to using long long and use %llu.
*/
    lzo_1 dbFileSize = 18446744073709551615u;
    lzo_2 fileSize = -9223372036854775808;
    char buf[128];
    memset(buf, 0x00, 128);
    sprintf( buf, "\nOD DB File Size = %" PRId64 " bytes \t"
                  " XML file size = %" PRIu64 " bytes\n"
                  , fileSize, dbFileSize );
    printf( "The string is %s\n", buf ); 
/* 
lzo_1 aaa1 = 18446744073709551615;
lzo_2 aaa2 = -9223372036854775808;
scanf('%lld',&aaa1);
printf('%lld',aaa1); 
scanf('%lld',&aaa2);
printf('%lld',aaa2); 

scanf('%I64d',&a);
printf('%I64d',a);
scanf('%lld',&a);
printf('%lld',a); 
linux下是 printf("%llu\n",a); windows下应该是 %l64u吧,你试试看
使用无符号数时,将"%lld"改成"%llu"即可。

printf("sizeof(_context1):%d\n",sizeof(_context1));
printf("sizeof(_context2):%d\n",sizeof(_context2));
printf("sizeof(_context5):%d\n",sizeof(_context5));
printf("sizeof(_context6):%d\n",sizeof(_context6));
printf("sizeof(_context7):%d\n",sizeof(_context7));
printf("sizeof(_context8):%d\n",sizeof(_context8));
    printf("sizeof(student):%d\n",sizeof(student));
	printf("sizeof(student1):%d\n",sizeof(student1));
	printf("sizeof(A1):%d\n",sizeof(A1));
    printf("sizeof(A2):%d\n",sizeof(A2));
	printf("sizeof(aes256_context):%d\n",sizeof(aes256_context));
	printf("sizeof(c):%d\n",sizeof(C));
    printf("sizeof(d):%d\n",sizeof(D));
	

sizeof(_context1):8
sizeof(_context2):8
sizeof(_context5):4
sizeof(_context6):4
sizeof(_context7):4
sizeof(_context8):4
 
sizeof(student):20
sizeof(student1):20
sizeof(A1):7
sizeof(A2):12
sizeof(aes256_context):96
sizeof(c):8
sizeof(d):7*/

    printf("\nminiLZO simple compression test passed.\n");
    return 0;
}
/*
gcc -m32 -I. -s -Wall -O2 -fomit-frame-pointer -o test32 test.c
gcc -m64 -I. -s -Wall -O2 -fomit-frame-pointer -o test64 test.c
*/




/*
一:#pragma warning指令

该指令允许有选择性的修改编译器的警告消息的行为
指令格式如下:
#pragma warning( warning-specifier : warning-number-list [; warning-specifier : warning-number-list...]
#pragma warning( push[ ,n ] )
#pragma warning( pop )

主要用到的警告表示有如下几个:

once:只显示一次(警告/错误等)消息
default:重置编译器的警告行为到默认状态
1,2,3,4:四个警告级别
disable:禁止指定的警告信息
error:将指定的警告信息作为错误报告

如果大家对上面的解释不是很理解,可以参考一下下面的例子及说明

#pragma warning( disable : 4507 34; once : 4385; error : 164 )
等价于:
#pragma warning(disable:4507 34) // 不显示4507和34号警告信息
#pragma warning(once:4385)        // 4385号警告信息仅报告一次
#pragma warning(error:164)        // 把164号警告信息作为一个错误。
同时这个pragma warning 也支持如下格式:
#pragma warning( push [ ,n ] )
#pragma warning( pop )
这里n代表一个警告等级(1---4)。
#pragma warning( push )保存所有警告信息的现有的警告状态。
#pragma warning( push, n)保存所有警告信息的现有的警告状态,并且把全局警告
等级设定为n。  
#pragma warning( pop )向栈中弹出最后一个警告信息,在入栈和出栈之间所作的
一切改动取消。例如:
#pragma warning( push )
#pragma warning( disable : 4705 )
#pragma warning( disable : 4706 )
#pragma warning( disable : 4707 )
#pragma warning( pop )

在这段代码的最后,重新保存所有的警告信息(包括4705,4706和4707)

在使用标准C++进行编程的时候经常会得到很多的警告信息,而这些警告信息都是不必要的提示,
所以我们可以使用#pragma warning(disable:4786)来禁止该类型的警告

在vc中使用ADO的时候也会得到不必要的警告信息,这个时候我们可以通过
#pragma warning(disable:4146)来消除该类型的警告信息

二:#pragma pack()
注:如果设置的值比结构体中字节最长的类型还要大,则这个变量(注意仅针对这一个变量)只按照它的字节长度对齐,即不会出现内存浪费的情况。请参见(4)。
(1)
#pragma pack(1)        //每个变量按照1字节对齐
struct A
{
char x;    //aligned on byte boundary 0
int y;     //aligned on byte boundary 1
}a;
sizeof(a)==5
(2)
#pragma pack(2)        //每个变量按照2字节对齐
struct A
{
char x;    //aligned on byte boundary 0
int y;     //aligned on byte boundary 2
}a;
sizeof(a)==6
(3)
#pragma pack(4)        //每个变量按照4字节对齐
struct A
{
char x;    //aligned on byte boundary 0
int y;     //aligned on byte boundary 4
}a;
sizeof(a)==8
(4)
#pragma pack()        //默认,相当于#pragma pack(8) 每个变量按照8字节对齐
struct A
{
char x;    //aligned on byte boundary 0
int y;     //aligned on byte boundary 4
}a;
sizeof(a)==8
但是这里y的大小是4字节,所以不会按照8字节对齐,否则将造成1个int空间的浪费

三.#pragma comment
The following pragma causes the linker to search for the EMAPI.LIB library while linking. The linker searches first in the current working directory and then in the path specified in the LIB environment variable:
#pragma comment( lib, "emapi" )


四.#pragma deprecated
When the compiler encounters a deprecated symbol, it issues C4995:
void func1(void) {}
void func2(void) {}
int main() {
   func1();
   func2();
   #pragma deprecated(func1, func2)
   func1();   // C4995
   func2();   // C4995
}


五.#pragma message
The following code fragment uses the message pragma to display a message during compilation:
#if _M_IX86 == 500
#pragma message( "Pentium processor build" )
#endif
*/

  编译参数 

gcc -m32 -s -Wall -O2 -fomit-frame-pointer -c minilzo.c -o gcc32.o
gcc -m64 -s -Wall -O2 -fomit-frame-pointer -c minilzo.c -o gcc32.o

上一篇:c++字节对齐编译器指令#pragma


下一篇:SpringCloud入门之Maven系统安装及配置