sizeof

In C are malloc(256) and malloc(sizeof(char)*256) equivalent?

I see that people often write C code such as: char *ptr = malloc(sizeof(char)*256); Is that really necessary? The standard says that sizeof(char)==1 by definition, so doesn't it make sense just to write: char *ptr = malloc(256); Thanks, Boda Cydo. ...

On which platforms/compilers sizeof(short) is NOT 2?

Tried to look for that and didn't find an answer. Can anyone help? Thanks. ...

What does sizeof do?

What is the main function of sizeof (I am new to C++). For instance int k=7; char t='Z'; What do sizeof (k) or sizeof (int) and sizeof (char) mean? ...

Is there a bit-equivalent of sizeof() in C?

Sizeof() doesn't work when applied to bitfields: # cat p.c #include<stdio.h> int main( int argc, char **argv ) { struct { unsigned int bitfield : 3; } s; fprintf( stdout, "size=%d\n", sizeof(s.bitfield) ); } # gcc p.c -o p p.c: In function ‘main’: p.c:5: error: ‘sizeof’ applied to a bit-field ...obviously, since it...

Funtion sizeof() in C

Consider the program main() { printf("%d %d %d",sizeof('3'),sizeof("3"),sizeof(3)); } output from a gcc compiler is: 4 2 4 Why is it so? ...

sizeof(void) equals 1 in C ?

Possible Duplicate: What is the size of void? Hi all ! I am using gcc for compiling my C programs, just discovered accidentally that the sizeof(void) is 1 byte in C. Is there any explanation for this ? I always thought it to be ZERO (if it really stores nothing) ! Thanks ! ...

How does this "size of array" template function work?

Possible Duplicates: Can someone explain this template code that gives me the size of an array? Magic arguments in function templates Can someone explain how this code works? I know that the purpose of this code is to get the length of an array, but I don't know how this code works: template<typename T, int size> int GetArr...

PHP: searching for the coincidence in the string

I would like to filter HTTP_REFERER where visitors come to my site from search engines. I want to ignore storing HTTP_REFERER information for the visitors came from the search engines. Could you please help with the PHP script? I have this, but not correct script: <? $exp_list = array('google', 'yahoo'); // exapmple of one HTTP_REFERE...

Understanding sizeof(char) in 32 bit C compilers

(sizeof) char always returns 1 in 32 bit GCC compiler. But since the basic block size in 32 bit compiler is 4, How does char occupy a single byte when the basic size is 4 bytes??? Considering the following : struct st { int a; char c; }; sizeof(st) returns as 8 as agreed with the default block size of 4 bytes (since 2 blocks are a...

Size of two structs are the same although one of them has a struct instance in them

typedef struct BaseMessage { int u32DeviceID : 32; int u32CoreID : 32; unsigned int u16Class : 16; unsigned int u8OpCode : 8; unsigned int u16CRC : 16; } BaseMessage; typedef struct MessageWithParameters { BaseMessage base; int u8Param1 : 8; int u8Param2 : 8; } MessageWithParameters; typedef ...

How to get the size of an object via reference?

Suppose I have a class class Foo { : : } I have another function void getf( Foo &f) { : : std::cout<<sizeof f<<std::endl; } After I process the data and assign a lot of data to f (vector included in Foo members), I need the size of f object However, as what I did above, I always get 16, which is the size of a reference here....

intializing allocating the size of an array

Hello, gcc 4.4.3 c89 I am wondering why I can't allocate the size of the array when initializing an array of pointers to char. I get the following error: variable-sized object may not be initialized This works ok. However, the sizeof of will return 4 bytes as a char * is 4 bytes in size. Which is no good, as its not the actual size...

C: sizeof single struct member

Hi, I am trying to declare a struct that is dependent upon another struct. I want to use sizeof to be safe/pedantic. typedef struct _parent { float calc ; char text[255] ; int used ; } parent_t ; Now I want to declare a struct child_t that has the same size as parent_t.text. How can I do this? (Pseudo-code below.) typedef st...

evaluation of operand in sizeof operator

As sizeof operator evaluates operand if it's a VLA so I tried to test it as : #include<stdio.h> int main(void) { int sz=20,i=0,j=0; int arr[sz]; printf("%d\n",sizeof((++i,sz))); printf("%d\n",sizeof((++j,arr))); printf("%d\n%d\n",i,j); } I thought that i won't increment as sz is not VLA but j would increment as arr is VL...

What is the size of a Nullable<Int32>?

So, a couple of questions, actually: An int (Int32) is specified to be (obviously) 32 bits. What about an int? (Nullable<int>)? My gut tells me that it would be 32 bits for the integer plus 8 more bits for the boolean, but perhaps the implementation is more intricate than that. I would have answered my own question using sizeof(int?); ...

How does sizeof know the size of the operand array?

This may be a stupid question but how does the sizeof operator know the size of an array operand when you don't pass in the amount of elements in the array. I know it doesn't return the total elements in the array but the size in bytes, but to get that it still has to know when the array ends. Just curious as to how this works. ...

Is there a guarantee as to the size of a class that contains an array?

Given: template <int N> struct val2size { char placeholder[N]; }; Is there any guarantee that sizeof(val2size<N>) == N? ...

Can I get the size of a struct field w/o creating an instance of the struct?

It's trivial to get the size of a struct's field in C++ if you have an instance of the struct. E.g. (uncompiled): typedef struct Foo { int bar; bool baz; } Foo; // ... Foo s; StoreInSomething(s.bar, sizeof(s.bar)); // easy as pie Now I can still do something like this, but with the interface I'm implementing (I get a BOOL t...

Size of a struct with two void pointers is 4?

I don't understand why struct e{ void * a; void * b[]; } has sizeof(e) == 4 while struct f{ void * a; void * b; } has sizeof(f) == 8. ...

How to declare a C array that takes up all free space in a memory section?

Assume I have a 128KB memory region. In my linker directives I split this region into three sections: .section_text .section_data .section_bss The size of each section is unknown pre-compilation, but I have constrained .section_bss to use all remaining space within the memory region after .section_text and .section_data are allocate...