sizeof

Can you resize a C++ array after initialization?

I'm learning to program, and C++ is my first language. Don't bother using pointers to show me - I don't understand them yet, and won't bother until I have more free time to dedicate to this. int mergeSort() { const int n = 9; int originalarray[n] = {1, 3, 5, 7, 9, 2, 4, 6, 8}; const int halfelements = (sizeof(originala...

Checking the size of an object in Objective-C

I'm trying to find the size of an objective-c object. I'm using something similar to: NSLog(@"sizeof myObject: %ld", sizeof(*myObject)); That just gives me the size of the pointer though. What am I doing wrong? ...

Why short is stored as 4 bytes in a struct in C?

I have the following two structs: The problem is the sizeof(Content) returns 160. The struct consists of 11 shorts, 6 ints, 76 chars, 7 floats, 1 double, totally adding to 158 bytes. I have counted three times and there is still a 2 byte difference. typedef struct TIME_T { short year,mon,day; short hour,min,sec; } TIME; typ...

Are there are any platforms where pointers to different types have different sizes?

The C standard allows pointers to different types to have different sizes, e.g. sizeof(char*) != sizeof(int*) is permitted. It does, however, require that if a pointer is converted to a void* and then back to its original type, it must compare as equal to its original value. Therefore, it follows logically that sizeof(void*) >= sizeof(...

What's sizeof(size_t) on 32-bit vs the various 64-bit data models?

On a 64-bit system, sizeof(unsigned long) depends on the data model implemented by the system, for example, it is 4 bytes on LLP64 (Windows), 8 bytes on LP64 (Linux, etc.). What's sizeof(size_t) supposed to be? Does it vary with data model like "long" does? If so, how? [1] en.wikipedia.org/wiki/64-bit#64-bit_data_models ...

Why call sizeof operator with two arguments?

I recently came across some code that looked like: if(sizeof(var,2) == 4) { ... } (where var is a type) I was quite surprised to see what appeared to be two arguments to the sizeof operator. A quick scan of the ISO/ANSI C99 standard did not yield any secrets. I couldn't come up with any reading of the grammar that allowed a comma the...

Is it necessary to multiply by sizeof( char ) when manipulating memory?

When using malloc and doing similar memory manipulation can I rely on sizeof( char ) being always 1? For example I need to allocate memory for N elements of type char. Is multiplying by sizeof( char ) necessary: char* buffer = malloc( N * sizeof( char ) ); or can I rely on sizeof( char ) always being 1 and just skip the multiplicatio...

What is the size of the memory area pointed to by an unsigned char *?

Ok, I know this has been asked before but after searching I couldn't find a proper answer. I need to convert a buffer (unsigned char *) to base64, the base64 function I am using takes as paramters: void Base64Enc(const unsigned char *src, int srclen, unsigned char *dest) where int srclen is the length of the src string. My question ...

.NET Reflection: determine sizes of a class' fields

Goal: to programmatically determine the sizes (in bytes) of the fields of a class. For example, see the comments below ... class MyClass { public byte b ; public short s ; public int i ; } class MainClass { public static void Main() { foreach ( FieldInfo fieldInfo in typeof(MyClass).GetFields(B...

How do I ignore a field size in a struct using Marshal.SizeOf?

Is there a way to ignore a field in the calculated of the struct size using Marshal.SizeOf Ex: public struct Message { public ushort X; public ushort Y; // Ignore this field in the calculation } int size = Marshal.SizeOf(typeof(Message)); Right now size is 4. I want the size to be 2. Is there a way to do this? ...

C++ Getting the size of a type in a macro conditional

Is there some way to do something like this in c++, it seems sizeof cant be used there for some reason? #if sizeof(wchar_t) != 2 #error "wchar_t is expected to be a 16 bit type." #endif ...

size of a datatype without using sizeof

Hi all I have a datatype say X and I want to know its size without declaring a variable or pointer of that type and of course without using sizeof operator. Is this possible. I thought of using standard header files which contain size and range of datatypes but that doesn't work with user defined datatype. Any help will be appreciated. ...

Why do I get a warning everytime I use malloc?

If I use malloc in my code: int *x = malloc(sizeof(int)); I get this warning from gcc: new.c:7: warning: implicit declaration of function ‘malloc’ new.c:7: warning: incompatible implicit declaration of built-in function ‘malloc’ I'm new to C. Am I doing something wrong? ...

How do you know how much space to allocate with malloc()?

I'm a total C newbie, I come from C#. I've been learning about memory management and the malloc() function. I've also came across this code: char *a_persons_name = malloc(sizeof(char) + 2); What I don't understand is how much space this is allocating for a_persons_name. Is it allocating 2 characters (eg. AB) or something else? I als...

JavaScript object size

I want to know the size occupied by a JavaScript object. Take the following function - function Marks() { this.maxMarks = 100; } function Student() { this.firstName = "firstName"; this.lastName = "lastName"; this.marks = new Marks(); } Now.. i instantiate the student var stud = new Student(); so that I can do stuff like ...

What should happen to the negation of a size_t (i.e. `-sizeof(struct foo)`))?

I'm dealing with some code at work that includes an expression of the form -(sizeof(struct foo)) i.e. the negation of a size_t, and I'm unclear on what the C and C++ standards require of compilers when they see this. Specifically, from looking around here and elsewhere, sizeof returns an unsigned integral value of type size_t. I can'...

Tokyo Cabinet and variable size C++ objects

I'm building a system, with C++, that uses Tokyo Cabinet (original API in C). The problem is I want to store a class such as: class Entity { public: string entityName; short type; vector<another_struct> x; vector<another_struct> y vector<string> z; }; The problem is that vectors an...

C sizeof equivalent for macros

So I'm optimizing some code by unrolling some loops (yes, I know that I should rely on my compiler to do this for me, but I'm not working with my choice of compilers) and I wanted to do so somewhat gracefully so that, in case my data size changes due to some edits in the future, the code will degrade elegantly. Something like: typedef ...

sizeof() And Template Argument In ctor / Non-ctor Function

I hit a snag today... I wanted to define a small templated helper class: template<class T> CMyClass { public : CMyClass() { size_t iSize = sizeof(T); } // Allowed. size_t GetElementSize() const { return sizeof(T); } // C2027. }; and of course, it wouldn't compile (C2027). My question was, is it possible to get the size of t...

Sizeof array passed as parameter

Given the following program #include <iostream> using namespace std; void foo( char a[100] ) { cout << "foo() " << sizeof( a ) << endl; } int main() { char bar[100] = { 0 }; cout << "main() " << sizeof( bar ) << endl; foo( bar ); return 0; } outputs main() 100 foo() 4 The questions: Why is the array passed as a point...