The program creates a 2D array with malloc
and func
fills it. fprintf
writes in a file. That's all folks.
But I am getting unexpected program exit form the program if I use big integers for height
and width
.
(30,60): OK
(60,80): not OK
(60,65): OK
(17,41): OK
(200,200): not OK
Does anybody have a clue?
int main() {
unsigned char **buf = (unsigned char **)malloc(height * sizeof(unsigned char*));
for (int i = 0; i < height; ++i)
buf[i] = (unsigned char *)malloc(width * sizeof(unsigned char));
func(buf);
FILE * f = fopen("foo.txt", "w+");
for(int i= 0;i<height;++i)
fprintf(f, "%s%c", buf[i], '\n');
fclose(f);
for (int i = 0; i < height; ++i)
free(buf[i]);
free(buf);
}
void func(unsigned char** buf) {
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j)
buf[i][j] = 48 + (i/10)%10;
buf[i][width] = '\0';
}
}