deleting array elements
let's say we have a unsigned char pointer with allocated memory of 1 million bytes. Could you give a little code sample which shows how to eliminate first 1000 bytes? the little similar situation is like below:
unsigned char array:
12,34,67,99,215,250,123,67
i want to make it:
215,250,123,67
I want to see the most efficient way to do this with no redundant memory and no memory leak or corruptions. do i have to use memcpy, if so i think it is not so efficient.
thank you.
asilter
Junior Poster in Training
60 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
unsigned char *p2 = p + 1000;
in this way, freeing p will cause freeing p2. i must free the memory of 1000 bytes after assigning to p2. i do not want any memory leaks.
asilter
Junior Poster in Training
60 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
So you want a somewhat dynamic array. For really dynamic array you should implement a linked list, which is not very difficult either, but you must be careful because mistakes in implementing it can easily cause a memory leak. But it depends, how are you going to use your array. If you want to use it for example for fifo buffer, it's much easier to implement it with a normal static array, but of course don't forget to always check the array boundary. If you only need to delete elements from the beginning, you may consider writing array in reverse, which is not difficult, but is somewhat easier and more logical to handle. But if you may need to delete anywhere in the array, then again it's important how are you going to use it. Is the order of the elements important or not, how much your array is expected to grow, how much it would be changed. Based on that, in some cases, especially when the order of the elements is not important, you may just leave some empty places where you delete, and add new values there when necessary, this is also not difficult to implement, and has been used for example even in the indexes of certain databases. But of course when everything is expected to change too much, and has to be fast at that, you may consider using a linked list, though there would rarely be a need for that, especially considering that a lot of memory is available today, and we don't so much have to care about efficient use of it.
Say that we go through the array element by element. Then the trick is to use two arrays, one forward and one reverse, and these arrays can be static. Every time we go forward, we write one element from the end of the reverse array, to the end of the forward array, vice versa if we move back. Then, every step we can delete or add as much information as we want, to the location where we are at the moment.
Sometimes that is much more bigger than 1000 bytes and memcpy slows my program :) It's a very little delay but irritating. I wonder if I find a better solution.
I need the erase first 1000, because i'm calling this function from a loop, i do not want to query the same 1000 bytes message from the front of the array in the next calling.
function abc(unsigned char ** ucBinary, unsigned char ** ucB1000, int nSizeOfArray)
{
int i=0;
*ucB1000 = malloc(1000);
unsigned char * ucDummy = *ucBinary;
unsigned char * ucNew = NULL;
for(i=0; i<nSizeOfArray; i++)
{
if(ucDummy[i] == 'B' && ucDummy[i+1] == 'U' && ucDummy[i+2] == 'F' && ucDummy[i+3] == 'R')
{
memcpy(*ucB1000,ucDummy,1000);
ucNew = malloc(nSizeOfArray-1000);
memcpy(ucNew,ucDummy+1000,nSizeOfArray-1000);
free(*ucBinary);
*ucBinary = ucNew;
}
}
}
It has just come to my mind keeping index without memcpy and free in the function. I will try :). Send the starting index to function to look every time. And when the loop finishes, free the ucBinary outside.
Thanx.
asilter
Junior Poster in Training
60 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
p2 is just a reference into the allocated memory, it's not the controlling reference for the memory it points to...
if I;
free(p2);
what happens? don't I get segmentation fault when ;
printf("%d\n",p[1001]);
asilter
Junior Poster in Training
60 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
Thanks for the post, IcantC, but take note that this thread is four years old. Perhaps your expertise would be better spent on current threads? :)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401