Hi, I am having trouble figuring out the reason why my .c code is having trouble allocating ~250K of memory. Here is the allocation code:
struct IMAGE {
int width, height, maxval;
char **data;
};
void raiseError(char *msg)
{
printf("%s", msg);
getch();
exit(1);
}
//...
IMAGE readPGM()
{
IMAGE image;
image.data = (char **) malloc(sizeof(char)*image.height);
//..
for (i=0; i<image.height; i++) {
image.data[i] = (char *) malloc(sizeof(char)*image.width);
if (image.data[i]=='\0') {
printf("%d\n", i);
raiseError("Not enough memory!..");
}
}
//..
}
//..
The program exits when i=116. image.width and image.height equals to 500 here, so I want 500x500=250000 bytes to be allocated here. But 116x500 = 58000 bytes are being allocated at maximum. So, is there something that limits it? Is there something wrong with my code? I am posting the full source below, just in case if it is necesarry. The idea is to read a PGM file into the structure IMAGE, process it and rewrite it in another file. As you can tell, it is not complete yet because I couldn't figure out a way to allocate more memory.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<alloc.h>
struct IMAGE {
int width, height, maxval;
char **data;
};
void raiseError(char *msg)
{
printf("%s", msg);
getch();
exit(1);
}
char *toString(int num)
{
char sign = 0;
if (num<0) {
sign = -1;
num*=-1;
}
int numLen = 1;
if (sign<0) {
numLen++;
}
int tmpNum = num;
while (tmpNum>9) {
tmpNum /= 10;
numLen++;
}
char *result = (char *)malloc(sizeof(char)*(numLen+1));
result[numLen] = '\0';
char ch;
while (num>9) {
ch = (num%10)+'0';
num /= 10;
result[numLen-1] = ch;
numLen--;
}
result[numLen-1] = num + '0';
if (sign<0)
result[0] = '-';
return result;
}
int toInteger(char *line)
{
int i=strlen(line)-1;
int factor = 1;
int result = 0;
while (i>=0) {
result += factor*(line[i]-'0');
factor *= 10;
i--;
}
return result;
}
char *getNewParam(FILE *fp)
{
char ch = 'X';
char *newParam;
newParam = (char*) malloc(1);
newParam[0] = '\0';
int paramSize = 0;
while (!isspace(ch)) {
ch = fgetc(fp);
if (!isspace(ch)) {
if (ch=='#') {
while (fgetc(fp)!='\n');
continue;
}
paramSize++;
newParam = (char *) realloc(newParam, paramSize+1);
newParam[paramSize-1] = ch;
}
}
newParam[paramSize] = '\0';
return newParam;
}
IMAGE readPGM()
{
FILE *fp;
IMAGE image;
//Open the file.
fp = fopen("seeds2.pgm","r+b");
if (fp=='\0')
raiseError("File could not be opened!..");
//Check if it is a raw PGM(P5)
char *line;
line = getNewParam(fp);
if (strcmp(line, "P5")!=0)
raiseError("File is not a valid raw PGM(P5)");
int paramCount = 0;
int *pgmParams;
pgmParams = (int *)malloc(sizeof(int)*3);
while (paramCount<3) {
line = getNewParam(fp);
pgmParams[paramCount++] = toInteger(line);
}
int pixelSize;
if (pgmParams[2]>255)
pixelSize = 2;
else
pixelSize = 1;
image.width =pgmParams[0];
image.height =pgmParams[1];
image.maxval =pgmParams[2];
free(pgmParams);
image.data = (char **) malloc(sizeof(char)*image.height);
int i,j;
long sum = 0;
for (i=0; i<image.height; i++) {
image.data[i] = (char *) malloc(sizeof(char)*image.width);
sum += sizeof(char)*image.width;
if (image.data[i]=='\0') {
printf("%d\n", i);
raiseError("Not enough memory!..");
}
}
for (i=0; i<image.height; i++) {
for (j=0; j<image.width; j++) {
fread(&image.data[i][j], sizeof(char), image.width, fp);
}
}
fclose(fp);
return image;
}
void savePGM(IMAGE image)
{
FILE *fp = fopen("yeni.pgm", "w+b");
fprintf(fp, "P5\n%s\n%s\n%s\n",
toString(image.width), toString(image.height), toString(image.maxval));
int i,j;
for (i=0; i<image.height; i++) {
for (j=0; j<image.width; j++) {
fwrite(&image.data[i][j], sizeof(char), 1, fp);
}
}
fclose(fp);
}
int main()
{
clrscr();
IMAGE image = readPGM();
//process
savePGM(image);
getch();
return 0;
}