Why output is incorrect?  I think the output should be:-  19 for the struct inside the union  19 again the same because Union reserves memory for the largest variable.  43 and the outside Struct is 43 because 19+20+4=43  #include&lt;stdio.h&gt; //structure //====================================================== struct {     char a[20];     int b;     union {         double c;         struct {             char d[15];             float e;                }x;           }y;        }z; //main function //====================================================== int main() { printf("%u\n%u\n%u\n",sizeof(z.y.x),sizeof(z.y),sizeof(z)); return 0; }  //Output is 20 24 48 //Assumed 19 19 43
1450
Why does the sizeof operator return a size larger for a structure than the total sizes of the structure's members?
116782
Possible Duplicate:          see this code    #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 &lt;= TOTAL_ELEMENTS-2;d++)            printf("%d\n",array[d+1]);         return 0;    }    now this loop won't run. sizeof() would return an unsigned value so TOTAL_ELEMENTS has an unsigned value. now , coming to the for loop, please tell me if the unary operator '-' works on signed int 2 or an implicit conversion takes place into unsigned and then the '-' operator works.
114370
This is about files straight from the compiler, say g++, and the -o (outfile) flag.  If they are binary, shouldn't they just be a bunch of 0's and 1's?  When you cat them, you get unintelligible output but also intact words.  If you file them, you get the answer immediately - there seem to be no computation. Do the binary files in fact have headers with this kind of information?  I thought a binary executable was just the program just compiled, only in the form of machine instructions that your CPU can instantly and unambiguously understand. If so, isn't that instruction set just bit patterns? But then, what's all the other stuff in the binaries? How do you display the bits?  Also, if you somehow get hold of the manual of your processor, could you write a binary manually, one machine instruction at a time? That would be terribly ineffective, but very fascinating if you got it to work even for a "Hello World!" demo.
222
In several C++ examples I see a use of the type size_t where I would have used a simple int. What's the difference, and why size_t should be better?
30661
This is a question that was sparked by 's answer . Suppose I declare a class/struct like so: struct {      char A;     int B;     char C;     int D; };  Is it safe to assume that these members will be declared in exactly that order in memory, or is this a compiler dependent thing?  I'm asking because I had always assumed that the compiler can do whatever it wants with them. This leads into my next question.  If the above example causes memory alignment issues, why can the compiler not just turn that into something like this implicitly: struct {      char A;     char C;     int B;     int D; };  (I'm primarily asking about C++, but I'd be interested to hear the C answer as well) Related topics
30414
Why can't we initialize members inside a structure ?  example:  struct s {    int i = 10; };
111701
I'm messing around with some C code using floats, and I'm getting 1.#INF00, -1.#IND00 and -1.#IND when I try to print floats in the screen. What does those values mean?  I believe that 1.#INF00 means positive infinity, but what about -1.#IND00 and -1.#IND? I also saw sometimes this value: 1.$NaN which is Not a Number, but what causes those strange values and how can those help me with debugging?  I'm using  which I believe uses  representation for float point numbers.  Can someone list all those invalid values and what they mean?
64269
My friend produced a small proof-of-concept assembler that worked on x86. I decided to port it for x86_64 as well, but I immediately hit a problem.  I wrote a small piece of program in C, then compiled and objdumped the code. After that I inserted it to my python script, therefore the x86_64 code is correct:  from ctypes import cast, CFUNCTYPE, c_char_p, c_long  buffer = ''.join(map(chr, [ #0000000000000000 &lt;add&gt;:   0x55,                     # push   %rbp   0x48, 0x89, 0xe5,         # mov    %rsp,%rbp   0x48, 0x89, 0x7d, 0xf8,   # mov    %rdi,-0x8(%rbp)   0x48, 0x8b, 0x45, 0xf8,   # mov    -0x8(%rbp),%rax   0x48, 0x83, 0xc0, 0x0a,   # add    $0xa,%rax   0xc9,                     # leaveq    0xc3,                     # retq ]))  fptr = cast(c_char_p(buffer), CFUNCTYPE(c_long, c_long)) print fptr(1234)   Now, why does this script keeps doing segmentation fault whenever I run it?  I have yet a question about mprotect and no execution flag. It is said to protect against most basic security exploits like buffer overruns. But what is the real reason it's in use? You could just keep on writing until you hit the .text, then inject your instructions into a nice, PROT_EXEC -area. Unless, of course, you use a write protection in .text  But then, why have that PROT_EXEC everywhere anyway? Wouldn't it just help tremendously that your .text section is write protected?
2266
In follow up to , it appears that some numbers cannot be represented by floating point at all, and instead are approximated.  How are floating point numbers stored?  Is there a common standard for the different sizes?  What kind of gotchas do I need to watch out for if I use floating point?  Are they cross-language compatible (ie, what conversions do I need to deal with to send a floating point number from a python program to a C program over TCP/IP)?
95859
Why does C/C++ differentiates in case of array index out of bound #include &lt;stdio.h&gt; int main() {     int a[10];     a[3]=4;     a[11]=3;//does not give segmentation fault     a[25]=4;//does not give segmentation fault     a[20000]=3; //gives segmentation fault     return 0; }  I understand that it's trying to access memory allocated to process or thread in case of a[11] or a[25] and it's going out of stack bounds in case of a[20000]. Why doesn't compiler or linker give an error, aren't they aware of the array size? If not then how does sizeof(a) work correctly?
13911
Possible Duplicate:          Here is a test case:   void foo(int i, int j) {    printf("%d %d", i, j); } ... test = 0; foo(test++, test);   I would expect to get a "0 1" output, but I get "0 0"  What gives??
7842
I notice that modern C and C++ code seems to use size_t instead of int/unsigned int pretty much everywhere - from parameters for C string functions to the STL.  I am curious as to the reason for this and the benefits it brings.
44888
I'm reading The C++ Programming Language and in it Stroustrup states that the int value of a char can range from 0 to 255 or -127 to 127, depending on implementation. Is this correct? It seems like it should be from -128 to 127.  If not, why are their only 255 possible values in the second implementation possibility, not 256.
86999
Admittedly I don't get it. Say you have a memory with a memory word of length of 1 byte. Why can't you access a 4 byte long variable in a single memory access on an unaligned address(i.e. not divisible by 4), as it's the case with aligned addresses?
103238
I want to work with unsigned 8-bit variables in C++.  Either unsigned char or uint8_t do the trick as far as the arithmetic is concerned (which is expected, since AFAIK uint8_t is just an alias for unsigned char, or so the debugger presents it.  The problem is that if I print out the variables using ostream in C++ it treats it as char.  If I have:  unsigned char a = 0; unsigned char b = 0xff; cout &lt;&lt; "a is " &lt;&lt; hex &lt;&lt; a &lt;&lt;"; b is " &lt;&lt; hex &lt;&lt; b &lt;&lt; endl;   then the output is:  a is ^@; b is 377   instead of   a is 0; b is ff   I tried using uint8_t, but as I mentioned before, that's typedef'ed to unsigned char, so it does the same.  How can I print my variables correctly?  Edit: I do this in many places throughout my code.  Is there any way I can do this without casting to int each time I want to print?
11872
How can I figure out the size of a file, in bytes?  #include &lt;stdio.h&gt;  unsigned int fsize(char* file){   //what goes here? }
73035
I'm translating a library written in C++ to C#, and the keyword 'union' exists once. In a struct.  What's the correct way of translating it into C#? And what does it do? It looks something like this;  struct Foo {     float bar;      union {         int killroy;         float fubar;     } as; }