short *massTempOld = new short[*size]; for (short i = 0; i < *size; i++) massTempOld[i] = mass[i]; delete [] mass; (*size) += (*sizeTemp); short *mass = new short[*size]; for (short i = 0; i < *size; i++) { if (i < (*size - *sizeTemp)) mass[i] = massTempOld[i]; else mass[i] = massTemp[i]; } delete sizeTemp; delete [] massTemp; delete [] massTempOld; 

Before the switch, memory is allocated for the mass[size] array and generate elements. In one of the cases, I create an array of massTempOld[size] in which I write the elements mass[size] , then delete the memory delete[]mass and allocate memory for a given size larger (*size) += (*sizeTemp)short *mass = new short[*size]; and write down there old + new items. After exiting the switch, the garbage from the mass[size] array is displayed on the screen. Tell me what the problem is. Thank!

  • one
    That's interesting, where the legs grow from this strange, but consistently repetitive manner to start an intermediate temporary array, when you need to re-allocate the main array. This stamped meaningless “transfusion” of data to and fro is constantly consistently encountered. It is clear that this comes from some kind of “methodics”. What is this tutorial? - AnT September
  • one
    What is delete sizeTemp; ? Did you have a single variable in dynamic memory allocated? What for? What is massTemp , where did it come from and what size does it have? - AnT
  • one
    what is bad std :: vector? - Artemy Vysotsky September
  • @ArtemyVysotsky, I'm pretty sure that this is some kind of lab. In the first year we could only use a C-subset of C ++, it was impossible to use std :: cout and the like. It was argued by the fact that we have to imagine how this works at a low level. But nobody explained why printf is more understandable than std :: cout. One lecturer generally in the fifth year demanded to write programs without using std :: vector and analogs (there needed an abstraction for mathematical vectors). I had to write my class based on unique_ptr<T[]> . Java and C # programmers were indignant. - Ariox

2 answers 2

... and allocate memory using this pointer ...

According to which such "same index"? Your code declares a completely new local pointer mass

 short *mass = new short[*size]; 

which has nothing to do with the original pointer mass .

The memory specified by the source mass pointer you freed

 delete [] mass; 

and more you didn’t touch this original mass pointer at all. Those. this original mass pointer remains to indicate "nowhere" (to the freed memory). Further behavior when accessing through this mass not defined. You just got trash on the screen, and it could be worse ...

PS See also my comment in question. Why you need to create an intermediate array massTempOld is not clear.

  • Thanks for your reply! I just learn so do not judge strictly. With the pointer figured out. But how can you re-allocate the main array without losing the data that lay there? - Andrey1512

If you have an array of mass in size and the task is to (manually) re-allocate memory with element-by-element data copying, then you do not need to create extra temporary arrays. It is enough to allocate memory for the new size2 , transfer all the data from the old array into it, and then delete the old array, and the new one can be reassigned to the old pointer:

 int* mass2 = new int[size2]; // выделяем новую память for (int i = 0; i < size; ++i) mass2[i] = mass[i]; // копируем старые данные delete [] mass; // удаляем старый массив mass = mass2; // переназначаем указатели 

For additional protection, you can reset the pointer mass2 :

 mass2 = 0; 

With this, we kind of say that he no longer owns the data and protects himself from accidentally freeing the memory again by calling delete [] mass2 . Calling delete on a null pointer is safe and does nothing.