I saw that when I checked the size of struct it took 36 bytes, but the same variables (without the struct) took 33 bytes. Why?
1450
Why does the sizeof operator return a size larger for a structure than the total sizes of the structure's members?
66688
I have a directory with a few hundred files (real files, no symlinks, no subdirectories).  When I use ls -la and sum up the sizes in Excel I get 287190 bytes(?).  When I use du -b or du --apparent-size --block-size=1 I get 422358 bytes(?).  I thought those two things mean the same thing, why the difference?
18675
When should you use struct and not class in C#? My conceptual model is that structs are used in times when the item is merely a collection of value types.  A way to logically hold them all together into a cohesive whole.  I came across these rules :   A struct should represent a single value. A struct should have a memory footprint less than 16 bytes. A struct should not be changed after creation.   Do these rules work? What does a struct mean semantically?
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? }
59948
I originally had an array[1..1000] that was defined as a global variable. But now I need that to be n, not 1000 and I don't find out n until later. I know what n is before I fill the array up but I need it to be global therefore need a way to define the size of a global array at run time.  Context is filling an array with a linear transformation of the bytes in a file.  I don't know how big the file is until someone wants to open it and the files can be of any size.
18053
What is the difference between new/delete and malloc/free?  Related (duplicate?):
32907
Are there any downsides to passing structs by value in C, rather than passing a pointer?  If the struct is large, there is obviously the performancd aspect of copying lots of data, but for a smaller struct, it should basically be the same as passing several values to a function.  It is maybe even more interesting when used as return values. C only has single return values from functions, but you often need several. So a simple solution is to put them in a struct and return that.  Are there any reasons for or against this?  Since it might not be obvious to everyone what I'm talking about here, I'll give  a simple example.  If you're programming in C, you'll sooner or later start writing functions that look like this:  void examine_data(const char *ptr, size_t len) {     ... }  char *p = ...; size_t l = ...; examine_data(p, l);   This isn't a problem. The only issue is that you have to agree with your coworker in which the order the parameters should be so you use the same convention in all functions.  But what happens when you want to return the same kind of information? You typically get something like this:  char *get_data(size_t *len); {     ...     *len = ...datalen...;     return ...data...; } size_t len; char *p = get_data(&amp;len);   This works fine, but is much more problematic. A return value is a return value, except that in this implementation it isn't. There is no way to tell from the above that the function get_data isn't allowed to look at what len points to. And there is nothing that makes the compiler check that a value is actually returned through that pointer. So next month, when someone else modifies the code without understanding it properly (because he didn't read the documentation?) it gets broken without anyone noticing, or it starts crashing randomly.  So, the solution I propose is the simple struct  struct blob { char *ptr; size_t len; }   The examples can be rewritten like this:  void examine_data(const struct blob data) {     ... use data.tr and data.len ... }  struct blob = { .ptr = ..., .len = ... }; examine_data(blob);  struct blob get_data(void); {     ...     return (struct blob){ .ptr = ...data..., .len = ...len... }; } struct blob data = get_data();   For some reason, I think that most people would instinctively make examine_data take a pointer to a struct blob, but I don't see why. It still gets a pointer and an integer, it's just much  clearer that they go together. And in the get_data case it is impossible to mess up in the way I described before, since there is no input value for the length, and there must be a returned length.
16626
I'm trying to read binary data using C#. I have all the information about the layout of the data in the files I want to read. I'm able to read the data "chunk by chunk", i.e. getting the first 40 bytes of data converting it to a string, get the next 40 bytes.  Since there are at least three slightly different version of the data, I would like to read the data directly into a struct. It just feels so much more right than by reading it "line by line".  I have tried the following approach but to no avail:  StructType aStruct; int count = Marshal.SizeOf(typeof(StructType)); byte[] readBuffer = new byte[count]; BinaryReader reader = new BinaryReader(stream); readBuffer = reader.ReadBytes(count); GCHandle handle = GCHandle.Alloc(readBuffer, GCHandleType.Pinned); aStruct = (StructType) Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(StructType)); handle.Free();   The stream is an opened FileStream from which I have began to read from. I get an AccessViolationException when using Marshal.PtrToStructure.  The stream contains more information than I'm trying to read since I'm not interested in data at the end of the file.  The struct is defined like:  [StructLayout(LayoutKind.Explicit)] struct StructType {     [FieldOffset(0)]     public string FileDate;     [FieldOffset(8)]     public string FileTime;     [FieldOffset(16)]     public int Id1;     [FieldOffset(20)]     public string Id2; }   The examples code is changed from original to make this question shorter.  How would I read binary data from a file into a struct?
46616
Currently I am . Having never used a library for working with large integers before, this seems slow, on the order of 10 times slower, even for ulong length numbers. Does anyone have any better (preferably free) libraries, or is this level of performance normal?
73190
got me wondering about the differences between these three ways of measuring size: a kibibyte, a kilobit, and the conventional kilobyte.  I understand that these measurements have different uses (data transfer rate is measured in bits/sec), but I'm not quite sure if I can tell the difference between Mb and MB and MiB.  Here is a comment, reproduced below, taken from  (emphasis mine).     The C64 has 65536 bytes of RAM. By   convention, memory size is specified   in kibiBytes, data transfer rates in   kilobits, and mass storage in   whatever-the-manufacturers-think-of-now-Bytes. Harddrives use T, G, M and k on the   label, Windows reports the size in Ti,   Gi, Mi and ki. And those 1.44MB   floppys? Those are neither 1.44MB nor   1.44MiB, they are 1.44 kilokibibytes. That's 1440kiB or 1'474'560 bytes. –   Third
107627
Is it the same convention as those used in known programming languages such as Java and C? I'm trying to create a lot of variables and I want it to start with a number, but I'm not sure if it's okay.
93814
A friend &amp; I were debating how Inf's and NaN's are stored during lunch today.  Take Fortran 90 for example.  4-byte reals can obtain the value of Inf or NaN.  How is this stored internally?  Presumably, a 4-byte real is a number represented internally by a 32 digit binary number.  Are Inf's and NaN's stored as 33 bit binary numbers?
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
73638
Is there any advantage over using a class over a struct in cases such as these?  (note: it will only hold variables, there will never be functions)  class Foo {  private:       struct Pos { int x, y, z }; public:        Pos Position;  };   Versus:  struct Foo {    struct Pos { int x, y, z } Pos; };     Similar questions:
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.
7923
template&lt;typename T, size_t n&gt; size_t array_size(const T (&amp;)[n]) {     return n; }   The part that I don't get is the parameters for this template function. What happens with the array when I pass it through there that gives n as the number of elements in the array?
42535
If I have a class as follows     class Example_Class     {        private:          int x;           int y;         public:           Example_Class()           {               x = 8;              y = 9;          }        ~Example_Class()         { }     };   And a struct as follows  struct {    int x;    int y; } example_struct;   Is the structure in memory of the  example_struct simmilar to that in Example_Class  for example if I do the following  struct example_struct foo_struct; Example_Class foo_class = Example_Class();  memcpy(&amp;foo_struct, &amp;foo_class, sizeof(foo_struct));   will foo_struct.x = 8 and foo_struct.y = 9 (ie: the same values as the x,y values in the foo_class) ?  The reason I ask is I have a C++ library (don't want to change it) that is sharing an object with C code and I want to use a struct to represent the object coming from the C++ library. I'm only interested in the attributes of the object.  I know the ideal situation would be to have Example_class wrap arround a common structure between the C and C++ code but it is not going to be easy to change the C++ library in use.