I want to read a file of size 478200 bytes into memory as an array, but I've been getting problems when I run the compiled program. General Protection Faults. I've found that it's not to do with loading the file, but I'm not quite sure what it IS to do with. Here's an example that will cause the same error -
#include <stdio.h>
int main()
{
long n;
int *pTerrain;
pTerrain = (int*)malloc(478200);
if (!pTerrain)
{
printf("Unable to allocate memory.\n"
return 1;
}
for (n = 0; n < 478200; n++)
{
printf("n = %ld\n", n);
pTerrain[n] = 0;
}
return 0;
}
Each time I run this, it drops out at a slightly different value of n (usually around 146500, but not always so high).
Can anyone tell me if I'm making a foolish mistake, because I just can't come up with any ideas. I am a beginner, though, so you'll have to bear with me if you will. Thanks in advance for any help anyone can give me.
Cheers,
Dan Roberts.
#include <stdio.h>
int main()
{
long n;
int *pTerrain;
pTerrain = (int*)malloc(478200);
if (!pTerrain)
{
printf("Unable to allocate memory.\n"
return 1;
}
for (n = 0; n < 478200; n++)
{
printf("n = %ld\n", n);
pTerrain[n] = 0;
}
return 0;
}
Each time I run this, it drops out at a slightly different value of n (usually around 146500, but not always so high).
Can anyone tell me if I'm making a foolish mistake, because I just can't come up with any ideas. I am a beginner, though, so you'll have to bear with me if you will. Thanks in advance for any help anyone can give me.
Cheers,
Dan Roberts.