Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Multidimensional array member initialization

1 view
Skip to first unread message

olivier....@algosyn.com

unread,
Jan 7, 2010, 4:40:38 AM1/7/10
to
Hello,

I would like to create an Image class. A use of this class could be
something as:

Image image1(1000, 1000);
Image image2(2000, 1000);

Color3d color;

image1.fill(color);
image2.fill(color);

Here is the code of the class:

struct Color3d
{
double r,g,b;
}

class Image
{
public:
Image(int width, int height);
~Image();

void fill(Color3d color);

private:
int _width;
int _height;

Color3d _pixels[][]; // Does not work !
};

Image::Image(int width, int height) : _width(width), _height(height)
{
_pixels = new Color3d[width][height]; // Does not work !
}

Image::~Image()
{
delete [][] _pixels; // Does not work
}

void Image::fill(Color3d color)
{
for (int y = 0; y < _height; y++)
{
for (int x = 0; x < _width; x++)
{
_pixels[x][y] = color;
}
}
}

As you can imagine, I have some problems around the pixels definition.

I see two alternatives:
- using templates;
- allocate a single dimension array of pixels and manage the
coordinates conversion myself.

I am sure there are some more elegant solutions, but I have no ideas.

Thanks to help me,

Olivier

Fred Zwarts

unread,
Jan 7, 2010, 4:56:40 AM1/7/10
to
olivier....@algosyn.com <olivier....@algosyn.com> typed
(in c5deac91-7a8b-48f0...@j24g2000yqa.googlegroups.com)

The size of a class is fixed. You can't use arrays with undefined size in a class.
With one-dimensional arrays you will have the same problem.
Use std::vector, which can be used as an array, but the size of which can be adjusted
in the constructor of your class.

Gert-Jan de Vos

unread,
Jan 7, 2010, 6:33:00 AM1/7/10
to
On Jan 7, 10:40 am, "olivier.scalb...@algosyn.com"

<olivier.scalb...@algosyn.com> wrote:
> Hello,
>
> I would like to create an Image class. A use of this class could be
> something as:
>
>     Image image1(1000, 1000);
>     Image image2(2000, 1000);
>
>     Color3d color;
>
>     image1.fill(color);
>     image2.fill(color);
>
> Here is the code of the class:
>
> struct Color3d
> {
>     double r,g,b;
>
> }
>
> class Image
> {
> public:
>     Image(int width, int height);
>     ~Image();
>
>     void fill(Color3d color);
>
> private:
>     int _width;
>     int _height;
>
>     Color3d _pixels[][]; // Does not work !
>
> };
>
> As you can imagine, I have some problems around the pixels definition.
>
> I see two alternatives:
> - using templates;
> - allocate a single dimension array of pixels and manage the
> coordinates conversion myself.
>
> I am sure there are some more elegant solutions, but I have no ideas.

Use a vector<Color3d> for your pixels. Index as (width*y + x). You can
use operator[] that returns a proxy class that represents the left
most index. The proxy class then implements another operator[] to
support image[y][x] style pixel access.

You may want to make your Image class a template class to support
different pixel types.

io_x

unread,
Jan 7, 2010, 3:12:49 PM1/7/10
to

<olivier....@algosyn.com> ha scritto nel messaggio
news:c5deac91-7a8b-48f0...@j24g2000yqa.googlegroups.com...
> Hello,

