I write a simple Buffer class which holds a buffer and provides a function to reverse the content of the buffer.
Buffer.h
#ifndef __BUFFER_H__
#define __BUFFER_H__
#include <stdlib.h>
#include <cerrno>
#include <stdio.h>
class Buffer
{
private:
char * buffer;
int size;
public:
Buffer(int size);
~Buffer();
void reverse(int size);
};
#endif
Buffer.cc
#include "Buffer.h"
Buffer::Buffer(int size)
{
this -> size = size;
this -> buffer = (char *)malloc(size);
if(this -> buffer == NULL)
throw 1;
}
Buffer::~Buffer()
{
if(this -> buffer != NULL)
free(this -> buffer);
}
void Buffer::reverse(int size)
{
char tmp;
int i;
char * tmpb = this -> buffer;
for(i = 0; i < size / 2; i++)
{
tmp = (char)tmpb[i];
tmpb[i] = tmpb[size - i - 1];
// printf("exchange %x with %x\n", tmp & 0xff, tmpb[i] & 0xff);
tmpb[size - i - 1] = tmp;
}
}
There is a remote server to test my implementation using fault injection. That server gives me report that there is bug caused by double free or corruption. I have read my implementation many times, but no luck to find the bug. I dont have access to that server. Any help?
NOTE: I have to use C style code. Otherwise I would fail the server test. That is a hard requirement. Well, you may think this requirement is silly. But this is the requirement. Maybe one point is learning the bad while someone mixing C and C++.
The server provides a main function to test my implementation.
For anyone who wants to have a look at all the code, you could download a zip file from https://mega.nz/#!FhoHQD5Y!iD9tIZMNtKPpxfZTpL2KWoUJRedbw6wToh6QfVvzOjU. Just compile using make
. The result is a program named rcopy
which reverses content of a file byte by byte and then outputs to a new file.