Thread: Array sizing

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

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2

    Array sizing


    when the array size is 100, it does not crash


    but when i make it higher or less than 100, it crashes. what causes the problem?


  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3
    You wrote:

    number[variable]. What does "variable" evaluate to?

    Maybe you meant to write:
    Code:
    for(variable=0;variable<=max;++variable)
        .......
        ........
    Was that it ?

    It's either change the whole loop to "counter" OR "variable". It's your pick.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    Originally Posted by C learner
    You wrote:

    number[variable]. What does "variable" evaluate to?

    Maybe you meant to write:
    Code:
    for(variable=0;variable<=max;++variable)
        .......
        ........
    Was that it ?
    well that was embarrassing -_- simple bug and couldn't find it! Thank you! it works now
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3
    lol no problem :)
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    First of all post your code in text format dont post screenshots of code.

    And Where did u initialize value of variable.?
    That's why u r getting such outputs.

    use
    Code:
    scanf("%d",&number[counter]);
    printf("%d ",number[counter]);
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    As already pointed out, variable was never initialized, so it contains a garbage value which is whatever just happened to be in memory at that location from previous use of that memory location.

    The probable reason for it crashing unless the array size was 100 would be that that size of the array placed variable at a memory location that just happened to have a garbage value small enough to be reasonable or at least positioned that array element at a location that did not visible harm. When you made the array a different size, then variable was positioned at a different location which had a garbage value that did cause a crash.
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    also you effectively wrote
    for (i=0; i<=MAX; ++i)
    which consumes MAX+1 values.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo