sizeof

is there any alternative to sys.getsizeof() in PyPy?

安稳与你 提交于 2019-11-30 17:11:46
问题 I am trying to run a Python (2.7) script with PyPy but I have encountered the following error: TypeError: sys.getsizeof() is not implemented on PyPy. A memory profiler using this function is most likely to give results inconsistent with reality on PyPy. It would be possible to have sys.getsizeof() return a number (with enough work), but that may or may not represent how much memory the object uses. It doesn't even make really sense to ask how much *one* object uses, in isolation with the rest

Why do I get different results when I apply sizeof operator?

烂漫一生 提交于 2019-11-30 17:09:41
I have this program #include <stdio.h> int main() { char arr[100]; printf("%d", (int)sizeof(0,arr)); } This prints 4 when compiled as a C file and prints 100 as a C++ file. Why? I am using gcc. In C the result of the right hand operand of the comma operator has a type and value . In C a comma operator does not yield an lvalue. So there is an lvalue to rvalue conversion resulting in decay of array type to pointer type. So in C what you get is the result of sizeof(char*) . In C++ the result of a comma expression is an lvalue. There is no such conversion[as in C] and what you get is the sizeof

How to find the size of a variable with out using sizeof

若如初见. 提交于 2019-11-30 16:36:18
Let us assume I have declared the variable 'i' of certain datatype (might be int, char, float or double) ... NOTE: Simply consider that 'i' is declared and dont bother if it is an int or char or float or double datatype. Since I want a generic solution I am simply mentioning that variable 'i' can be of any one of the datatypes namely int, char, float or double. Now can I find the size of the variable 'i' without sizeof operator? You can use the following macro, taken from here : #define sizeof_var( var ) ((size_t)(&(var)+1)-(size_t)(&(var))) The idea is to use pointer arithmetic ( (&(var)+1) )

fread与read效率对比

余生长醉 提交于 2019-11-30 16:19:09
网上看了很多文章,都说 fread 比 read 读取数据更快云云,今天在我的 mac 上做了一个小测试,结果比较意外 1 #include <stdio.h> 2 #include <unistd.h> 3 #include <time.h> 4 #include <fcntl.h> 5 6 void test_fread(char *buff, int num); 7 void test_read(char *buff, int num); 8 9 int main() { 10 char buff[100]; 11 test_fread(buff, sizeof(buff)); 12 test_read(buff, sizeof(buff)); 13 } 14 15 void test_fread(char *buff, int num) { 16 clock_t start, finish; 17 double duration; 18 start = clock(); 19 20 FILE *fp; 21 char filename[] = "a.txt"; 22 fp = fopen(filename, "r"); 23 int i=0; 24 while(1) { 25 i++; 26 fread(buff, num, 1, fp); 27 if(feof(fp))

sizeof(void) equals 1 in C? [duplicate]

耗尽温柔 提交于 2019-11-30 15:46:37
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: What is the size of void? Hi all ! I am using gcc for compiling my C programs, just discovered accidentally that the sizeof(void) is 1 byte in C. Is there any explanation for this ? I always thought it to be ZERO (if it really stores nothing) ! Thanks ! 回答1: This is a non standard extension of gcc, but has a rationale. When you do pointer arithmetic adding or removing one unit means adding or removing the object

Is sizeof… allowed in template arguments for specialization?

余生颓废 提交于 2019-11-30 15:37:24
I'm trying to do something along the lines of this using GCC 4.7 snapshot: template <int n, int... xs> struct foo { static const int value = 0; }; // partial specialization where n is number of ints in xs: template <int... xs> struct foo<sizeof...(xs), xs...> { // error: template argument ‘sizeof (xs ...)’ // involves template parameter(s) static const int value = 1; }; template <int... xs> struct foo<sizeof(xs), xs...> { // This compiles fine. sizeof(xs) is sizeof int // even though packs aren't expanded static const int value = 2; }; The error is strange because sizeof instead of sizeof...

sizeof(value) vs sizeof(type)?

Deadly 提交于 2019-11-30 15:21:25
Considering : double data; double array[10]; std::vector<int> vec(4, 100); MyClass myclass; Is there a difference between : sizeof(double); sizeof(double[10]); sizeof(std::vector<int>); sizeof(MyClass); and sizeof(data); sizeof(array); sizeof(vec); sizeof(myclass); Are the two syntaxes different or strictly equivalent ? Are all of them evaluated at compile-time ? If not, which one is evaluated at run-time ? The only differences are in syntax and convenience. Syntactically, you're allowed to leave out the parentheses in one case, but not the other: double d; sizeof(double); // compiles sizeof(d

Printing sizeof(T) at compile time [duplicate]

假装没事ソ 提交于 2019-11-30 15:00:13
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Is it possible to print out the size of a C++ class at compile-time? Can I output the size of an object at compile time? Since the compiler already has this information when it is compiling a source file, can I see it (at compile time) rather than going through the lengthy process of outputting the size somewhere in my application's console or the debug output window? This will be very useful especially when I

Why would the size of a packed structure be different on Linux and Windows when using gcc?

夙愿已清 提交于 2019-11-30 14:37:26
问题 In the code below, why is the size of the packed structure different on Linux and Windows when compiled with gcc? #include <inttypes.h> #include <cstdio> // id3 header from an mp3 file struct header { uint8_t version[ 2 ]; uint8_t flags; uint32_t size; } __attribute__((packed)); int main( int argc, char **argv ) { printf( "%u\n", (unsigned int)sizeof( header ) ); return 0; } gcc versions used: $ g++ --version g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2 $ x86_64-w64-mingw32-g++ --version x86_64

Why sizeof(array) and sizeof(&array[0]) gives different results?

会有一股神秘感。 提交于 2019-11-30 13:54:30
问题 #include <stdio.h> int main(void){ char array[20]; printf( "\nSize of array is %d\n", sizeof(array) ); //outputs 20 printf("\nSize of &array[0] is %d\n", sizeof(&array[0]); //output 4 } Code above gives 20 for sizeof(array) and 4 for sizeof(&array[0]) . What I knew was instead of giving array as a argument, its first element can be passed. Shouldn't they give same output as 20? and why &array[0] gives 4 as result? char is stored in 1 byte as far as I know? 回答1: In the expression sizeof array