#include&lt;stdio.h&gt; #include&lt;malloc.h&gt;  struct node {     int data;     struct node* left;     struct node* right; }; struct node* newNode(int data) {     struct node* node=(struct node*)malloc(sizeof(struct node));     node-&gt;data=data;     node-&gt;left=NULL;     node-&gt;right=NULL;      return (node); };  int height(struct node* root) {     static int lheight,rheight;     if(root==NULL)         return;     else     {         lheight=height(root-&gt;left)+1;         rheight=height(root-&gt;right)+1;         if(lheight&gt;rheight)             return lheight;         else             return rheight;     }  }  int main()     {           struct node* root=newNode(1);           root-&gt;left=newNode(2);           root-&gt;right       = newNode(3);           root-&gt;left-&gt;left  = newNode(4);           root-&gt;left-&gt;right = newNode(5);           printf("%d",height(root));           return 0;     }   This program gives two different result. One is 2 for the above program where I use static and 3 if static is not used. Please explain the reason for the change in output using static.
26363
I've seen the word static used in different places in C code; is this like a static function/class in C# (where the implementation is shared across objects)?
86209
I know how to create an array of structs but with a predefined size. However is there a way to create a dynamic array of structs such that the array could get bigger?  For example:      typedef struct     {         char *str;     } words;      main()     {         words x[100]; // I do not want to use this, I want to dynamic increase the size of the array as data comes in.     }   Is this possible?    I've researched this: words* array = (words*)malloc(sizeof(words) * 100);  I want to get rid of the 100 and store the data as it comes in. Thus if 76 fields of data comes in, I want to store 76 and not 100. I'm assuming that I don't know how much data is coming into my program. In the struct I defined above I could create the first "index" as:      words* array = (words*)malloc(sizeof(words));   However I want to dynamically add elements to the array after. I hope I described the problem area clearly enough. The major challenge is to dynamically add a second field, at least that is the challenge for the moment.    I've made a little progress however:      typedef struct {         char *str;     } words;      // Allocate first string.     words x = (words) malloc(sizeof(words));     x[0].str = "john";      // Allocate second string.     x=(words*) realloc(x, sizeof(words));     x[1].FirstName = "bob";      // printf second string.     printf("%s", x[1].str); --&gt; This is working, it's printing out bob.      free(x); // Free up memory.      printf("%s", x[1].str); --&gt; Not working since its still printing out BOB even though I freed up memory. What is wrong?   I did some error checking and this is what I found. If after I free up memory for x I add the following:      x=NULL;   then if I try to print x I get an error which is what I want. So is it that the free function is not working, at least on my compiler? I'm using DevC??    Thanks, I understand now due to:     FirstName is a pointer to an array of char which is not being allocated by the malloc, only the pointer is being allocated and after you call free, it doesn't erase the memory, it just marks it as available on the heap to be over written later. – MattSmith   Update  I'm trying to modularize and put the creation of my array of structs in a function but nothing seems to work. I'm trying something very simple and I don't know what else to do. It's along the same lines as before, just another function, loaddata that is loading the data and outside the method I need to do some printing. How can I make it work? My code is as follows:      # include &lt;stdio.h&gt;     # include &lt;stdlib.h&gt;     # include &lt;string.h&gt;     # include &lt;ctype.h&gt;      typedef struct     {         char *str1;         char *str2;     } words;      void LoadData(words *, int *);      main()     {         words *x;         int num;          LoadData(&amp;x, &amp;num);          printf("%s %s", x[0].str1, x[0].str2);         printf("%s %s", x[1].str1, x[1].str2);          getch();     }//      void LoadData(words *x, int * num)     {         x = (words*) malloc(sizeof(words));          x[0].str1 = "johnnie\0";         x[0].str2 = "krapson\0";          x = (words*) realloc(x, sizeof(words)*2);         x[1].str1 = "bob\0";         x[1].str2 = "marley\0";          *num=*num+1;     }//   This simple test code is crashing and I have no idea why. Where is the bug?
47371
Let's say I have some Java code:  public class SomeClass {     static {         private final double PI = 3.14;         private final double SOME_CONSTANT = 5.76;         private final double SOME_OTHER_CONSTANT = 756.33;     }    //rest of class }   If a thread is initializing SomeClass's Class object and is in the middle of initializing the values in the static block when a second thread wants to load SomeClass's Class again, what happens to the static block?  Does the second thread ignore it assuming it's already initialized even though the first thread is not done? Or does something else happen?
62317
In the early days of C++ when it was bolted on top of C, you could not use NULL as it was defined as (void*)0. You could not assign NULL to any pointer other than void*, which made it kind of useless. Back in those days, it was accepted that you used 0 (zero) for null pointers.  To this day, I have continued to use zero as a null pointer but those around me insist on using NULL. I personally do not see any benefit to giving a name (NULL) to an existing value - and since I also like to test pointers as truth values:  if (p &amp;&amp; !q)   do_something();   then using zero makes more sense (as in if you use NULL, you cannot logically use p &amp;&amp; !q - you need to explicitly compare against NULL, unless you assume NULL is zero, in which case why use NULL).  Is there any objective reason to prefer zero over NULL (or vice versa), or is all just personal preference?  Edit: I should add (and meant to originally say) that with RAII and exceptions, I rarely use zero/NULL pointers, but sometimes you do need them still.
46005
If I have a source.c file with a struct:  struct a {      int i;     struct b {         int j;     } };   How can this struct be used in another file (i.e. func.c)?  Should I create a new header file, declare the struct there and include that header in func.c?  Or should I define the whole struct in a header file and include that in both source.c and func.c?  How can the struct be declared extern in both files?  Should I typedef it? If so, how?
83585
I think I've got it down to the most basic case:  int main(int argc, char ** argv) {   int * arr;    foo(arr);   printf("car[3]=%d\n",arr[3]);   free (arr);   return 1; }  void foo(int * arr) {   arr = (int*) malloc( sizeof(int)*25 );   arr[3] = 69; }   The output is this:  &gt; ./a.out   car[3]=-1869558540  a.out(4100) malloc: *** error for object 0x8fe01037: Non-aligned pointer                          being freed  *** set a breakpoint in malloc_error_break to debug &gt;   If anyone can shed light on where my understanding is failing, it'd be greatly appreciated.
43933
#include &lt;stdio.h&gt; int main() {     unsigned long long int num = 285212672; //FYI: fits in 29 bits     int normalInt = 5;     printf("My number is %d bytes wide and its value is %ul. A normal number is %d.\n", sizeof(num), num, normalInt);     return 0; }   Output:  My number is 8 bytes wide and its value is 285212672l. A normal number is 0.   I assume this unexpected result is from printing the unsigned long long int. How do you printf() an unsigned long long int?
97462
This has been something that has bothered me for ages now.  We are all taught in school (at least, I was) that you MUST free every pointer that is allocated.  I'm a bit curious, though, about the real cost of not freeing memory.  In some obvious cases, like when malloc is called inside a loop or part of a thread execution, it's very important to free so there are no memory leaks.  But consider the following two examples:  First, if I have code that's something like this:  int main() {     char *a = malloc(1024);     /* Do some arbitrary stuff with 'a' (no alloc functions) */     return 0; }   What's the real result here?  My thinking is that the process dies and then the heap space is gone anyway so there's no harm in missing the call to free (however, I do recognize the importance of having it anyway for closure, maintainability, and good practice).  Am I right in this thinking?  Second, let's say I have a program that acts a bit like a shell.  Users can declare variables like aaa = 123 and those are stored in some dynamic data structure for later use.  Clearly, it seems obvious that you'd use some solution that will calls some *alloc function (hashmap, linked list, something like that).  For this kind of program, it doesn't make sense to ever free after calling malloc because these variables must be present at all times during the program's execution and there's no good way (that I can see) to implement this with statically allocated space.  Is it bad design to have a bunch of memory that's allocated but only freed as part of the process ending?  If so, what's the alternative?
138
What is the main difference between an inner class and a static nested class in Java? Does design / implementation play a role in choosing one of these?
32521
I came across this strange code snippet which compiles fine:  class Car {     public:     int speed; };  int main() {     int Car::*pSpeed = &amp;Car::speed;     return 0; }   Why does C++ have this pointer to a non-static data member of a class? What is the use of this strange pointer in real code?
19528
In C++, when is it best to use the stack? When is it best to use the heap?
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??
72809
When you create an instance of a class with the new operator, memory gets allocated on the heap. When you create an instance of a struct with the new operator where does the memory get allocated, on the heap or on the stack ?
58615
Have you ever tried this before?  static void Main(string[] args) {     int x = 10;     x = x++;     Console.WriteLine(x); }   Output: 10.  but for   static void Main(string[] args) {     int x = 10;     x++;     Console.WriteLine(x); }   Output: 11.  Could anyone explain why this?
1982
The following code is generating a stack overflow error for me  int main(int argc, char* argv[]) {     int sieve[2000000];     return 0; }   How do I get around this? I am using Turbo C++ but would like to keep my code in C  EDIT:  Thanks for the advice.  The code above was only for example, I actually declare the array in a function and not in sub main.  Also, I needed the array to be initialized to zeros, so when I googled malloc, I discovered that calloc was perfect for my purposes.  Malloc/calloc also has the advantage over allocating on the stack of allowing me to declare the size using a variable.
73357
On the discussion of dynamic memory here:   The author states:     A memory block like this can effectively be used as a more flexible array. This approach is actually much more common in real-world C programs. It's also more predictable and flexible than a "variable size array"   The type of memory block he is talking about is as such:  const int size = 5; int * array = calloc(size, sizeof(int));   and then using another pointer to iterate over the array:  int * index = array; for (i = 0; i &lt; size; i++) {     *index = 1; // or whatever value     index++; }   My question is how is this method better than a standard variable sized array like this?:  int array[variable];   or dynamic:  char name[] = "Nick";   The author doesn't really shed much light as to why I should prefer the former method to the latter. Or more specifically: How is it more "predictable and flexible"?
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?