dynamic memory allocation

Hello everyone,
I have a simple question

If make a char pointer and then create dynamically a char array of say 5 size....and then at the pressing a key i want to delete /release memory from specific index say index 2,and finally when my program ends it should delete/release memory from the remaining indexes...how is it going to be in vc++...

in the following code i tried to do this but it gives error


#include<iostream>
#include<conio.h>
using namespace std;
void main() {
char *text;
text = new char[5];

for(int i=0;i<5;i++)
cin>>text[i];

getch();
//deleting
delete text[4];
getch();
//deleting rest of array
delete[] text;
}



thanks
Please use [code]int a=5; // code [/code] tags in future posts.

Anyway, you cannot release memory for a specific index as you ask.
Unless your array is allocated element by element, I mean by that that it's not really an array but you have allocated 5 ints for instance you cannot do what you ask. Mainly because if you want to delete the remaining you are going to redelete some and that's not good.
Topic archived. No new replies allowed.