Wo liegt der Fehler?

Moin,

also,, ich weiß nicht wo der fehler liegt..

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define COLOR_MAX 256

int getColorR(int x, int y);
int getColorG(int x, int y);
int getColorB(int x, int y);
void countColors(int dimX, int dimY);

int main()
{
	srand(time(NULL));
	countColors(800,600);
	
	return 0;
}

int getColorR(int x, int y)
{
	return ((int) (COLOR_MAX*rand()/(RAND_MAX+1.0)));
	
}

int getColorG(int x, int y)
{
	return ((int) (COLOR_MAX*rand()/(RAND_MAX+1.0)));
}

int getColorB(int x, int y)
{
	return ((int) (COLOR_MAX*rand()/(RAND_MAX+1.0)));
}

void countColors(int dimX, int dimY)
{
	int i;
	int j;
	// Farbwürfel anlegen
	
	int array[256][256][256];
	
	for(i=0;i<dimY;i++)
		for(j=0;j<dimY;j++)
			array[getColorR(i,j)][getColorG(i,j)][getColorB(i,j)]++;
	}
 
Original von M4CH!N3
was ist denn der fehler?
Mit der Syntax ist alles in Ordnung, aber beim Ausführen schmiert das Programm irgendwo in der countColors() Funktion ab. Ich vermute es wird in das Array außerhalb seines Gültigkeitsbereiches geschrieben. So ganz versteh ich den Sinn dieser Funktion nicht, vielleicht könnte ein Dynamisches Array helfen?

Edit: Allem Anschein nach handelt es sich um einen Stack Overflow.
Das Array ist mit seinen 256^3 Integerwerten einfach zu groß für eine Funktion...
Bei nur 5^3 Bereichen gibt es keinen Absturz.
Siehe auch
 
Aus dem Wikipedia Artikel:
The other major cause of a stack overflow results from an attempt to allocate more memory on the stack than will fit. This is usually the result of creating local array variables that are far too large. For this reason arrays larger than a few kilobytes should be allocated dynamically instead of as a local variable.

Also warum definierst du den Array nicht einfach vor der Laufzeit global?
Code:
...
int getColorR(int x, int y);
int getColorG(int x, int y);
int getColorB(int x, int y);
void countColors(int dimX, int dimY);
int array[256][256][256];

int main()
{
	srand(time(NULL));
	countColors(800,600);
	
	return 0;
}
...
Wenn du dann countColors() mehrmals durchlaufen lassen willst, setz den Array wieder auf 0.
 
Zurück
Oben