sizeof

Size of C++ classes

给你一囗甜甜゛ 提交于 2019-12-03 06:20:46
Here is the code which prints size of different classes #include <iostream> using namespace std; class EmptyClass { }; class AbstractClass { public: virtual void funcOne() = 0; virtual void funcTwo() = 0; }; class NotAbstrClass { public: int virtFunc( int ); }; class MixClass { public: virtual void clFunc( int ); static int i; int j; }; int main() { // Print size of class or class objects cout<<"Size of empty class: "<< sizeof(EmptyClass)<<endl; cout<<"Size of Abstract class :"<< sizeof(AbstractClass)<<endl; cout<<"Size of Non Abstract class: "<< sizeof(NotAbstrClass)<<endl; cout<<"Size of Mix

About sizeof of a class member function pointer [duplicate]

可紊 提交于 2019-12-03 06:13:53
This question already has answers here : Pointers to members representations (2 answers) Let's say we have a class A class A; and these typedefs typedef void (A::*a_func_ptr)(void); typedef void (*func_ptr)(void); My question is why sizeof(a_func_ptr) returns 16, while sizeof(func_ptr) returns 4 (as for any pointer on x86 system)? For instance int main(int argc, char *argv[]) { int a = sizeof(a_func_ptr); int b = sizeof(func_ptr); } Nawaz My question is why sizeof(a_func_ptr) returns 16, while sizeof(func_ptr) returns 4 (as for any pointer on x86 system)? Because pointer-to-members are

C语言qsort函数总结

一个人想着一个人 提交于 2019-12-03 05:25:08
   前几天在leetcode上刷题,用qsort对二维数组进行排序,发现不会写qsort的比较函数。后面在网上找了几篇博客才弄明白,趁今天有空,对这个做一下总结,主要是以下4个方面: 1、qsort总体介绍 2、qsort应用于一维数组 3、qsort应用于指针数组 4、qsort应用于二维数组 1、qsort总体介绍   函数声明:void qsort(void *base, size_t nitems, size_t size, int (*compare)(const void *, const void*))   参数解释:     base-- 指向要排序的 数组 的第一个元素的指针。注意这里说是数组,所以必须是对连续的内存块进行排序。     nitems-- 由 base 指向的数组中元素的个数     size-- 数组中每个元素的大小,以字节为单位     compare-- 用来比较两个元素的函数。这是qsort最难的一部分了。这里主要要注意以下2点:1、在写compare函数时,你的两个形参必须是const void *型,但是在compare函数内部你又必须将const void *类型的形参转换为实际的类型。这是我最当时最难以理解的一个问题了:我需要转换成什么类型。看了别人的总结才知道,是: 指向数组元素的指针,指向数组元素的指针,指向数组元素的指针

Why do C++ classes without member variables occupy space?

ⅰ亾dé卋堺 提交于 2019-12-03 05:12:57
I found that both MSVC and GCC compilers allocate at least one byte per each class instance even if the class is a predicate with no member variables (or with just static member variables). The following code illustrates the point. #include <iostream> class A { public: bool operator()(int x) const { return x>0; } }; class B { public: static int v; static bool check(int x) { return x>0; } }; int B::v = 0; void test() { A a; B b; std::cout << "sizeof(A)=" << sizeof(A) << "\n" << "sizeof(a)=" << sizeof(a) << "\n" << "sizeof(B)=" << sizeof(B) << "\n" << "sizeof(b)=" << sizeof(b) << "\n"; } int

C的基本数据类型小结

