sizeof

Why can't I use sizeof in a preprocessor condition ?

I understand that sizeof is an operator, which is evaluated at compile time to an integer constant. But it seem it can not be used in the #if preprocessor directive like: #if 4 == sizeof(int) typedef int Int32; #endif (cygwin-gcc 3.4.4 as well as Visual C++ 6.0 report compile errors) Why is such usage not allowed? ...

What am I missing in the following program?

#include<stdio.h> #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) int array[] = {23, 34, 12, 17, 204, 99, 16}; int main() { int d; for (d = -1; d <= (TOTAL_ELEMENTS - 2); d++) printf("%d\n", array[d + 1]); return 0; } Why is the for loop not run even once? ...

How to catch bugs of the form sizeof(#define)

I'm sure there are sometimes good reasons for taking the sizeof() a #define in C, but I occasionally come across bugs where someone has taken the sizeof() a #define instead of the sizeof() a structure (and in my codebase I don't need to take the sizeof() a #define). For example (contrived, but hopefully illustrates the point): typedef ...

My buffer contains elements, but aren't being printed...

Sorry scratch my last post, it's way to late =S But basically I'm having problems sending out the buffer I created. Just need to know where I'm going wrong =( or if theres a better way. ------ Client Sending Username ------- int bufferSize = 32; char messageBuffer[bufferSize]; char* message; if (userName.size() > 8) { cout << "I...

MSVC: what compiler switches affect the size of structs?

I have two DLLs compiled separately, one is compiled from Visual Studio 2008 and one is a mex file compiled from matlab. Both DLLs have a header file which they include. when I take the sizeof() the struct in one DLL it returns 48, and in the other it returns 64. I've checked the /Zp switch and in both compilations it is set to /Zp8. Wha...

Why can I not perform a sizeof against a static char[] of another class?

Why does the following code generate a compile error? Edit: My original code wasn't clear - I've split the code up into separate files... First.h class First { public: static const char* TEST[]; public: First(); }; First.cpp const char* First::TEST[] = {"1234", "5678"}; First::First() { uint32_t len = sizeof(TES...

How can a template function 'know' the size of the array given as template argument?

Hi all, In the C++ code below, the templated Check function gives an output that is not what I would like: it's 1 instead of 3. I suspect that K is mapped to int*, not to int[3] (is that a type?). I would like it to give me the same output than the second (non templated) function, to which I explicitly give the size of the array... Sh...

Size of an array

This question is about C++ I always thought that name of an array in C++ is only a pointer, so I thought that int ar[10]; cout << sizeof(ar); will give me the same as sizeof(int *). But it gives 40 - so it is real size of whole array. It also gives 40 when array size is given by variable: int n = 10; int ar[n] I want to copy a cla...

C++ struct size: 2+4+2+2+4 = 16

Possible Duplicate: Why isnt sizeof for a struct equal to the sum of sizeof of each member? Why is the sizeof(); of this structure 16 bytes? I'm compiling in g++. struct bitmapfileheader { unsigned short bfType; unsigned int bfSize; unsigned short bfReserved1; unsigned short bfReserved2; unsigne...

Getting the size in bytes or in chars of a member of a struct or union in C/C++?

Let's say that I want to get the size in bytes or in chars for the name field from: struct record { int id; TCHAR name [50]; }; sizeof(record.name) does not work. ...

sizeof array of structs in C?

In C I have an array of structs defined like: struct D { char *a; char *b; char *c; }; static struct D a[] = { { "1a", "1b", "1c" }, { "2a", "2b", "2c" } }; I would like to determine the number of elements in the array, but sizeof(a) returns an incorrect resu...

struct sizeof result not expected

Hey, I have a a struct defined thusly: typedef struct _CONFIGURATION_DATA { BYTE configurationIndicator; ULONG32 baudRate; BYTE stopBits; BYTE parity; BYTE wordLength; BYTE flowControl; BYTE padding; } CONFIGURATION_DATA; Now, by my reckoning, that struct is 10 bytes long. However, sizeof reports that it ...

Size of pid_t, uid_t, gid_t on Linux

On Linux systems (either 32- or 64-bit), what is the size of pid_t, uid_t, and gid_t? ...

Sizeof an array in the C programming language?

why isn't the size of an array sent as a parameter the same as within main? #include <stdio.h> void PrintSize(int p_someArray[10]); int main () { int myArray[10]; printf("%d\n", sizeof(myArray)); /* as expected 40 */ PrintSize(myArray);/* prints 4 not 40 */ } void PrintSize(int p_someArray[10]){ printf("%d\n", sizeof(...

Number of bits in a data type

I have two tasks for an assignment, one return the number of bits in type int on any machine. I thought I would write my function like so: int CountIntBitsF() { int x = sizeof(int) / 8; return x; } Does that look right? The second part is to return the number of any bits of any data type with a macro, and the macro can be ...

Rotating Bits, using sizeof operator

I'm trying to write a bit rotator function and I'm trying to get more clarification the sizeof operator. Since I don't know what type of numerical object I need to rotate, I assume I need to use the sizeof operator for unsigned rotator(unsigned object, int count) this function prototype where object is the object to be rotated and c...

Sizeof struct in Go

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

Why doesn't sizeof parse struct members?

I know that sizeof is a compile-time calculation, but this seems odd to me: The compiler can take either a type name, or an expression (from which it deduces the type). But how do you identify a type within a class? It seems the only way is to pass an expression, which seems pretty clunky. struct X { int x; }; int main() { // ret...

how to get the size of a C global array into an assembly program written for the avr architecture compiled with gcc

I have a .c file with the following uint8_t buffer[32] I have a .S file where I want to do the following cpi r29, buffer+sizeof(buffer) The second argument for cpi muste be an immediate value, not a location. But unfortunately sizeof() is a C operator. Both files are compiled to separate object files and linked together afterward...

sizeof(void*) and sizeof(function_type_ptr*) equality

sizeof(void*) and sizeof(function_type_ptr*) equality Must sizeof(void*) and sizeof(function_type_ptr*) be equal? Is it required by C (C90, C99) standard? function_type_ptr is pointer to function. This question is about standard requirements, not your own opinion. Please, give links, section numbers and quotations from standards ...