deallocating a single pointer from pointer array

Hi, how can i delete a single pointer from pointer array?
I'm was using this method and its wan't working

1
2
3
void deallocate(int* p){
    delete p; 
}

1
2
3
4
int* p = new int[5];
for(int i = 0; i < 5; i++){
    p[i] = i*i;
}


Here when i = 1 im getting runtime error probably because i deallocated all 5 elements with 1st call of this function.
How can i fix this?
1
2
3
for(int i = 0; i < 5; i++){
    deallocate(&p[i]);
}


Also how can delete[] p can know ho many elements in advance it has to deallocate from free store?
Last edited on
The array allocated below is contiguous memory
 
int *ia = new int[n]; // allocates n * sizeof(int) bytes of memory. 


You wouldn't want to (and can't as far as I know) free only a portion of this memory. You'd just call delete [] ia; to free the whole thing, as you allocated the whole thing at once to begin with.
but how can delete[] ia know how much memory to release starting from ia[0]? As far as I'm reading that's the problem with pointers to these arrays, they don't know their size, but how can delete[] suddenly know the size of an array?
Last edited on
As far as I know it's just a function of the language. for new (object); call delete, and for new (object)[n]; call delete []. I'm assuming the compiler keeps track of the details automagically.
Ok thanks man for for response :), will keep this open in case someone maybe knows some trick how to delete a single ellement from this kind of array :)
Edit : Not delete but deallocate so this element no longer points to anything but element next to it still does!
Also maybe someone knows about automagically, knowing the size of array, compiler!
Last edited on
So, to answer your former question, you're mistaking a pointer to an array of ints with a pointer to an array of pointer-to-ints. The first being an array of integers, and the second being an array of pointer-to-ints; where each element of the array is a pointer that you can allocate and de-allocate with new/delete.

You're trying to deallocate the address of an integer when you do:

1
2
3
for(int i = 0; i < 5; i++){
    deallocate(&p[i]);
}


As p[i] is an integer and &p[i] is the address of an integer. Delete only works on pointers, not addresses of integers.

What you want is an array of pointer-to-ints, where each element of the array can be allocated and deallocated with new.

I'm unsure if a construct like this exists; but you may be better off (at that point) just doing a vector of pointer-to-ints: std::vector<int*> vectorOfPointerToInts; Then you could use delete vectorOfPointerToInts[i]; as each element in the vector would be a pointer that you could allocate and deallocate.

Make sense?

EDIT: To be clear, a construct like that DOES exist (as I believe this is how a two-dimentional array is implemented), but I'm unsure of the syntax for declaring a pointer to an array of pointer-to-ints.
Last edited on
Tnx man a lot for response, but it was kind of a part of exercise of implementing my own vector class so i cant use already existing vector :)

Anyway i checked out how my solution worked with delete[] all array at once instead of deleting every element and i didn't notice any memory leaks so that's good!

Maybe solution to this problem would be different if i would be using malloc() and free() like some hints in exercise pointed out. But i was reading about it and saw that i should use new and delete in C++ so i choose to do it this way.
Thanks a lot for answer man :)
You're welcome. To answer your latter question, the C++ standard does not define how the size of an array is stored; but it is indeed stored somewhere - otherwise delete would not know how much memory to free.

A common implementation is to create an array 1 element larger than specified and store the size of the array at the first index, and return a pointer to the second index; So, programmatically, this would be array[-1]. However, because the C++ standard does not define how the size of an array must be stored, it can be done other ways and you cannot reliably predict 1 behavior across multiple environments.

See this article for a bit more info: http://stackoverflow.com/questions/975792/how-does-delete-know-the-size-of-an-array
Tnx again man, i feel like now i can close this thread for sure :)
Topic archived. No new replies allowed.