sizeof

sizeof array clarification

匿名 (未验证) 提交于 2019-12-03 01:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am studying for a final tomorrow in C, and have a question regarding the sizeof operator. Let's say the size of an int is 32 bits and a pointer is 64 bits. If there were a function: int foo (int zap[]) { int a = sizeof(zap); return a; } Because zap is a pointer, foo would return 8 , as that's how many bytes are needed to store this particular pointer. However, with the following code: int zip[] = { 0, 1, 2, 3, 4, 5 }; int i = sizeof(zip); i would be 6 * sizeof(int) = 6 * 4 = 24 Why is it that sizeof(zip) returns the number of elements

Is it always the case that sizeof(T) >= alignof(T) for all object types T?

匿名 (未验证) 提交于 2019-12-03 01:27:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: For any object type T is it always the case that sizeof(T) is at least as large as alignof(T) ? Intuitively it seems so, since even when you adjust the alignment of objects like: struct small { char c; }; above what it would normally be, their "size" is also adjusted upwards so that the relationship between objects in an array makes sense while maintaining alignment (at least in my testing . For example: struct alignas(16) small16 { char c; }; Has both a size and alignment of 16. 回答1: At least in standard C++, for anything you can make an

Old style C function declaration

匿名 (未验证) 提交于 2019-12-03 01:27:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Here's a simple function delcared and defined using old style syntax: #include void error(message,a1,a2,a3,a4,a5,a6,a7) char *message; char *a1,*a2,*a3,*a4,*a5,*a6,*a7; { fprintf(stderr,message,a1,a2,a3,a4,a5,a6,a7); } int main () { error("[ERROR %d]: %s.\n",110,"Connection timed out"); return 0; } It can be compiled and runs correctly to print: [ERROR 110]: Connection timed out. I read that this style doesn't have associated prototype, but how can it convert int to char * automatically at runtime and even the provided arguments are fewer

IntPtr to Byte Array and Back

匿名 (未验证) 提交于 2019-12-03 01:27:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Referencing How to get IntPtr from byte[] in C# I am attempting to read the data that an IntPtr is referencing into a byte [] and then back into another IntPtr. The pointer is referencing an image captured from a scanner device so I have also made the assumption that capturing this information should be placed into a byte array. I am also not sure if the Marshal.SizeOf() method will return the size of the data the IntPtr is referencing or the size of the pointer itself. My issue is I am receiving the error "Type 'System.Byte[]' cannot be

How to check if a template parameter is an iterator type or not?

匿名 (未验证) 提交于 2019-12-03 01:27:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: template struct is_iterator { static const bool value = ??? // What to write ??? }; int main() { assert(false == is_iterator ::value); assert(true == is_iterator ::iterator>::value); assert(true == is_iterator ::iterator>::value); assert(true == is_iterator ::value); assert(true == is_iterator ::value); // a raw pointer is also an iterator } The question is: How to make the five assert statements pass? 回答1: template struct is_iterator { static T makeT(); typedef void * twoptrs[2]; // sizeof(twoptrs) > sizeof(void *) static twoptrs & test(...

Why does “sizeof(a ? true : false)” give an output of four bytes?

帅比萌擦擦* 提交于 2019-12-03 01:23:43
问题 I have a small piece of code about the sizeof operator with the ternary operator: #include <stdio.h> #include <stdbool.h> int main() { bool a = true; printf("%zu\n", sizeof(bool)); // Ok printf("%zu\n", sizeof(a)); // Ok printf("%zu\n", sizeof(a ? true : false)); // Why 4? return 0; } Output (GCC): 1 1 4 // Why 4? But here, printf("%zu\n", sizeof(a ? true : false)); // Why 4? the ternary operator returns boolean type and sizeof bool type is 1 byte in C. Then why does sizeof(a ? true : false)

Why is sizeof(std::string) only eight bytes?

匿名 (未验证) 提交于 2019-12-03 01:23:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Why is std::string 's size, as determined by sizeof(std::string) , yield 8 ? I thought it should be more than 8 as it has to have an int ( sizeof(int) == 8 on my machine) data member for giving std::string::length() and std::string::size() in O(1) and probably a char* for characters. 回答1: The implementation of std::string is not specified by the C++ standard. It only describes the classes behaviour. However, I would expect there to be more than one pointer's worth of information in the class. In particular: A pointer to the actual

Serialise and deserialise vector in binary

匿名 (未验证) 提交于 2019-12-03 01:23:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I am having problems trying to serialise a vector (std::vector) into a binary format and then correctly deserialise it and be able to read the data. This is my first time using a binary format (I was using ASCII but that has become too hard to use now) so I am starting simple with just a vector of ints. Whenever I read the data back the vector always has the right length but the data is either 0, undefined or random. class Example { public : std :: vector val ; }; WRITE: Example example = Example (); example . val . push_back ( 10

Why is (sizeof(int) &gt; -1) false? [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:23:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This question already has an answer here: Why should be there the involvement of type promotion in this code? 1 answer Comparison operation on unsigned and signed integers 5 answers Can You justify the below code: #include int main() { if(sizeof(int) > -1) { printf("\nTrue\n"); } else { printf("\nFALSE\n"); } } The output is FALSE .....suggest me the reason 回答1: sizeof(int) has type size_t , which is an unsigned integer type. -1 has type int , which is a signed integer type. When comparing a signed integer with an unsigned integer, first the

Understanding the mesh created by Qt3D

匿名 (未验证) 提交于 2019-12-03 01:23:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I create a Qt3D mesh like this: Qt3DCore::QEntity *newEntity = new Qt3DCore::QEntity(); Qt3DExtras::QConeMesh *mesh =new Qt3DExtras::QConeMesh(); mesh->setTopRadius(0.2); mesh->setBottomRadius(1.0); mesh->setLength(2.0); for(int i = 0; i < mesh->geometry()->attributes().size(); ++i) { mesh->geometry()->attributes().at(i)->buffer()->setSyncData(true); // To have access to data } newEntity->addComponent(mesh); The created mesh looks like this: Later in the code, I try to export the above mesh in STL binary format . To do so, I extract the