Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

A teaser pt 2.

Options
  • 18-08-2000 6:10pm
    #1
    Registered Users Posts: 2,199 ✭✭✭


    This is a question that is related to Baz's original post that has been presented to me many a time.

    If u declace an array in C/C++ such as

    int array[5]

    does C/C++ create 5 element spaces in memory (0-4) or 6 elements (0-5)?


Comments

  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    It creates 0-4 afair. I remember having an argument with my teacher over this. Not that I was saying he was wrong more that I couldn't figure what the hell he was talking about.

    In BASIC, "dim a$(5)" would be 6 characters, where as "dim a$(1 to 5)" would be 5.



  • Business & Finance Moderators, Entertainment Moderators Posts: 32,387 Mod ✭✭✭✭DeVore


    afair its 5
    even in basic.

    Dim a$(5) in basic and try to access a(0) should give you an out of bounds array.

    I'd have to test this to be sure, but thats my recollection of VB.

    I'm breaking my own rules here and posting my opinion rather then a researched fact but I'm labeling it as such smile.gif

    DeV.




  • Registered Users Posts: 3,308 ✭✭✭quozl


    Its definately 5 in c. 0-4. Definately.
    if you declare int array[5] and then use
    array[5]=254;
    thats an array overrun and will cause a segmentation error. And a crash.
    Greg


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    dim a(5) as integer
    a(0) = 1
    print a(0)
    
    That works.

    Loaded up VB6.
    dim a(5) as integer
    a(0) = 1
    msgbox a(0)
    

    That works too.

    The reason why you may be getting that error is using "OPTION BASE 1" stops the programmer from defining the 0 part of the array (Default is normally "OPTION BASE 0").

    Like "OPTION EXPLICIT" I believe VB allows you to set this as a default in the preferences somewhere.

    My feeling on the whole thing is to leave Option Base set at 0. Setting it for 1 is for newbies. wink.gif


    [This message has been edited by Hobbes (edited 19-08-2000).]


Advertisement