hi frnds below is my code when i run this program iam getting the error message as segmentation fault.can any one tell me what is segmentation fault and how to overcome that.it will of great help if any one can tell me where i need to make corrections.any help is highly appreciated.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define N 64
#define SIZE pow(2,N)
int main()
{
int j,n;
double m;
n=(int)SIZE;
long double* a = (long double*)malloc( n * (sizeof(long double)));
long double* b = (long double*)malloc( n * (sizeof(long double)));
m = 0.0;
for (j=0; j<pow(2,N); j++)
{
a[j] = (6.78 + 9.56);
b[j] = exp(a[j]);
m=m+b[j];
}
printf("m = %e\n",m);
getchar();
}
2^64 won't fit into an int (j) unless you have a 128-bit CPU, which I very much doubt. Also, 2^64*sizeof(long double) is, assuming sizeof(long double)==16 bytes, 295,147,905,179,352,825,856 bytes, or 256 exabytes, or 274,877,906,944 gigabytes, which is many more times more storage than has ever been manufactured.
You'll have to either decrease N, or find a more space-efficient algorithm to calculate what you want, because there won't be a computer with enough memory to run this until at least the next century.
WTF. You've asked this question several times already in other posts and have received several "no you can't allocate anywhere close to that much memory" responses.
I noticed this yesterday, but only after waking up I can answer.
There's no need for this program to use neither arrays nor a loop: m=exp(6.78+9.56)*pow(2,N);
There. You don't need to wait an enormous amount of time for this to finish, and you don't need to wait until the next century for a computer capable of running this.
I don't know where you found that long double is 16 bytes. I knew it was only 8 bytes like the normal double, I checked now, and it seems that in visual studio 2008 pro edition, it's only 8 bytes. Please let me know how did you get a 16 byte double. I think I can use that if it's available.
It was just an example. It doesn't really matter what the sizeof(long double) equals. Even if it was 4 bytes, 4*2^64==64 exabytes, which is still a ludicrous amount of information.
How many more times will you need to hear this? The program you posted above can't be ran by all the computers on Earth working together. Did you think I was exaggerating when I said that the memory needed exceeds the sum of all the storage that has ever been manufactured? It's just impossible to run with our current technology, and it will continue to remain so for both our lifetimes.
There are basically two requirements to run a program: CPU time and memory space.
A program exceeds the capacity of a CPU when it takes longer to run on that CPU than is practical. For example, a video playback program can't take longer than ~1/30 of a second to display a video frame. However, given enough time, any runnable program can be ran on any CPU*.
If a program requires more memory to run than is available to the computer, the program is unrannable on that computer. This is the case of your program. Running your program requires an amount of memory that exceeds not just on any one computer, but on all the computers on the planet. Your program is unrannable in the most literal sense of the word.
The simplified formula m=exp(6.78+9.56)*pow(2,N); does exactly the same as your program without the ridiculous memory requirements.