gcc 变量类型大小 练习 远离 cygwin64 需要带dll
/* 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
gcc 变量类型大小 练习 远离 cygwin64 需要带dll的更多相关文章
- c语言中的数据变量类型,大小
C中有哪些数据类型? 回答: 有两种类型的数据类型,用户定义和预定义.预定义的数据类型是int,char,float,double等,用户使用标签struct,union或enum创建用户定义的数据类 ...
- C语言各类型大小,结构体大小 sizeof(struct A)
C语言类型大小总览 编译器pack指令 #pragma pack(n)——定义n字节对齐 C++固有类型的对齐取编译器对齐与自身大小中较小的一个 32位C++默认8字节对齐.gcc编译器默认4字节对齐 ...
- C语言的类型大小
C语言的类型大小 设计程序的时候我们一般会考虑的尽量的周全,尤其是像C这样的静态类型语言. 有一些溢出的问题就源于没有搞清楚变量的大小范围,所以我们编写的时候需要特别注意 C的整形(整数类型)大小 C ...
- C++数据类型和变量类型。
数据类型 数字是自由的[不只属于某个类型]!但是它可以有不同的身份!int.char.float.double等身份.它以不同的身份[存储规则]存储在内存的某个位置内部! 变量类型 内存编号是不会变的 ...
- CUDA1.1-函数类型限定符与变量类型限定符
这部分来自于<CUDA_C_Programming_Guide.pdf>,看完<GPU高性能变成CUDA实战>的第四章,觉得这本书还是很好的,是一种循序渐进式的书,值得看,而不 ...
- 20151010 C# 第一篇 变量类型
20151010 变量类型: 1. 值类型:变量本身直接存储数据 整数类型:代表没有小数点的整数数值 类型 说明 范围 sbyte 8位有符号整数 -128——127 short 16位有符号整数 - ...
- 深入理解计算机各种类型大小(sizeof)
深入理解计算机各种类型大小(sizeof) // Example of the sizeof keyword size_t i = sizeof( int ); struct align_dep ...
- 【C语言探索之旅】 第二部分第六课:创建你自己的变量类型
内容简介 1.课程大纲 2.第二部分第六课: 创建你自己的变量类型 3.第二部分第七课预告: 文件读写 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C ...
- Python中的高级变量类型
高级变量类型 目标 列表 元组 字典 字符串 公共方法 变量高级 知识点回顾 Python 中数据类型可以分为 数字型 和 非数字型 数字型 整型 (int) 浮点型(float) 布尔型(bool) ...
随机推荐
- mysql5.7根据.frm和.ibd文件恢复表结构和数据
一.恢复表结构 1.环境:Windows .mysql5.7:首先创建一个数据库,可以通过navicat来创建: 2.使用当前创建的数据库:use ww; 3.随意创建一张表,但是这张表的名字 ...
- 【WIP】swift3的timer的用法
创建: 2017/10/14 更新: 2017/10/14 标题加上[WIP],补充创建时间 回家再写
- 机器学习--DIY笔记与感悟--②决策树(1)
在完成了K临近之后,今天我们开始下一个算法--->决策树算法. 一.决策树基础知识 如果突然问你"有一个陌生人叫X,Ta今天需要带伞吗?", 你一定会觉得这个问题就像告诉你& ...
- C#大话设计模式学习总结
如有雷同,不胜荣欣,如转载,请注明 C#大话设计模式学习总结 一.工厂模式 面向对象的三个特性:封装,继承和多态 1.封装 Class Operate { privatedouble _numberA ...
- AForge.NET .NET2.0程序集无法在.net 4.0 中运行的解决方案
如有雷同,不胜荣欣,若转载,请注明 最近在项目上一直使用.net4.0 framework,突然发现一个AForge.net中使用ffmepeg下的一个dll时,提示只能在2.0下运行,在众多MSDN ...
- poj1273 Drainage Ditches 基础网络流
#include <iostream> #include <queue> using namespace std; ][]; ]; //路径上每个节点的前驱节点 ]; int ...
- [WOJ1583]向右看齐
题目链接: WOJ1583 题目分析: 大水题--我就来水个题解 倒序扫,单调栈维护单减序列,每个对象的答案是栈里它下面那个元素 代码: #include<bits/stdc++.h> # ...
- 题解报告:hdu 6441 Find Integer(费马大定理+智慧数)
Problem Description people in USSS love math very much, and there is a famous math problem .give you ...
- P1482 Cantor表(升级版)
题目描述 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 … 2/1 2/2 2/3 2/4 … ...
- 刷ID卡的就餐系统
需求分析:公司旧的考勤系统,缺 “就餐”功能模块,不能查询和统计每天的就餐人数.故需开发一个简易的“刷ID卡的就餐系统”,三 部 分组成,一部分为人事资料的增删改查,二部分为处理从“刷卡就餐机”采集的 ...