sizeof

Different sizeof results

Why does n not equal 8 in the following function? void foo(char cvalue[8]) { int n = sizeof cvalue; } But n does equal 8 in this version of the function: void bar() { char cvalue[8]; int n = sizeof cvalue; } ...

Why isn't sizeof for a struct equal to the sum of sizeof of each member?

Why does the 'sizeof' operator return a size larger for a structure than the total sizes of the structure's members? ...

How do I find the size of a struct?

struct a { char *c; char b; }; What is sizeof(a)? ...

Programatically calculate the size of a value type

I'm writing a unit test for a method that packs boolean values into a byte. The various bit locations are determined by the value of an enum, which only has 5 values right now, but it's conceivable (though extremely unlikely) that this number could go to 9. I'd like a simple test along the lines of: private byte m_myNum; enum MyEnum {...

"C" sizeof with a type or variable

Recently saw someone commending another user on their use of sizeof var instead of sizeof(type). I always thought that was just a style choice. Is there any significant difference? As an example, the lines with f and ff were considered better than the lines with g and gg: typedef struct _foo {} foo; foo *f = malloc(count * sizeof f);...

C++ char array with stdin

I am trying to get the size of an array populated by stdin: char *myArray; cin >> myArray cout << sizeof(myArray); This returns 4 when I enter a string greater with a length greater than 4 e.g. "40905898" Where am i going wrong? ...

Question on multiple inheritance, virtual base classes, and object size in C++

The following code prints 20, i.e. sizeof(z) is 20. #include <iostream.h> class Base { public: int a; }; class X:virtual public Base { public: int x; }; class Y:virtual public Base { public: int y; }; class Z:public X,public Y { }; int main() { Z z; cout << sizeof(z) <<endl; } ...

Is the sizeof(some pointer) always equal to four?

For example: sizeof(char*) returns 4. As does int*, long long*, everything that I've tried. Are there any exceptions to this? ...

Why are C character literals ints instead of chars?

In C++, sizeof('a') == sizeof(char) == 1. This makes intuitive sense, since 'a' is a character literal, and sizeof(char) is defined to be 1 by the standard. But in C, sizeof('a') == sizeof(int). That is, it appears that C character literals are actually integers. Does anyone know why? I can find plenty of mentions of this C quirk but no ...

How do I determine the size of an object in Python?

In C, we can find the size of an int, char, etc. I want to know how to get size of objects like a string, integer, etc. in Python. Related question: How many bytes per element are there in a Python list (tuple)? I am using an XML file which contains size fields that specify the size of value. I must parse this XML and do my coding. Whe...

How to find the sizeof( a pointer pointing to an array )

First off, here is some code: int main() { int days[] = {1,2,3,4,5}; int *ptr = days; printf("%u\n", sizeof(days)); printf("%u\n", sizeof(ptr)); return 0; } Is there a any way to find out the size of the array that ptr is pointing to? Instead of just giving it's size, which is 4 bytes. Thanks. ...

Getting the size of an indiviual field from a c++ struct field

The short version is: How do I learn the size (in bits) of an individual field of a c++ field? To clarify, an example of the field I am talking about: struct Test { unsigned field1 : 4; // takes up 4 bits unsigned field2 : 8; // 8 bits unsigned field3 : 1; // 1 bit unsigned field4 : 3; // 3 bits unsigned field5 ...

A question about sizeof and class member function.

class B { public: int a; void fn(); } If I create an object of B, using B* pb = new B; Where is the memory of fn() locate? Is there a pointer in object that pointing at the memory loaction of fn()? If yes, why sizeof(B) returns the value as if there is no pointer in object at all? ...

sizeof(int) on x64?

When I do sizeof(int) in my C#.NET project I get a return value of 4. I set the project type to x64, so why does it say 4 instead of 8? Is this because I'm running managed code? ...

Byte size of object including size of members

I want to know (for debugging and logging) the size of an object in bytes, but not like Sizeof (Object) but like a 'deep sizeof'. For example if the object contains a hash map or a list, the real size needed by that hash map or list should be added to the result. Is there any way to do this without having to add a property Size : Lon...

sizeof(*this) gives wrong value

I have a class, C. C has a member variable declared as: bool markerStart; From within C a call to sizeof(*this) gives a value of 0x216 bytes. Elsewhere within C, I do: markerStart = false; Rather than setting markerStart to false, this call is actually clobbering the start of the next class in memory! Looking at the disassembled code...

AccessViolationException during P/Invoke Help

I keep getting an AccessViolationException when calling the following from an external DLL: FILES_GetMemoryMapping(MapFile, out size, MapName, out PacketSize, pMapping, out PagePerSector); Which has a prototype that I've setup as such: [DllImport("Files.DLL", SetLastError = true)] public static extern uint FILES_GetMemoryMapp...

C: Why isn't size_t a C keyword?

sizeof is a C keyword. It returns the size in a type named size_t. However, size_t is not a keyword, but is defined primarily in stddef.h and probably other C standard header files too. Consider a scenario where you want to create a C program which does not include any C standard headers or libraries. (Like for example, if you are creat...

sizeof a union in C/C++

What is the sizeof the union in C/C++? Is it the sizeof the largest datatype inside it? If so, how does the compiler calculate how to move the stack pointer if one of the smaller datatype of the union is active? ...

C memset seems to not write to every member

I wrote a small coordinate class to handle both int and float coordinates. template <class T> class vector2 { public: vector2() { memset(this, 0, sizeof(this)); } T x; T y; }; Then in main() I do: vector2<int> v; But according to my MSVC debugger, only the x value is set to 0, the y value is untouched. Ive never used si...