November 16th, 2013, 07:17 AM
Integer array gives memory error at array
Confuses me a lot, here is the source code:
printf("\n \n \n");
printf("This program will calculate the first 10 factorials\n \n");
printf("\n Those were the first 10 factorials! \n \n \n");
int fakultaet(void) //Parameter nicht benötigt, da immer fakultät von 1-10
fakul[j]=fakul[(j-1)] * j;
printf("\n %d factorial: %d",j,fakul[j]);
output is: http://vvcap.net/db/cupuhH5BXr6KEyoROCqv.png
(excuse the language)
after the 8'th factorial it gives me a memory access error, the one you would get when you would access fakul, even though fakul was defined to have 10 elements.
November 16th, 2013, 07:54 AM
You should really run the program in the debugger to find out what is really going on.
For one thing, exceptions are almost certainly printed on stderr, which is typically unbuffered, whereas stdout is typically buffered.
The result of which is what you see might not be a completely accurate assessment of where the program really got to.
Having said that, a program with undefined behaviour is broken from the start, not from the point where it first goes wrong. So technically, anything you do observe is "heresay" evidence at best.
> int fakul,j,fakultaet(void);
It would seem that on your machine, trying to write fakul results in j being trashed instead.
November 16th, 2013, 07:58 AM
Originally Posted by salem
I actually just figured out what the issue was.
The memory access error wasn't occuring at 8, but rather at 10 or 11, not quite sure which one of those two, after eliminating that the program works.
November 17th, 2013, 03:04 AM
The language is fair enough, but why have you posted an *image* of *text* output when you could have far more simply merely posted the text!? You know that you can copy & paste console text directly?
Originally Posted by TRUCKERm
Even had you not posted text, you might at least have rendered the image directly in the forum rather than requiring an off site excursion:
November 17th, 2013, 03:29 AM
You should be sure bro! lol. Your array is declared as i.e the index is from 0 to 9. You initialized the first array index with is fakul to value 1.
Originally Posted by TRUCKERm
And then in your function you were using a for loop like so
with the variable j which is to serve as your array index, starting from 1, which in this case will be your array second index from which you started to do your calculation from using the previous value of the last index, like so , multiply that with the value of j and then "push" into the array index which the number j stands for.
But note that since the array has just 10 indexes starting from 0, the last result you will have is in fakul, which is your 10th factorial value. So your for loop should have been
Run this in your function
for ( j = 1; j < 10; j++)
and you will see something like thus:
for(i = 0; i < ((int) (sizeof(fakul)/sizeof(fakul))); i++)
printf("index: %d and value :%d\n", i, fakul[i]);
Among other things.
index: 0 and value :1
index: 1 and value :1
index: 2 and value :2
index: 3 and value :6
index: 4 and value :24
index: 5 and value :120
index: 6 and value :720
index: 7 and value :5040
index: 8 and value :40320
index: 9 and value :362880
Hope this helps.