#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Integer array[10] gives memory error at array[8]


    Confuses me a lot, here is the source code:



    Code:
    #include <stdio.h> 
    #include <math.h>  
    
    int fakul[10],j,fakultaet(void);
    
    int main(void) 
    {
    
    	fakul[0]=1;
    	printf("\n \n \n"); 
    
    	printf("This program will calculate the first 10 factorials\n \n");
    	fakultaet();
    	printf("\n Those were the first 10 factorials! \n \n \n");
    	return 0;
    }
    
    int fakultaet(void) //Parameter nicht benötigt, da immer fakultät von 1-10
    {
    	for (j=1;j<=11;j++)
    	{
    		fakul[j]=fakul[(j-1)] * j; 
    		printf("\n %d factorial: %d",j,fakul[j]);
    	}
    	return 0;
    }

    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[11], even though fakul was defined to have 10 elements.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,384
    Rep Power
    1871
    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[10],j,fakultaet(void);
    It would seem that on your machine, trying to write fakul[10] results in j being trashed instead.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by salem
    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[10],j,fakultaet(void);
    It would seem that on your machine, trying to write fakul[10] results in j being trashed instead.

    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.
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1803
    Originally Posted by TRUCKERm
    output is: http://vvcap.net/db/cupuhH5BXr6KEyoROCqv.png
    (excuse the language)
    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?

    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:

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    1
    Originally Posted by TRUCKERm
    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.
    You should be sure bro! :D lol. Your array is declared as
    Code:
    int fakul[10];
    i.e the index is from 0 to 9. You initialized the first array index with is fakul[0] to value 1.
    And then in your function you were using a for loop like so
    Code:
    for (j=1;j<=11;j++)
    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
    Code:
    fakul[(j-1)]
    , 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[9], which is your 10th factorial value. So your for loop should have been
    Code:
    for ( j = 1; j < 10; j++)
    Run this in your function
    Code:
    	for(i = 0; i < ((int) (sizeof(fakul)/sizeof(fakul[0]))); i++)
    	    printf("index: %d and value :%d\n", i, fakul[i]);
    and you will see something like thus:
    Code:
    ...
    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
    ..
    Among other things.
    Hope this helps.

IMN logo majestic logo threadwatch logo seochat tools logo