风流意气都作罢 提交于 2019-12-03 04:56:29
代码 /** * 基本数据类型 */ #include <stdio.h> #include <limits.h> /* 定义 32 位时的 long 与 unsigned long 取值范围 */ #define LONG_MIN_32 -2147483648L #define LONG_MAX_32 2147483647L #define ULONG_MAX_32 4294967295UL int main(int argc, char *argv[]) { // short printf("short 取值范围为 %d ~ %d, 占 %d 个字节\n", SHRT_MIN, SHRT_MAX, sizeof(short)); // short 取值范围为 -32768 ~ 32767, 占 2 个字节 // unsigned short printf("unsigned short 取值范围为 0 ~ %u, 占 %d 个字节\n", USHRT_MAX, sizeof(unsigned short)); // unsigned short 取值范围为 0 ~ 65535, 占 2 个字节 // int printf("int 取值范围为 %d ~ %d, 占 %d 个字节\n", INT_MIN, INT_MAX, sizeof(int)); // int 取值范围为

Best practice for getting datatype size(sizeof) in Java

倾然丶 夕夏残阳落幕 提交于 2019-12-03 04:52:32
问题 I want store a list of doubles and ints to a ByteBuffer, which asks for a size to allocate. I'd like to write something like C's syntax int size=numDouble*sizeof(double)+numInt*sizeof(int); But there is no sizeof in Java. What is the best practice to calculate the size in byte? Should I hardcode it? 回答1: (If you're using Java 8 or beyond, be sure to look at @Frank Kusters' answer!) All of the primitive wrappers have a SIZE constant, which is in bits, not bytes. So in the example given, it

c++ function template specialization for known size typedefed array

╄→尐↘猪︶ㄣ 提交于 2019-12-03 04:02:36
Please consider the following code: #include <iostream> #include <typeinfo> template< typename Type > void func( Type var ) { std::cout << __FUNCTION__ << ": var = " << var << " [" << typeid( var ).name( ) << "]." << std::endl; std::cout << "-> var is SCALAR. Size = " << sizeof( Type ) << std::endl; } #if 1 template< typename Type > void func( Type * var ) { std::cout << __FUNCTION__ << ": var = " << var << " [" << typeid( var ).name( ) << "]." << std::endl; std::cout << "-> var is ARRAY. Size = " << sizeof( Type * ) << std::endl; } #endif int main( ) { typedef char char16[ 16 ]; char16 c16 =

Sizeof struct in Go

感情迁移 提交于 2019-12-03 03:49:14
问题 I'm having a look at Go, which looks quite promising. I am trying to figure out how to get the size of a go struct, for example something like type Coord3d struct { X, Y, Z int64 } Of course I know that it's 24 bytes, but I'd like to know it programmatically.. Do you have any ideas how to do this ? 回答1: import unsafe "unsafe" /* Structure describing an inotify event. */ type INotifyInfo struct { Wd int32 // Watch descriptor Mask uint32 // Watch mask Cookie uint32 // Cookie to synchronize two

OpenGL EXC_BAD_ACCESS when calling glDrawElements in Swift but not in Objective-C

匿名 (未验证) 提交于 2019-12-03 03:10:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm working through the OpenGL for iOS tutorial by Ray Wenderlich in an attempt to convert his code from Objective-C to Swift. I am very new to OpenGL and to Swift and believe my problem has to do with how I have translated the Objective-C. Here's why: In my swift file for setting up my view that contains OpenGL content, on the final logical step (calling glDrawElements) the app will crash with a EXC_BAD_ACCESS alert. If, however, I move this portion of the code to an Objective-C file , the app works as expected. Swift version of this code:

Using sizeof operator on a typedef-ed struct

匿名 (未验证) 提交于 2019-12-03 03:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: This might be something too obvious. However, I couldn't find the specific answer though many stackoverflow threads talk about different aspects of this. typedef struct _tmp { unsigned int a ; unsigned int b ; } tmp ; int main () { int c = 10 ; if ( c <= sizeof tmp ) { printf ( "less\n" ); } else { printf ( "more\n" ); } return 0 ; } I compile this prog as - g ++ - lstdc ++ a . cpp I get an error - expected primary - expression before ‘)’ token I think I am missing something very obvious and straightforward. But can't seem to