Reputation: 3075
I've a fread line in my code that reads the contents of the file like this
fread(buffer,sizeof(char),1024,file1);
I kow that I can free the buffer using free(buffer);
but my questions is, is there a way to delete the elements in the buffer one by one?. Say, I used the first element and I no longer need it so I want to delete it.
Something which might do like this
for(i=0 ; i< 1024; i++){
*do something with buffer[1] *
free(buffer[1]); // I know this is wrong but something which can do the same.
}
Thanks, sunil
Upvotes: 0
Views: 1193
Reputation: 10794
A C++ stl way of doing this would be
#include <fstream>
#include <cassert>
#include <vector>
void foo () {
const char* myFilename = "some_file_path";
std::ifstream myStream (myFilename);
assert (myStream.is_open());
int n = 1024;
std::vector<char> buffer (n);
myStream.read (&buffer[0], n);
assert (myStream.good());
for (int i = 0; i < n; i++) {
char* myPtr = &buffer[i];
//do something with buffer[i]
}
}
Upvotes: 0
Reputation: 25537
You can free only what you allocated. What you allocated is the whole buffer and not the individual elements of the buffer.
From N1256 (I don't have the C99 standard with me)
$7.20.3.2/2- "The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or 261) realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.
If you are however looking at removing unnecessary elements read from the buffer, you will have to either do it yourself if you are using arrays, or you will have to settle in for STL containers (vector/list) etc (which provide interfaces for element management)
Upvotes: 1
Reputation: 59151
If you want to break up your allocation/file read into smaller values, you could do it. Like Steve said, though, you can only deallocate the exact same chunks of memory you allocate.
An example of changing your existing code:
const size_t size_of_full_buffer = 1024;
const size_t size_of_buffer_chunk = 128; /* evenly divisible */
size_t size_read = 0;
while(size_read <= size_of_full_buffer)
{
buffer = (char*) malloc(sizeof(char) * size_of_buffer_chunk)
fread(buffer, sizeof(char), size_of_buffer_chunk, file1);
for(i = 0; i < size_of_buffer_chunk; i++)
{
/* do something with buffer[1] */
}
free(buffer);
size_read += size_of_buffer_chunk;
}
However, you should use an input file stream instead of fread. If file streams require you to allocate a buffer (they might not) use a vector instead of malloc/free.
Upvotes: 1
Reputation: 106609
No, there is no way to do this. Just store the original pointer somewhere, and increment the pointer you're working with. When you're done, you can feel free to free
the original pointer.
That said, if you are using C++, you shouldn't be using malloc
or free
at all, nor should you be using fread
....
Upvotes: 1
Reputation: 54178
The buffer has to be freed the same way it as allocated, in one shot. You asked for a contiguous block of memory, which is returned to you prefaced by heap control info describing the buffer as such. If you were to try to free elements individually the heap manager would get confused and the process fault.
Upvotes: 1