Question (A):

void Fun(void){ int *a = new int[10]; int *b = new int[10]; CopyMemory(b, a, 10); } 

If further we do not use an a[] array:

  1. We need to free the memory: delete[] a ?
  2. And if instead of 1 we write a = NULL ?
  3. And if we do not execute either 1 or 2, but just quit Fun ?

Question (B):

 void Fun(int **c){ int *a = new int[10]; int *b = new int[100]; CopyMemory(b, a, 10); *c = b; delete[] a; } 

So it will be right?

Question (C):

  int *a = new int[10]; int *b = new int[100]; CopyMemory(b, a, 10); delete[] a; a = b; 

Is everything correct here?

    3 answers 3

    The correct answer is 1. In C ++, there is no garbage collector, so all responsibility for freeing the memory lies with the developer.
    The second answer is wrong, because a is just a pointer, i.e. the variable storing the address of the beginning of the allocated block of memory and changing the pointer will not affect the memory itself.
    It should be noted that in C ++ it is considered a good practice to use smart pointers - wrapper classes over pointers that care about freeing memory (for example, auto_ptr from the standard library).

      Code code with appropriate tags, please. Absolutely unreadable happened.

      1. Actually, yes.
      2. We will write, and we will write. The program or throw exception, or will limp further.
      3. Lose 20 * sizeof (int) bytes.

      Exactly in order to avoid such problems come up with smart pointers. They combine the best of stack and dynamic memory allocation. Stack memory is automatically cleaned when it goes out of scope, and the speaker allows you to allocate large amounts of memory, because stack is always not enough. Thus, creating a smart pointer on the stack, then we can not worry about whether to clean the memory. As soon as the pointer crashes, all memory indicated by it will be released. True, you need to be careful with the assignments and copying pointers. Because at the same time, several pointers will point to one memory area. And then all sorts of unpleasant things can happen. Therefore, further differentiation of smart pointers occurs. There are “strong” who own objects, and there are “weak” who do not participate in the counting of the number of links.


      int * a = new [10];

      so to write is incorrect. Probably. Because new takes type. Those. we need to rewrite it as:

      int * a = new int [10]; // allocate memory for an array of 10 ints


      int * a = new [10];
      int * b = new [100];
      delete [] a;
      a = b;

      Yeah. And then you almost certainly do

       delete [] a; delete [] b; //FAIL!? 
      • Stop making fun! :) It is clear that after new there is a type! (forgot to write) Question (C) is still valid! - rejie 7:37 pm
      • And tell me - what prevents you from making your own my_super_puper_array class, where do you implement the necessary functionality? Well, or use any standard template type. See towards STL and boost. Surely there is something more successful than reinventing the wheel. - gecube 9:01 pm
      • When you invent a bike you start to understand not only how to pedal, but also how to pump up the wheel! :) - rejie
      • The function of changing the dimension of the array with data preservation: Void RedimPreserve (double ** Array, long OldSize, long NewSize) {double * tmpArray = new double [OldSize]; CopyMemory (tmpArray, * Array, OldSize * sizeof (double)); delete [] * Array; * Array = tmpArray;} In the end, I think this code should work optimally !? Or are there other opinions !? - rejie

      (A) memory is lost, how to use the result of the work of Fun () is unclear.

      (B) not my opinion is correct. The first (further unused) array is freed, the address of its copy is transferred from the function and can be further used.

      (C) I do not understand what you want. Both pointers (a and b) point to a copy of the original a [], so what's next?

      • (C) - Increasing the size of the array while preserving what was in it! (analogue of the function VB Redim Preserve a (0 to 99)) - rejie
      • one
        realloc () what is not to like? - avp
      • Well ... I don `t know, and in general when it makes sense to use not new but malloc, this is a C-shnaya library!? And it is also called by the operator new (C ++) - rejie
      • new - calls the constructor. malloc - does not cause. Therefore, new in OOP code is preferable, since we get the object ready for work. And when allocating memory with malloc, you will have to conjure. - gecube
      • And if I allocate memory for an array with the operator new, and then I need to change the dimension, saving the data, can I use realoc () or will I need something else to whisper!? :) - rejie