Array filling problem

Hello everybody,

i wrote a program which find every pixel and check their around. I want to write every checked pixel to 2d array. For this reason i initialized struct and with help of struct 2d array. But when i want to fill 2d array with checked pixels after x=167, the program can not terminate its operation. In advance thank you very much!

typedef unsigned char buftype;
#define NBUF 4104
#define MAXCLU 10000
#define MAXPIX 6000

struct tpix {
unsigned char x,y;
unsigned short z;
};

tpix cluster[MAXCLU][MAXPIX];

void fillcluster(int x, int y) {
const int ncluster=34;
int np[ncluster];
int nn=np[ncluster];

cluster[ncluster][nn].x=x;
cluster[ncluster][nn].y=y;
cluster[ncluster][nn].z=frame[x][y];
printf("%d %d %d \n", cluster[ncluster][nn].x, cluster[ncluster][nn].y, cluster[ncluster][nn].z);
frame[x][y]=0;
np[ncluster]++;
}

void clust(int x, int y) {
int i,j,m;

for (i=-1;i<2;i++) {
for (j=-1;j<2;j++) {
if (frame[x+i][y+j]>0) {
fillcluster(x+i,y+j);
clust(x+i,y+j);
}

}
}

}

analyze_clusters() {

int i,j;
int k,l;

for (i=0;i<258;i++) {
for (j=0;j<258;j++) {
if (frame[i][j]>0) {
clust(i,j);
}
}
}
}
What are you trying to to with this np[ncluster] array?
An array is zero based. That means you can use idexes only form 0 to array size - 1.

in your program that'd be e.g. np[ncluster - 1]++;

But I don't see what ncluster / int np[ncluster]; is good for

Nevertheless np[ncluster]++; is out of bounds and will crash

This int nn=np[ncluster]; provides a more or less random (even negative) number and therefore may or may not crash
Hi coder777,

I have 2d array (frame[i][j]). I want to check around every pixel in frame[i][j] and i do this int the function clust(int x, int y). After this i want to fill 2d array (cluster[MAXCLU][MAXPIX]) with these checked pixels. But i do not know how i can fill cluster[MAXCLU][MAXPIX] exactly. Can you help me?Can you advice me other thing?
how's the data supposed to be organized? Has x/y in cluster the same place like in frame? It doesn't look like. How did you come to the arbitrary looking 34?
Hi coder777,

Thank you very much for you answer. I have solved my problem with you answer. Again thank you very much!
I am not able to understand this line ..
please clarify

An array is zero based. That means you can use idexes only form 0 to array size - 1.

and i don't understand what coder7777 is trying to say .. please explain.
as i do not see
ncluster / int np[ncluster];


thanks in advance .
1
2
3
4
5
6
7
#define MAXCLU 10000
#define MAXPIX 6000
struct tpix {
unsigned char x,y;
unsigned short z;
};
tpix cluster[MAXCLU][MAXPIX];
is equal to tpix cluster[10000][6000]; that's equal to tpix cluster[60000000];.
A tpix is
1
2
3
(2 * sizeof(char)) + sizeof(short) =
(2 * 1) + 2 =
4 bytes
so
tpix cluster[60000000]; wants 60000000 * 4 bytes.
That's too much.

Also i don't see entrypoints nor code tags.
Last edited on
@bluecoder
sorry, but I don't know what your problem is. You know that indexing of an array is zero based, don't you?

@EssGeEich
That's too much.
No, it's about 60MB and todays memory is supposed to hold that. It's gobal memory, not on the stack
I think it's too much to be a one-byte-behind-another memory type. And it's not 60M, it's 60 * 4M (240M). Anyways it sounds exagerate, hope that program will work anyways without having to edit this size.
Topic archived. No new replies allowed.