不用sizeof()函数求当前主机上的一个int占用几个字节

1)宏定义实现:

   #define MySizeof(Value) (char*)(&Value + 1) - (char*)&Value

   (char*)&Value返回Value的地址的第一个字节, (char*)(&Value + 1)返回的是Value的地址的下一个地址的第一个字节

#include <iostream>
using namespace std;
#define my_sizeof(L_Value)  (char* )(&L_Value + 1) - (char* )&L_Value
int main()
{
    int i;
    double f;
    double a[4];
    double* q;
    cout<<my_sizeof(i)<<endl;
    cout<<my_sizeof(f)<<endl;
    cout<<my_sizeof(a)<<endl;
    cout<<my_sizeof(q)<<endl;
    cout<<my_sizeof("aadf")<<endl;
    return 0;
}

2 )

#include <iostream>
using namespace std;
template <class Any>
int LengofArray(Any* p)
{
    return int(p+1) - int(p);
}

int main()
{
    int* i;
    double* q;
    char a[10];
    cout<<LengofArray(i)<<endl;
    cout<<LengofArray(q)<<endl;
    cout<<LengofArray(&a)<<endl;
    return 0;
}
时间: 2024-07-30 17:58:32

不用sizeof()函数求当前主机上的一个int占用几个字节的相关文章

不用聚合函数求最高工资

对于emp 表,不用聚合函数求出最高工资 如果使用聚合函数的话,求出最高工资比较方便 select max(sal) from emp; 如果不使用聚合函数的话,该从哪个方向出发呢? 可以排序,然后从排序后的结果中取工资最高的:可以取出除最高工资之外的所有工资,然后再排除,剩下最高工资. method1  按工资收入降序排列 select * from emp order by sal desc 工资最高的5000 就排在第一个,接下来再取第一个即可 select a.sal from (sel

sizeof()函数求各类型变量所占空间的方法

#include "stdafx.h" #include <iostream> using namespace std; void func(char str[100]) { cout<<sizeof(str)<<endl; } int main() { char str[100]; func(str); //str传入函数做参数,做sizeof运算时被当做指针 返回4 cout<<sizeof(str)<<endl; //返

在相同的主机上创建一个duplicate数据库

RMAN> duplicate target database to dupdb; Starting Duplicate Db at 25-JUN-2015 08:18:24 using target database control file instead of recovery catalog allocated channel: ORA_AUX_DISK_1 channel ORA_AUX_DISK_1: SID=396 device type=DISK contents of Memo

编写一个函数,在页面上输出一个N行M列的表格,表格内容填充1~100的随机数字

function tab(n,m){ document.write("<table border=1>"); for(var i=1;i<=n;i++){ document.write("<tr>"); for(var j=1;j<=m;j++){ document.write("<td>"); document.write(numRandom(1,100)); document.write(&qu

C/C++ sizeof函数解析——解决sizeof求结构体大小的问题

C/C++中不同数据类型所占用的内存大小 32位                 64位 char               1                    1 int                  4             大多数4,少数8 short              2                    2 long               4                    8 float               4              

sizeof函数详解

原文链接http://blog.csdn.net/wzy198852/article/details/7246836 sizeof,一个其貌不扬的家伙,引无数菜鸟竟折腰,小虾我当初也没少犯迷糊,秉着“辛苦我一个,幸福千万人”的伟大思想,我决定将其尽可能详细的总结一下.但当我总结的时候才发现,这个问题既可以简单,又可以复杂,所以本文有的地方并不适合初学者,甚至都没有必要大作文章.但如果你想“知其然,更知其所以然”的话,那么这篇文章对你或许有所帮助.菜鸟我对C++的掌握尚未深入,其中不乏错误,欢迎各

C语言 sizeof函数详解

1. 定义:sizeof是何方神圣sizeof乃C/C++中的一个操作符(operator)是也,简单的说其作用就是返回一个对象或者类型所占的内存字节数.MSDN上的解释为:The sizeof keyword gives the amount of storage, in bytes, associated with avariable or a type (including aggregate types). This keyword returns a value of type siz

【C++】sizeof()函数解析

[1] 常见数据类型的的sizeof函数得出的值表 char int short long float double 指针 32 bit 1 4 2 4 4 8 4 64 bit 1 4 or 8 2 8 4 8 8 [2] 数组求sizeof 的值 数组的大小 = 数组中元素的个数 x 该类型的大小 [3] union 求 sizeof 的值 由于 union 中存在复写现象,union 结构的 sizeof 的值即为 union 中最大的元素的大小.例如: union student{   

Linux中在主机上实现对备机上目录及文件的操作的C代码实现

需求描述 编写程序,完成在主机上实现对备机上目录及文件的操作.例如,主机为A,备机为B,要求编写的程序运行在A机上,该程序实现在B机上创建文件目录及拷贝文件的操作. 需求分析 我们先不考虑用程序实现需求,如果是让我们用手工实现需求中描述的操作,我们会怎么做呢?大家也许都会想到使用telnet的方式.也就是说,在A机上使用telnet远程到B机上去,然后在B机上执行创建目录和拷贝文件的操作. 我们也可以在程序中使用telnet来完成上述需求. 程序编写 我们可以使用两种方式来执行telnet命令,