#include <stdio.h>
#include <stdlib.h>
int main(void) {
printf("型態 \t \t大小(bytes)\n");
printf("short %15d\n", sizeof(short));
printf("int %15d\n", sizeof(int));
printf("long %15d\n", sizeof(long));
printf("float %15d\n", sizeof(float));
printf("double %15d\n", sizeof(double));
printf("long double %15d\n", sizeof(long double));
printf("char %15d\n", sizeof(char));
system("pause");
return 0;
}
---------------------------------------------
型態 大小(bytes)
short 2
int 4
long 4
float 4
double 8
long double 8
char 1
---------------------------------------------
參考
http://openhome.cc/Gossip/CGossip/Datatype.html
而Java language 中則沒有這種 operator,
主要因為
- The size of data structure is same on all platforms
- Java virtual machine with garbage collection will do the memory management for you.
But there are few use cases where we may need a way to measure size of objects at runtime.
參考
http://blog.madhukaraphatak.com/sizeof-operator-java-scala/
在Java已經封裝好了, 只要用new就可以了,
JVM 會自動配置好記憶體位置.
malloc(sizeof(int));
new ClassName();
而Java也初始化好這階型態的變化量大小, new時就得到想對應的記憶體大小,
但是Java多了一些安全檢查(?), 所以效率上會差一點.
參考
http://bbs.csdn.net/topics/60085935
參考
http://www.slideshare.net/DawidWeiss/sizeofobject-how-much-memory-objects-take-on-jvms-and-when-this-may-matter
沒有留言:
張貼留言