I was wondering if someone could shed some light on the deallocation-of-memory processes in c++.   If I have a struct that I declare static, such that it's constructor is the first thing to execute and its destructor is the last thing to execute:  struct initializer execute_before_and_after_main {      initializer() { init(); }      ~initializer() { cleanup(); } } static initializer execute_around_main;   And I then have something like:  class my_class {     my_object objects[100]; } extern my_class gobal_my_class; my_class global_my_class;   and main is not important here:  int main (int argc, char* argv[]) {     .... }    When cleanup() is called, is the objects array now containing deallocated/invalid memory? Is there a standard  sequence of initialisation/destruction that c++ implements here that someone could perhaps point me to?   Thanks  EDIT: I understand this type of code is possibly not the best practice, but I am still wondering if the behaviour is defined.
50943
Can I control the order static objects are being destructed? Is there any way to enforce my desired order? For example to specify in some way that I would like a certain object to be destroyed last, or at least after another static object?
40108
What does the volatile keyword do? In C++ what problem does it solve?  In my case, I have never knowingly needed it.
73055
I have a program in which I've lost the C++ source code.  Are there any good C++ decompilers out there?  I've already ran across .
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 ?
19458
Ignoring programming style and design, is it "safe" to call delete on a variable allocated on the stack?  For example:     int nAmount;    delete &amp;nAmount;   or  class sample { public:     sample();     ~sample() { delete &amp;nAmount;}     int nAmount; }
93044
What is better: void foo() or void foo(void)? With void it looks ugly and inconsistent, but I've been told that it is good. Is this true?  Edit: I know some old compilers do weird things, but if I'm using just GCC, is void foo() Ok? Will foo(bar); then be accepted?
13470
I am a little surprised by the following.  Example 1:  char s[100] = "abcd"; // declare and initialize - WORKS   Example 2:  char s[100]; // declare s = "hello"; // initalize - DOESN'T WORK ('lvalue required' error)   I'm wondering why the second approach doesn't work. It seems natural that it should (it works with other data types)? Could someone explain me the logic behind this?
49393
I'm working with some code that widely uses the idiom of returning a pointer to a static local variable.  eg:  char* const GetString() {   static char sTest[5];   strcpy(sTest, "Test");   return sTest; }   Am I right in thinking that this is safe?  PS, I know that this would be a better way of doing the same thing:  char* const GetString() {   return "Test"; }   Edit: Apologies, the function signature should of course be:  const char* GetString();
88792
Let's say I write a DLL in C++, and declare a global object of a class with a non-trivial destructor.  Will the destructor be called when the DLL is unloaded?
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.
25150
This is a question that's been nagging me for some time. I always thought that C++ should have been designed so that the delete operator (without brackets) works even with the new[] operator.  In my opinion, writing this:  int* p = new int;   should be equivalent to allocating an array of 1 element:  int* p = new int[1];   If this was true, the delete operator could always be deleting arrays, and we wouldn't need the delete[] operator.  Is there any reason why the delete[] operator was introduced in C++?  The only reason I can think of is that allocating arrays has a small memory footprint (you have to store the array size somewhere), so that distinguishing delete vs delete[] was a small memory optimization.
19528
In C++, when is it best to use the stack? When is it best to use the heap?
94655
I was experimenting with C++ and found the below code as very strange.   class Foo{ public:     virtual void say_virtual_hi(){         std::cout &lt;&lt; "Virtual Hi";     }      void say_hi()     {         std::cout &lt;&lt; "Hi";     } };  int main(int argc, char** argv) {     Foo* foo = 0;     foo-&gt;say_hi(); // works well     foo-&gt;say_virtual_hi(); // will crash the app     return 0; }   I know that the virtual method call crashes because it requires a vtable lookup and can only work with valid objects.   I have the following questions    How does the non virtual method say_hi work on a NULL pointer? Where does the object foo get allocated?   Any thoughts?
122083
A colleague of mine recently got bitten badly by writing out of bounds to a static array on the stack (he added an element to it without increasing the array size).  Shouldn't the compiler catch this kind of error?  The following code compiles cleanly with gcc, even with the -Wall -Wextra options, and yet it is clearly erroneous:  int main(void) {   int a[10];   a[13] = 3;  // oops, overwrote the return address   return 0; }   I'm positive that this is undefined behavior, although I can't find an excerpt from the C99 standard saying so at the moment.  But in the simplest case, where the size of an array is known as compile time and the indices are known at compile time, shouldn't the compiler emit a warning at the very least?
97981
The .NET  implies that if you write a finalizer, and implement IDisposable, that your finalizer needs to explicitly call Dispose. This is logical, and is what I've always done in the rare situations where a finalizer is warranted.  However, what happens if I just do this:  class Foo : IDisposable {      public void Dispose(){ CloseSomeHandle(); } }   and don't implement a finalizer, or anything. Will the framework call the Dispose method for me?  Yes I realise this sounds dumb, and all logic implies that it won't, but I've always had 2 things at the back of my head which have made me unsure.   Someone a few years ago once told me that it would in fact do this, and that person had a very solid track record of "knowing their stuff." The compiler/framework does other 'magic' things depending on what interfaces you implement (eg: foreach, extension methods, serialization based on attributes, etc), so it makes sense that this might be 'magic' too.    While I've read a lot of stuff about it, and there's been lots of things implied, I've never been able to find a definitive Yes or No answer to this question.
53259
Is it ever acceptable to have a  in your C or C++ application?  What if you allocate some memory and use it until the very last line of code in your application (for example, a global object's destructor)? As long as the memory consumption doesn't grow over time, is it OK to trust the OS to free your memory for you when your application terminates (on Windows, Mac, and Linux)? Would you even consider this a real memory leak if the memory was being used continuously until it was freed by the OS.  What if a third party library forced this situation on you? Would refuse to use that third party library no matter how great it otherwise might be?  I only see one practical disadvantage, and that is that these benign leaks will show up with memory leak detection tools as false positives.
98843
In my initial basic tests it is perfectly safe to do so.  However, it has struck me that attempting to manipulate this later in a function that deletes this could be a runtime error.  Is this true, and is it normally safe to delete this? or are there only certain cases wherein it is safe?