Re: memory leaks
"James Kanze" <james.kanze@gmail.com> ha scritto nel messaggio
news:4d4a6dae-1a50-4e01-9fbe-4bb005e180bd@f12g2000yqn.googlegroups.com...
Also, vector< unsigned char > will normally do bounds checking,
and other important things to avoid undefined behavior.
this seems untrue for std::vector<T> data_;
for T == struct Color3d{double r, g, b;};
#include <iostream>
#include <cstdlib>
#include <stdint.h>
#include <vector>
#define u8 uint8_t
#define i8 int8_t
#define u32 uint32_t
#define i32 int32_t
// float 32 bits
#define f32 float
#define S sizeof
#define R return
#define P printf
#define F for
using namespace std;
template <typename T>
class Image
{
class Indexer
{
public:
Indexer(T* data) : data_(data)
{
}
T& operator[](int x) const
{
return data_[x];
}
private:
T* data_;
};
class ConstIndexer
{
public:
ConstIndexer(const T* data) : data_(data)
{
}
T operator[](int x) const
{
return data_[x];
}
private:
const T* data_;
};
public:
Image(int width, int height) :
width_(width),
height_(height),
data_(width*height)
{
}
int width() const
{
return width_;
}
int height() const
{
return height_;
}
Indexer operator[](int y)
{
return Indexer(&data_[y*width_]);
}
ConstIndexer operator[](int y) const
{
return ConstIndexer(&data_[y*width_]);
}
private:
int width_;
int height_;
std::vector<T> data_;
};
struct Color3d
{
double r, g, b;
};
void fill(Image<Color3d>& img, Color3d color)
{
for (int y = 0; y < img.height(); ++y)
for (int x = 0; x < img.width(); ++x)
img[y][x] = color;
}
int main(void)
{Color3d h={0.78373, 0.1383, 1-0.78373-0.1383};
Image<Color3d> g(768, 1024);
// cout << "a=" << Image::a << "\n" ;
cout << "Inizio\n";
fill(g, h);
cout << "(r,g,b)==(" << g[0][0].r << ", "
<< g[0][0].g << ", "
<< g[768][1024].b << ")\n";
cout << "end\n";
return 0;
}
-------
result
Inizio
(r,g,b)==(0.78373, 0.1383, 0.07797)
end