>
> As you can imagine, I have some problems around the pixels definition.
>
> I see two alternatives:
> - using templates;
> - allocate a single dimension array of pixels and manage the
> coordinates conversion myself.
i don't know
here is my solution (don't know if it is right)
------------------------
#include <iostream>
#include <stdlib.h>

using namespace std;

struct Color3d{
float r, g, b;
};

class Image{
public:
Image(int width, int height);
~Image();

void fill(Color3d& color);

int width;
int height;
Color3d** pxs;
};

Image::Image(int w, int h)
{int i, g, gg;

width=0; height=0; pxs=0;
if(w<=0||h<=0) return;
g =h*sizeof(Color3d*);
if(g<=0) return;
gg=w*sizeof(Color3d); // pixel are h*w
if(gg<=0) return;
pxs=(Color3d**) malloc(g);
if(pxs==0) return;
for(i=0; i<h; ++i)
{pxs[i]=(Color3d*) malloc(gg);
if(pxs[i]==0)
{for(--i; i>=0; --i)
free(pxs[i]);
free(pxs); pxs=0;
return;
}
}
width=w; height=h;
}

Image::~Image()
{int i;
if(pxs==0) return;
for(i=0; i<height; ++i)
free(pxs[i]);
free(pxs);
}

void Image::fill(Color3d& color)
{int x, y;
if(pxs==0) return;
for(x=0; x<height; ++x)
{for(y=0; y<width; ++y)
pxs[x][y]=color;
}
}

int main(void)
{Color3d h={0.78373, 0.1383, 1-0.78373-0.1383};
Image g(1024, 768);

if(g.pxs==0)
{cout << "Error\n"; return 0;}
g.fill(h);
cout << "(r,g,b)==(" << g.pxs[0][0].r << ", "
<< g.pxs[0][0].g << ", "
<< g.pxs[0][0].b << ")\n";
return 0;

io_x

unread,
Jan 7, 2010, 3:12:54 PM1/7/10
to

<olivier....@algosyn.com> ha scritto nel messaggio
news:c5deac91-7a8b-48f0...@j24g2000yqa.googlegroups.com...
> Hello,
>
> I would like to create an Image class. A use of this class could be
> something as:
>
> Image image1(1000, 1000);
> Image image2(2000, 1000);
>
> Color3d color;
>
> image1.fill(color);
> image2.fill(color);
>
> Here is the code of the class:
>
> struct Color3d
> {
> double r,g,b;
> }

this can not compile ";"

Gert-Jan de Vos

unread,
Jan 7, 2010, 4:19:05 PM1/7/10
to

No need to handle memory management yourself. vector is very good at
it.
Consider making the functions on Image free functions. That way, you
can
add functions later without changing the basic Image type.

Here's an example of how to use vector in combination with proxy
objects
to support array style [][] indexing.

Hope this helps.

Gert-Jan

#include <vector>

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;
}

io_x

unread,
Jan 8, 2010, 4:31:30 AM1/8/10
to

"io_x" <a...@b.c.invalid> ha scritto nel messaggio
news:4b463e69$0$1105$4faf...@reader4.news.tin.it...

>
> here is my solution (don't know if it is right)

this is better because all immage is one array without holes

#include <iostream>
#include <stdlib.h>
#define S sizeof
#define R return

using namespace std;

struct Color3d{
float r, g, b;
};

class Image{
public:
int height;
int width;
char* pxs;

Image(int height, int width);
~Image();
Color3d& v(int h, int w);
void fill(Color3d& color);
};

Color3d& Image::v(int h, int w)
{R (Color3d&) *(pxs+w*S(Color3d)+h*width*S(Color3d));}

Image::Image(int h, int w)
{int i, g;

height=0; width=0; pxs=0;


if(w<=0||h<=0) return;

g =h*sizeof(Color3d);
if(g<=0) return;
g*=w;
if(g<=0) return;
pxs=(char*) malloc(g);
if(pxs==0) return;
width=w; height=h;
}

Image::~Image(){free(pxs);}

void Image::fill(Color3d& color)
{int x, y;
if(pxs==0) return;
for(x=0; x<height; ++x)
{for(y=0; y<width; ++y)

(*this).v(x,y)=color;
}
}

int main(void)
{Color3d h={0.78373, 0.1383, 1-0.78373-0.1383};

Image g(768, 1024);
cout << "Inizio\n";


if(g.pxs==0)
{cout << "Error\n"; return 0;}
g.fill(h);

cout << "(r,g,b)==(" << g.v(0,0).r << ", "
<< g.v(0,0).g << ", "
<< g.v(0,0).b << ")\n";
cout << "end\n";
return 0;
}
----------
why is not possible to define:

Color3d& Image::operator[][](int i, int j)
{return (Color3d&) *(pxs+j*sizeof(Color3d)+i*sizeof(Color3d)*width);}

io_x

unread,
Jan 9, 2010, 4:00:51 AM1/9/10
to

"io_x" <a...@b.c.invalid> ha scritto nel messaggio
news:4b46f995$0$1116$4faf...@reader2.news.tin.it...

>
> "io_x" <a...@b.c.invalid> ha scritto nel messaggio
> news:4b463e69$0$1105$4faf...@reader4.news.tin.it...
>>
>> here is my solution (don't know if it is right)
#include <iostream>
#include <cstdlib>
#include <stdint.h>

#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;

// one point is 3 32 bits float
struct Color3d{
f32 r, g, b;
};

class Image{
public:
u32 height;
u32 width;
u8* pxs;
u32 wmul;

Image(i32 height, i32 width);


~Image();
Color3d& v(int h, int w);

int fill (Color3d& color);
int fill1(Color3d& color);
};

Color3d& Image::v(i32 h, i32 w)
{return (Color3d&) *(pxs+w*S(Color3d)+h*wmul);}

// pixel are square coord: from 0..h-1, 0..w-1
Image::Image(i32 h, i32 w)
{i32 i, g;

height=0; width=0; pxs=0; wmul=0;


if(w<=0||h<=0) return;
g =h*sizeof(Color3d);
if(g<=0) return;
g*=w;
if(g<=0) return;
pxs=(char*) malloc(g);
if(pxs==0) return;

height=h; width=w;
wmul=width*S(Color3d);
}

Image::~Image(){free(pxs);}

i32 Image::fill(Color3d& color)
{u32 x, y;
if(pxs==0) R 0;
F(x=0; x<height; ++x)
{F(y=0; y<width; ++y)
(*this).v(x,y)=color;
}
R 1;
}

i32 Image::fill1(Color3d& color)
{u32 x, end;
Color3d *p;
if(pxs==0) R 0;
p=(Color3d*) pxs;
end=height*width;
F(x=0; x<end; ++x, ++p)
*p=color;
R 1;
}

int main(void)
{Color3d h={0.78373, 0.1383, 1-0.78373-0.1383};
Image g(768, 1024);

cout << "Inizio\n";
if(g.pxs==0)
{cout << "Error\n"; R 0;}
g.fill1(h);


cout << "(r,g,b)==(" << g.v(0,0).r << ", "
<< g.v(0,0).g << ", "

<< g.v(767,1023).b << ")\n";
cout << "end\n";
R 0;
}


olivier....@algosyn.com

unread,
Jan 9, 2010, 7:13:32 AM1/9/10
to
On Jan 7, 10:19 pm, Gert-Jan de Vos <gert-

Thanks. Your solution is very elegant and also very efficient.

Olivier

olivier....@algosyn.com

unread,
Jan 9, 2010, 7:17:13 AM1/9/10
to
Thanks to all of you !

Olivier

olivier....@algosyn.com

unread,
Jan 9, 2010, 7:17:21 AM1/9/10
to

olivier....@algosyn.com

unread,
Jan 9, 2010, 7:17:35 AM1/9/10
to

io_x

unread,
Jan 9, 2010, 4:28:19 PM1/9/10
to
----------
"Gert-Jan de Vos" <gert-ja...@onsneteindhoven.nl> ha scritto nel messaggio
news:4a41375a-4445-4ffb...@j5g2000yqm.googlegroups.com...

Hope this helps.

Gert-Jan

#include <vector>
etc
---------

i have copy above code adding only some macro,
add 3 "_" in the constructor Image(int, int),
and the main() function for debug.

not understand why img.width()
is called each cicle of the inner loop; why not use
just x<img.width_?

yes i not have understood all but seems to me there
is something wrong g[768][1024].b
not seg fault, so for me could be something wrong.

than for me the centre of the exercise is:
immage has to be one big array without holes in it.
Can some of you could say "vector" operate in one big array
without holes in it?
-----------------------


#include <iostream>
#include <cstdlib>
#include <stdint.h>

#include <vector>

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 << "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;
}
---------
this below is the rewrite of above in one readable form;
with the same error, in one inesistent position of the array
the [768][124] say the same value of all other (1-0.78373-0.1383)
and not seg fault.

instead my example seg fault if [768][1024]; it
return 0 [and not (1-0.78373-0.1383)] for [767][1024]
better!
---------


#include <iostream>
#include <cstdlib>
#include <stdint.h>

#include <vector>

using namespace std;

template <typename T> class Image{

public:
int width_;
int height_;
std::vector<T> data;

class Indexer{
public:
T* data;
Indexer(T* dat) : data(dat){}
T& operator[](int x){return data[x];}
};

Image(int w, int h): width_(w), height_(h), data(w*h){}

int width() {return width_;}
int height() {return height_;}

// img[y] is Indexer
// where is the memory of Indexr? It is created and destroy?
// img[y][x] is T&
Indexer operator[](int y)
{return Indexer( & data[y*width_] );}
};

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 << "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;
}


io_x

unread,
Jan 10, 2010, 5:09:35 AM1/10/10
to

"io_x" <a...@b.c.invalid> ha scritto nel messaggio
news:4b48f314$0$1110$4faf...@reader2.news.tin.it...

What about this?
Is not this better than using "vector"?


-------
#include <iostream>
#include <cstdlib>
#include <stdint.h>

#define u8 uint8_t


#define i8 int8_t
#define u32 uint32_t
#define i32 int32_t
// float 32 bits
#define f32 float

#define f64 double

#define S sizeof
#define R return
#define P printf
#define F for

using namespace std;

template <typename T> class Image{
public:

u32 height;
u32 width;
T* pxs;

Image(i32 h, i32 w)
{i32 i, g;

height=0; width=0; pxs=0;


if(w<=0||h<=0) return;

g =h*S(T);


if(g<=0) return;
g*=w;
if(g<=0) return;

cout << "Size=" << g << "\n";
pxs=(T*) malloc(g);
if(pxs==0) return;
height=h; width=w;
}

~Image(){free(pxs);}

T& v(int h, int w)
{return *(pxs+w+h*width);}

int fill (T& color)


{u32 x, y;
if(pxs==0) R 0;

for(x=0; x<height; ++x)
{for(y=0; y<width; ++y)
(*this).v(x,y)=color;
}
R 1;
}

int fill1(T& color)
{u32 x, end;
T *p;
if(pxs==0) R 0;
p=pxs;
end=height*width;
for(x=0; x<end; ++x, ++p)
*p=color;
R 1;
}

class Indexer{


public:
T* data;
Indexer(T* dat) : data(dat){}
T& operator[](int x){return data[x];}
};

// img[y] is Indexer
// img[y][x] is *&T
Indexer operator[](int y)
{return Indexer(pxs+y*width);}

};

struct Color3d{f32 r, g, b;};

struct Color3d1{f64 r, g, b;};

int main(void)
{Color3d1 h={0.78373, 0.1383, 1-0.78373-0.1383};
Image<Color3d1> g(768, 1024);

cout << "Inizio\n";
if(g.pxs==0)
{cout << "Error\n"; return 0;}

g.fill1(h);
cout << "(r,g,b)==(" << g.v(0,0).r << ", "
<< g.v(0,0).g << ", "

<< g.v(767,1024).b << ")\n";
cout << "g[767][1024].b==" << g[767][1024].b << "\n";
cout << "g[767][1023].b==" << g[767][1023].b << "\n";


cout << "end\n";
R 0;
}

--------
Size=18874368
Inizio
(r,g,b)==(0.78373, 0.1383, 0)
g[767][1024].b==0
g[767][1023].b==0.07797
end


io_x

unread,
Jan 10, 2010, 5:12:50 AM1/10/10
to

"io_x" <a...@b.c.invalid> ha scritto nel messaggio
news:4b49a5c2$0$824$4faf...@reader5.news.tin.it...

Gert-Jan de Vos

unread,
Jan 10, 2010, 2:41:10 PM1/10/10
to
On Jan 9, 10:28 pm, "io_x" <a...@b.c.invalid> wrote:
> ----------
> "Gert-Jan de Vos" <gert-jan.de....@onsneteindhoven.nl> ha scritto nel messaggionews:4a41375a-4445-4ffb...@j5g2000yqm.googlegroups.com...

>
> Hope this helps.
>
> Gert-Jan
>
> #include <vector>
> etc
> ---------
>
> i have copy above code adding only some macro,
> add 3 "_" in the constructor Image(int, int),
> and the main() function for debug.
>
> not understand why img.width()
> is called each cicle of the inner loop; why not use
> just x<img.width_?

width_ and height_ are private functions such that
these values can only be set when constructing an
Image. When these values where public, the values
could be changed, and the data content would be
meaningless or even invalid. In case you are worried
about possible function call overhead: width()/
height() are inline functions and all sensible
compilers will generate exactly the same code as
with the public members.

> yes i not have understood all but seems to me there
> is something wrong g[768][1024].b
> not seg fault, so for me could be something wrong.

Your image was created as 768x1024 in size. Therefore
your index is out of range. This is translated into a
vector out of range index. This results in good C and
C++ tradition in Undefined Behavior: anything can
happen, including seg fault.

> than for me the centre of the exercise is:
> immage has to be one big array without holes in it.
> Can some of you could say "vector" operate in one big array
> without holes in it?

Sorry, I do not understand your question.

io_x

unread,
Jan 11, 2010, 1:58:50 AM1/11/10
to

"Gert-Jan de Vos" <gert-ja...@onsneteindhoven.nl> ha scritto nel messaggio
news:4a41375a-4445-4ffb...@j5g2000yqm.googlegroups.com...

On Jan 7, 10:40 am, "olivier.scalb...@algosyn.com"
<olivier.scalb...@algosyn.com> wrote:
> Hello,
>
> I would like to create an Image class. A use of this class could be
> something as:

What about this?

---------------
; nasmw -fobj this.asm
; bcc32 -v that.cpp this.obj
section _DATA use32 public class=DATA

global @Image@fill2$qr7Color3d

section _TEXT use32 public class=CODE

;
; 0j, 4i, 8ra, 12&This_i, 16&Color3d_j
align 4
@Image@fill2$qr7Color3d:
push esi
push edi
mov esi, dword[esp+ 12]
cmp esi, 0
jne .1
.e: mov eax, 0
stc
jmp short .z
.1: mov eax, [esi]
mov ecx, [esi+4] ;eax=h, ecx=w
mul ecx
cmp edx, 0
jne .e
cmp eax, 0
jl .e
mov ecx, eax ;ecx=h*w
mov edi, [esi+8]
cmp edi, 0
je .e ;edi=pxs
mov esi, dword[esp+ 16]
mov eax, [esi]
mov edx, [esi+4]
mov esi, [esi+8]
.2: mov [edi], eax
mov [edi+4], edx
mov [edi+8], esi
add edi, 12
loop .2
mov eax, 1
clc
.z:
pop edi
pop esi
ret
----------------------


#include <iostream>
#include <cstdlib>
#include <stdint.h>

#define u8 uint8_t
#define i8 int8_t
#define u32 uint32_t
#define i32 int32_t
// float 32 bits
#define f32 float
#define f64 double

#define S sizeof
#define R return
#define P printf
#define F for

using namespace std;

struct Color3d{f32 r, g, b;};

class Image{


public:
u32 height;
u32 width;

Color3d* pxs;

Image(i32 h, i32 w)
{i32 i, g;

height=0; width=0; pxs=0;
if(w<=0||h<=0) return;

g =h*S(Color3d);


if(g<=0) return;
g*=w;
if(g<=0) return;
cout << "Size=" << g << "\n";

pxs=(Color3d*) malloc(g);
if(pxs==0) return;
height=h; width=w;
}

~Image(){free(pxs);}

Color3d& v(int h, int w)
{return *(pxs+w+h*width);}

int fill (Color3d& color)


{u32 x, y;
if(pxs==0) R 0;
for(x=0; x<height; ++x)
{for(y=0; y<width; ++y)
(*this).v(x,y)=color;
}
R 1;
}

int fill1(Color3d& color)
{u32 x, end;
Color3d *p;


if(pxs==0) R 0;
p=pxs;
end=height*width;
for(x=0; x<end; ++x, ++p)
*p=color;
R 1;
}

int fill2(Color3d& color);

class Indexer{
public:
Color3d* data;
Indexer(Color3d* dat) : data(dat){}
Color3d& operator[](int x){return data[x];}
};

// img[y] is Indexer

// img[y][x] is *&Color3d
Indexer operator[](int y)
{return Indexer(pxs+y*width);}

};


int main(void)
{Color3d h={0.78373, 0.1383, 1-0.78373-0.1383};
Image g(768, 1024);

cout << "Inizio\n";
if(g.pxs==0)
{cout << "Error\n"; return 0;}

g.fill2(h);

io_x

unread,
Jan 11, 2010, 2:26:19 AM1/11/10
to

"Gert-Jan de Vos" <gert-ja...@onsneteindhoven.nl> ha scritto nel messaggio
news:c2e8f0d9-e97d-4226...@m25g2000yqc.googlegroups.com...

On Jan 9, 10:28 pm, "io_x" <a...@b.c.invalid> wrote:
> ----------
> "Gert-Jan de Vos" <gert-jan.de....@onsneteindhoven.nl> ha scritto nel
> messaggionews:4a41375a-4445-4ffb...@j5g2000yqm.googlegroups.com...
>
> Hope this helps.
>
> Gert-Jan
>
> #include <vector>
> etc
> ---------
>
> i have copy above code adding only some macro,
> add 3 "_" in the constructor Image(int, int),
> and the main() function for debug.
>
> not understand why img.width()
> is called each cicle of the inner loop; why not use
> just x<img.width_?

width_ and height_ are private functions such that
these values can only be set when constructing an
Image.


When these values where public, the values
could be changed, and the data content would be
meaningless or even invalid.

#"could be changed" from who? the programmer
#that i hope know what he is doing: all must be public

In case you are worried
about possible function call overhead: width()/
height() are inline functions and all sensible
compilers will generate exactly the same code as
with the public members.

> yes i not have understood all but seems to me there
> is something wrong g[768][1024].b
> not seg fault, so for me could be something wrong.

Your image was created as 768x1024 in size. Therefore
your index is out of range. This is translated into a
vector out of range index. This results in good C and
C++ tradition in Undefined Behavior: anything can
happen, including seg fault.

#but seems to me that g[768][1024].b [out of index]
#with vector here
#with my sys return 0.07797 exat how all other;
#
#this is the UB worst that can happen
#because if someone use g[768][1024].b
#could not easy understand that value is plain wrong
#instead if g[768][1024].b seg fault
#or g[768][1024].b==0 someone could understand something
#goes wrong

> than for me the centre of the exercise is:
> immage has to be one big array without holes in it.
> Can some of you could say "vector" operate in one big array
> without holes in it?

Sorry, I do not understand your question.

#it seems cpp library vector has no hole in its data representation


Gert-Jan de Vos

unread,
Jan 11, 2010, 8:33:32 AM1/11/10
to
On Jan 11, 8:26 am, "io_x" <a...@b.c.invalid> wrote:

> #"could be changed" from who? the programmer
> #that i hope know what he is doing: all must be public

Making member data private is a well established OO principle.
This protects an object from unintended state changes. The
object itself takes care of being in a consistent state
always.

> #it seems cpp library vector has no hole in its data representation

C++03 specifies that vector stores its elements in a single
contiguous block of memory. My Image implementation indeed makes
use of this property of vector. The same will not work with deque
or list.

0 new messages