0

How to change length of mass?

#include <iostream> using namespace std; int main() { int numbers[1]; numbers[0]=23; numbers[1]=45; cout << "before:" << endl; for(int x=1;x>=0;x--) { cout << numbers[x] << "," << endl; } cout << "size = " << sizeof(numbers)/sizeof(numbers[0])<<endl; //---------------- //try to cut mass and make it length 1 delete numbers[1]; //numbers[1]=NULL; //---------------- cout << "after:" << endl; for(int x=1;x>=0;x--) { cout << numbers[x] << "," << endl; } cout << "size = " << sizeof(numbers)/sizeof(numbers[0])<<endl; return 0; }

8th Jan 2019, 12:20 AM
Costa Jis
Costa Jis - avatar
6 Respuestas
+ 1
An array is a pointer for consecutive chunks of memory, this means that if you try to delete one of the indexes of the array, you are in effect trying to delete a chunk of memory.
8th Jan 2019, 12:58 AM
Ulisses Cruz
Ulisses Cruz - avatar
0
Costa Jis, If I understood, mass is in the array 'numbers' in index 1. So to change it to 1 just do: numbers[1] = 1;
8th Jan 2019, 12:28 AM
Ulisses Cruz
Ulisses Cruz - avatar
0
I mean mass ‘numbers’ have length 2. (numbers[0],numbers[1]). How to change it length to 1? At result to have only numbers[0]? How to delete index [1]?
8th Jan 2019, 12:34 AM
Costa Jis
Costa Jis - avatar
0
In my limited knowledge about C++ you cannot really delete index 1. You just have to use only index 0 and if you will, add the a termination character ('\0') to index.
8th Jan 2019, 12:44 AM
Ulisses Cruz
Ulisses Cruz - avatar
0
How to write it? numbers[1]=‘\0’? I can’t believe that in c++ we cannot delete object from memory
8th Jan 2019, 12:51 AM
Costa Jis
Costa Jis - avatar
0
we can rewrite elements to a new chunk of memory as like as we want and then delete the old massive, is that operation correct?
8th Jan 2019, 7:10 AM
Costa Jis
Costa Jis - avatar