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

(zz) 我为旅行狂---Key West亲情计划 (2009/5/5)

强烈建议各亲朋好友在离开南部之前,到美国最南端的小岛上看一看。体验一下海天相接的极致景致,以及阳光灿烂的明媚心情。给在南部生活的学术生涯点缀一些不一样的心情,留下值得回忆的美好。此次 KW 之行历时 6 天,驱车 38 小时,经过 Pensacola , Miami , Key Largo , Key west , Plam Beach, 以及 Everglades. 除了预计到的长途跋涉辛苦以及晒成麦色的皮肤之外,可以用圆满来形容。特此感谢 YC MM ,其先驱之行起到了至关重要的影响意义。在脑海里只留下一片片的海蓝色之前,先奉献上我们的驱车路线,以便感兴趣的驴友们方便之用。 提前 10 天的时候记得先查天气,千万不要遇到 hurricane 这样的危险情况,这对海边游历是个大忌。我们此次采取西下东上。一路晴空万里,运气甚为好。               4.24           早 6 点出发, 4 个半小时之后到达 pensacola beach. 这片海域的确与众不同,若是以前没有去过,千万不要错过。雪地一般的沙滩,宝石一般的海水,一望无际的天空。最妙的在于那是狭长的海岸,我们 停下车的地方就是小小海道的尽头,像是走进了海的深处,妙不可言。若是允许,最好带上透风较好的帐篷,海风中安睡的感觉值得体验。下午 6 点左右吃过饭,继 续向南前行大概 2 个小时,到达一个不知名小镇安住下来,目的是给明天的 miami 路途节省时间和精力。           4.25           早 9 点出发,史进 Miami 。这段距离大概需要 8 个小时。一路上风景一般,不过还是和家里不同。成片的椰树林也是很养眼的。到达 Miami 的时候已是日暮。若是想去大名鼎鼎的 joy's stone crab 吃著名的 stone crad, 一定记得先去餐厅定上位子。他们生意做的很牛,若是刚好赶上周末,是不给电话定位的。所以要 physically 先去,定上时间。然后去天体浴 场看看裸体的人们以及美丽的日落。差不多华灯...

Java: Syntax error, parameterized types are only available if source level is 5.0

HOW TO FIX IT: It is because eventhough you have Java 5 installed, the JVM running may be different. Try typing this in the console window: java -version. It will give you the Version of the Runtime Environment. Or, if you are using an IDE, then you need to enable Java 5 support. In Eclipse, You can add Java 5 in Window > Preferences > Java > Installed JRE’s. and/or, Set the compiler compliance level to 5.0 (Window > Preferences > Java > Compiler) In Netbeans, you can use (Tools -> Java Platform Manager) to check your default platform. If you use javac commond line, try: javac -1.5 xxx.java Reference: http://laksmono.wordpress.com/2007/05/27/syntax-error-parameterized-types-are-only-available-if-source-level-is-50/

Oracle ORA-01000 maximum open cursors exceeded

Several days ago, I wrote a piece of program in C#/.Net to import some records into oracle database. It is windows version 10g XE. When the program is running, I got a run-time error which is ORA-01000 maximum open cursors exceeded. Here is the Oracle docs about ORA-01000: ORA-01000 maximum open cursors exceeded Cause: A host language program attempted to open too many cursors. The initialization parameter OPEN_CURSORS determines the maximum number of cursors per user. Action: Modify the program to use fewer cursors. If this error occurs often, shut down Oracle, increase the value of OPEN_CURSORS, and then restart Oracle. In order to solve this problem, I increased my OPEN_CURSORS by using: ALTER SYSTEM SET open_cursors = 5000 SCOPE=BOTH; You can use SQL/PL client and "connect" command to login the database system by using "system" user to change the open_cursors. If you are using .NET, DBA, and Java programming, please remember to close all the re...