sizeof

Marshal.SizeOf error in computing size.

i have a structure public struct SERVER_USB_DEVICE { USB_HWID usbHWID; byte status; bool bExcludeDevice; bool bSharedManually; ulong ulDeviceId; ulong ulClientAddr; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] string usbDe...

Pointer Implementation Details in C

I would like to know architectures which violate the assumptions I've listed below. Also I would like to know if any of the assumptions are false for all architectures (i.e. if any of them are just completely wrong). sizeof(int *) == sizeof(char *) == sizeof(void *) == sizeof(func_ptr *) The in-memory representation of all pointers fo...

sizeof float (3.0) vs (3.0f)

Hi, What is the difference between sizeof(3.0) and sizeof(3.0f) I was expecting both of them to give the same result (sizeof float)..but its different. In 32 bit machine,gcc compiler, sizeof(3.0f) =>4 sizeof(3.0) => 8 Why so? ...

sizeof on a class inheriting from a base class with a virtual function

For the following code fragment. /*This program demonstartes how a virtual table pointer * adds to a size of a class*/ class A{ }; class X{ public: void doNothing(){} private: char a; }; class Z:public X { public: void doNothing(){} private: char z; }; class Y{ public: ...

How do I programatically determine how tall a div tag will be when rendered?

I'd like to be able to load a page up and once all the content is added see how high it is and include that as a hidden parrameter to be picked up later. Is there a way to do this? [NOTE] Just to clarify, I have to do this n code behind because it has to be retrieved via webrequest object. ...

Sizeof string literal

The following code #include <iostream> using namespace std; int main() { const char* const foo = "f"; const char bar[] = "b"; cout << "sizeof(string literal) = " << sizeof( "f" ) << endl; cout << "sizeof(const char* const) = " << sizeof( foo ) << endl; cout << "sizeof(const char[]) = " << sizeof( bar ) << endl; ...

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

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 i...

Why is sizeof an operator?

Why is sizeof considered an operator and not a function? What property is necessary for something to qualify as operator? ...

What is a common C/C++ macro to determine the size of a structure member?

In C/C++, how do I determine the size of the member variable to a structure without needing to define a dummy variable of that structure type? Here's an example of how to do it wrong, but shows the intent: typedef struct myStruct { int x[10]; int y; } myStruct_t; const size_t sizeof_MyStruct_x = sizeof(myStruct_t.x); // error Fo...

Determining size of an overloaded C++ class

Using the sizeof operator, I can determine the size of any type – but how can I dynamically determine the size of a polymorphic class at runtime? For example, I have a pointer to an Animal, and I want to get the size of the actual object it points to, which will be different if it is a Cat or a Dog. Is there a simple way to do this, sh...

Sizeof in C++ and how to calculate pointer length?

Can someone explain the following code snippet for me? // Bind base object so we can compute offsets // currently only implemented for indexes. template<class DataObj> void BindAsBase(DataObj &rowbuf) { // Attempting to assign working_type first guarantees exception safety. working_type = DTL_TYPEID_NAME (rowbuf); working_ad...

C/C++: Size of builtin types for various compilers/platforms

Where can I go to get information about the size of, say, unsigned int compiling under gcc for Mac OS X (both 32 and 64 bits)? In general I'd love to have a resource I can go to with a compiler/settings/platform/type and be able to look up how big that type will be. Does anyone know of such a thing? Update: Thanks for all the responses....

Is there anything wrong with using sizeof(type[1234])?

I've been using it for years, i.e.: text = (char *)malloc( sizeof(char[1234]) ); instead of: text = (char *)malloc( sizeof(char) * 1234 ); People have told me it's dangerous, but none could say why. I checked the C spec and it's legal. Are there any pitfalls here? ...

newbie questions about malloc and sizeof

Can someone explain to me why my call to malloc with a string size of 6 returns a sizeof of 4 bytes? In fact, any integer argument I give malloc I get a sizeof of 4. Next, I am trying to copy two strings. Why is my ouput of the copied string (NULL)? Following is my code: int main() { char * str = "string"; char * copy = malloc(s...

Potential problem with C standard malloc'ing chars.

When answering a comment to another answer of mine here, I found what I think may be a hole in the C standard (c1x, I haven't checked the earlier ones and yes, I know it's incredibly unlikely that I alone among all the planet's inhabitants have found a bug in the standard). Information follows: Section 6.5.3.4 ("The sizeof operator") p...

Is there a way to get the byte size of vectors through type alone?

How can I predict the size of a vector? #include <vector> #include <iostream> using namespace std; int main() { cout << sizeof(vector<char[8]>) << endl; cout << sizeof(vector<char[16]>) << endl; return 0; } [starlon@localhost LCDControl]$ ./test 12 12 ...

What arguments does the sizeof operator take in C?

[Original title referred to 'sizeof function'.] I tried these and they all worked: char *p; printf("Size of *p is %d\n",sizeof(*p)); //result =1 printf("Size of p is %d\n",sizeof( p)); //result =4 printf("Size of p is %d\n",sizeof( //result =4 I wonder why the first printf is 1, the 2nd and 3rd is 4? So what arguments can size...

C sizeof char* array

I have a char* array as follows: char *tbl[] = { "1", "2", "3" }; How do I use the sizeof operator to get the number of elements of the array, here 3? The below did work, but is it correct? int n = sizeof(tbl) / sizeof(tbl[0]) ...

Getting the size of the data of a Pointer

I tried the following code in order to see how to get size of the data of a pointer: #include <stdio.h> #include <stdlib.h> #include <string.h> int main(){ char *test_char_ptr = "This is just a test"; int *test_int_ptr = (int *) malloc(16*sizeof(int)); for(int i = 0; i<16; i++){ test_int_ptr[i] = i; } printf("%s\n",te...

what's the mechanism of sizeof() in C/C++?

It seems sizeof is not a real function? for example, if you write like this: int i=0; printf("%d\n", sizeof(++i)); printf("%d\n", i); You may get output like: 4 0 And when you dig into the assemble code, you'll find sth like this: movl $4, %esi leaq LC0(%rip), %rdi xorl %eax, %eax call _printf So, the compiler put d...