hi frnz..
have a bit of trouble here...:(

i am allocating a dynamic integer array...
after allocation i assign different values to every array element.

now what i wish to do is to delete the array and free the memory element by element & not as a whole array at once.

(i.e)

void main()
{
    int col =5;
    int *arr=new int[col];
    arr[0]=1;
    arr[1]=2;
    arr[2]=3;
    arr[3]=4;
    arr[4]=5;                

    getch();
}

on performing

delete[] arr;

the whole memory is deallocated, but what i wish to do is to first delete and free the memory of arr[0],then arr[1] and so on till arr[4].
the thing is that i want to delete 4 bytes at a time...i dont wish to delete the whole 20 bytes at one go...

how to perform this????

Recommended Answers

All 13 Replies

hi..thnx a lot..
but i wud like not to use any STL..
could you suggest a method without using STL..ie. as i have declared a user defined array in above case


Use std::vector and the erase() method.
http://www.sgi.com/tech/stl/Vector.html

Oh, and void main is wrong; main returns int.

So create your own class which is a "works like STL vector" then.

Like linked lists for example

well.....if thats the only alternative I WILL....
else....if i do find another method...u wil be the first one to know it...!!!!!

So create your own class which is a "works like STL vector" then.

There's all sorts of ways you COULD solve it.

What there isn't (and what you seem to be after) is a simple 1-line statement which does it all for you.

well that was what i did ask for in the first place...
i wud just like to know one of those "all sorts of ways"....and believe me..i do not need any single line statemnt...the only thing i dont want is the use of STL... :)

There's all sorts of ways you COULD solve it.

What there isn't (and what you seem to be after) is a simple 1-line statement which does it all for you.

So why didn't you click my link on linked lists (post #5) ?

Hi...
thnx for the link..i did check it..
i had already tried the linked list method nd was able to delete the intermediate nodes..
i was just wondering as to how to solve it with an array...!!
this seems to be a bit tricky coz the pointer actually points to the beginning of the address nd deleting the pointer itself will make the starting address unavailable....nd thats where the whole problem arises...so kindly help with the problem at hand..
i have succedded with linked lists.... :)

So why didn't you click my link on linked lists (post #5) ?

1) How about declaring an array of pointers to pointers to int? Declare memory dynamically for both the array and the pointers it holds and then delete one pointer at a time.

2) How about the reverse of "expanding" an array? Declare temp using enough dynamic memory to hold the original array contents. Copy the oringinal to array contents to temp. Then delete the memory for the original array and redeclare memory that is on item less than before. Copy back the contents from temp, leaving out the item desired. Delete the memory from temp.

3) How about declaring a user defined array class than is based on an a list? The erase() method deletes the desired node. The [] operator returns the information in the desired location using a loop and the index number of the [] to determine how many nodes from the start ot f the list to travel.

Other people might have other ideas as well.

EDIT: it is readily possible to delete the first node of a list and still maintain access to the list if that's your concern with solving the task at hand using a list.

Hey buddy...
thnx for the info..:)
but i have some doubts over here...

1. in method 1 & 2 , the space complexity actually increases with increase in the size of array. i was just wondering if there's another method that could encounter this problem, as it needs to be done in constant space.

2. as far as the second one is concerned, i wud not like to use any STL containers or functions.

the problem here is whether this could be solved in constant space without using any inbuilt C++ library or containers...
I am really thankful for ur solutions, I am just wondering if u could still provide me better ones, with the above constraints.... :)

1) How about declaring an array of pointers to pointers to int? Declare memory dynamically for both the array and the pointers it holds and then delete one pointer at a time.

2) How about the reverse of "expanding" an array? Declare temp using enough dynamic memory to hold the original array contents. Copy the oringinal to array contents to temp. Then delete the memory for the original array and redeclare memory that is on item less than before. Copy back the contents from temp, leaving out the item desired. Delete the memory from temp.

3) How about declaring a user defined array class than is based on an a list? The erase() method deletes the desired node. The [] operator returns the information in the desired location using a loop and the index number of the [] to determine how many nodes from the start ot f the list to travel.

Other people might have other ideas as well.

EDIT: it is readily possible to delete the first node of a list and still maintain access to the list if that's your concern with solving the task at hand using a list.

For the record, none of my suggestions would require use of STL containers or algorithms, though they could be used if desired. But that's neither here nor there if you have other constraints to performance of the task.

Why the restrictions? Curiosity to see if it can be done or something else.

Why can't you use STL vectors?

I mean, the problem was solved 5 seconds after you posted.

The STL exists to stop people continually worrying about minutiae, and get on with solving the problem at hand.

If you're somehow worried about performance, then you'd be advised to actually finish the program and then profile it with real-world data. Premature Optimisation Disease (POD) has wasted many a month of programmer effort to save microseconds of run-time. Your chance of guessing where the hot-spots 'a-priori' are close to zero.

To delete the middle from an array, simply loop from [n+1] to the end of the array, and copy to [n].

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.