Re: Heap corruption
Fran?ois wrote:
When using the following code, i am getting a heap corruption at the
delete step. After tracking the error, it seems that it comes from the
memmove operation. Can someone point me out the wrong thing when calling
memmove?
[...]
float** lineArray;
How about std::vector<float> or, if you really need two dimensions, Boost's
multidimensional array? Also, consider
lineArray = new float*[2*kernel_size+1];
Allocate storage for pointers.
initBuffer(lineArray,2*kernel_size+1,aInNrOfPixelsPerLine);
Allocate storage for each pointer?
for (int i = 1;i < aValue ;++ i){
memmove( lineArray[0], lineArray[1],
aOutNrOfPixelsPerLine*sizeof(float)*2*kernel_size);
}
1. Why do you start with 'i=1'?
2. Why do you not use 'i' in the loop?
3. The size is wrong. You might consider storing the '2*kernel_size+1' in a
constant:
size_t const num_lines = 2*kernel_size+1;
The memmove would then read
memmove( line[0], line[1], num_lines*pixel_per_line*sizeof(float));
which makes it more obvious that it makes no sense!
Note: if you wanted to move several lines at once (and therefore omitted
the '+1'), you need to store the whole bitmap in one contiguous block of
memory. If you need to access the lines separately, rather compute the
position in the array like
float* line = array + line_index*pixel_per_line;
Alternatively, you could move the pointers around instead of copying the
memory. In any case, consider putting the whole thing into a class so this
is properly encapsulated and you can swap the implementation without
affecting other code.
void initBuffer(float **theBuf,int tabsize,int ppl)
[...]
Note: 'size_t' would be more fitting than 'int' for 'tabsize' and 'ppl'.
Uli