Skip to main content

Tips about sizeof() function in C and C++

The unary operator sizeof is used to calculate the sizes of datatypes in Bytes in modern luanguages in the programming languages C and C++. It is an useful operation in memory allocation. In order to use it right, some tips that you may want to know:

  • sizeof() is a compile-time function (macro-like function), not a run-time function. Therefore, you can declare array as: int arr[sizeof(int)];
  • because sizeof() is a compile-time function, the equation in sizeof() function will not be calculated. For example: int i=3; int a = sizeof(i++); the value of i will not be changed after sizeof(i++);
  •  because sizeof() is a compile-time function, it can not help you determine the size of an array parameter. The following code will print out size 12 and 4.
void test_sizeof(int arr[])
{
    cout << "sizeof(arr) = " << sizeof(arr) << endl;
}

int main() {
    int arr[3];

    cout << "sizeof(arr) =" << sizeof(arr) << endl;
    testsizeof(arr); 
}

  • sizeof() can not be applied to incomplete datatype. Compile errors will be report when the following code is compiled:
class InCompClass;
int main() {
    cout << "Sizeof(InComplete Types)" << sizeof(InComClass) << endl;
}

  • However, sizeof() can be used to determine the size of empty class type and 1 will be returned.
  • When sizeof() is used to calculate the size of user-defined data structure, the size may not be equal to the sum of the size of data members because of the memory alignment in most compilers.

Comments

Popular posts from this blog

Create Splitting Windows in VC 2005

Add two CSplitterWnd type variables into the MainFrame: CSplitterWnd m_wndSplitterLM; // split the window to left and middle panel // Than add the following code into the OnCreateClient Function: BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/, CCreateContext* pContext) { // create a splitter with 1 row, 2 columns if (!m_wndSplitterLM.CreateStatic(this, 1, 2)) { TRACE("SpltLR create failed\n"); return false; } // add the first splitter pane - the default view in column 0 if (!m_wndSplitterLM.CreateView(0, 0, RUNTIME_CLASS(CMultiLayerTree), CSize(60, 0), pContext)) { TRACE0("Failed to create first pane\n"); return FALSE; } // add the second splitter pane - which splits the right window into two columns if (!m_wndSplitterMR.CreateStatic( &m_wndSplitterLM, // our parent window is the first splitter 1, 2, // the new splitter is 1

Enable root Account in Ubuntu Server

After ubuntu server has been installed, you can login with the account that you created. Then the following command can enable you create a password for root. test@test: sudo passwd root And then input your passwd for root. Command "su" can let you access the root, just type: test@test: su Be sure that remember the passwd for root for security reasons.

(zz) 我为旅行狂 --- 携Las Vegas 清泉入Death Valley广阔 (2010-04-04 03:40pm)

有 一个小小的立志:决绝悲情,远离造作。开心的生活,尽心地记录点滴的快乐。我是这样酷爱旅行,这样喜欢面对自然真实地感受自己。最好我能足够幸运,今后的 生活里可以继续走走行行。辛劳地生活,快乐地游历,满怀幸福和感恩地记录一切。还有,我想和每一个人分享我感受到过的一切。 3 月的尾巴,一样温暖明亮的日光,一样安静恬适的加州。我们抓紧时间,挤出了这个 3 日的美假。出发,去感受拉斯维加斯夜的风情,还有死亡谷的别样冷峻。 拉斯维加斯本是内华达和加州接壤的一片小小绿洲,周围则是一望无垠的沙漠。 1830 年西班牙的探险队发现这片美地,命名为 “ 丰美的草场 ” ,此名延用至今。 30 年代,内华达成为了全美惟一一个赌博合法的州,一夜之间,拉斯维加斯的大小赌场犹如神抚,瞬间充斥了这整座城市。 LV 的白天安静异常,人们为夜的疯狂蓄积能量。看不到霓虹,想不出夜的放肆。一旦霞光探头,华灯不等初上,夜就会瞬间爆燃点亮。 这里聚集了所有可以想象到的微型名景,用于豪赌的人们怡情。凯撒宫,金字塔,威尼斯人,海市蜃楼,火山口,百乐宫,还是有世界上最大的街边 LD 荧光屏。通亮的夜喧闹的场所,挑逗刺激着人们的神经。成千上万豪挥猛掷的人比比皆是。脚踩在绿洲上,投身在这赌城里,确实有一点不太真实。 LV 让我们感到的震撼,在于赌的事业可以如此壮观。而这样一座赌城,却是历史记录上美国最安全的城市。酒精和抛洒的金钱下,居然色情行业绝无一例。这里是美国唯一一个明文禁止性交易的城市,让人不由地 去赞叹法制的构建。 睡梦醒来,别了拉斯维加斯。我们又闯入沙漠,向死亡谷开去。 死亡谷的得名在于 18 世 纪淘金潮中,路径这里的人们。这是一片神奇的土地,常年没有雨水的滋养。地貌地形的多变犹如女孩子的脾气变化频繁。驱车行走,刚进入一片浩瀚如海的盐碱 地,还未待反映,五颜六色的岩石峭壁就会陡然呈现。这一边可以是风化的矿产宝地,那一边竟然可以是酷热的无垠沙漠。还有远处时时呈现的雪山,让人不由怀疑 究竟到底身处何地。 这一片夸张的盐碱地叫做 Badwater. 这里远远地看去,会让人误认为是广阔的海水,稍稍近一些,会以为是白白的沙滩。走进它,踩在咯吱咯吱的盐粒上,才相信这是矿物盐的世界。人是渺小的,小到和盐粒一般。刺眼的阳光折射在地面上,使得人睁不开眼睛。 Badwater 是全北美海拔最低的地方,也是 Death Va