Thread: Null Terminated Arrays

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    Null Terminated Arrays

    Which data types, when declared as arrays, are null-terminated? Is it just the char type? Or do numerical data do it to?

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    When I cout the last place in an "int array", these numbers appear "39124504". I have no idea why.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    No arrays are automatically NULL-terminated. They are simply an array of a certain datatype (!).
    A string is an array of char's, where the last element is a NULL character. This is the definition of a string.
    If the NULL-terminator wasn't there, it would be a simple array of char's, not a string.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Zewu
    When I cout the last place in an "int array", these numbers appear "39124504". I have no idea why.
    Did you do this:
    Code:
    int MyArray[5];
    cout << MyArray[4];
    Then it will print out the value of the last element. But since you haven't specified any values, it can be theoretically anything (whatever was on that spot in the memory before).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >When I cout the last place in an "int array", these numbers
    >appear "39124504". I have no idea why.

    If the array is defined as "int array [N];" then N-1 is the last element. So if you printed array [N], you get undefined values, since array [N] is beyond the range of the array.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    I did this: int Array[] = {2, 8}; cout << Array[2];

    I am aware of that an array starts counting at 0.

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >int Array[] = {2, 8}; cout << Array[2];

    You are aware?

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Zewu
    I did this: int Array[] = {2, 8}; cout << Array[2];

    I am aware of that an array starts counting at 0.
    You have two elements in the array, but try to read from the third element, thus a "random" value.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    Well, then that's another way to get randomize numbers. Or?

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Zewu
    Well, then that's another way to get randomize numbers. Or?
    They aren't random as in "random", but random as in "whatever data was there before".
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    i could be wrong but...

    >They aren't random as in "random", but random as in "whatever data was there before".

    no, this truly depends on the compiler,

    first in any of the suggested examples you should never see what data(if any) was there before.

    this memory is usually initialized to a single junk value, virtually all non dynamic memory will recieve the same value...

    >
    int MyArray[5];
    cout << MyArray[4];
    <

    in theory the entire array would have the same value
    so,

    MyArray[0] == MyArray[1] ... == MyArray[4] == whatever value...

    and the value will remain the same each time your start reguardless of how many times you execute the program or any other programs.

    like i said this varies from compiler to compiler, machine to machine...

    anyway, in C++(maybe C now) reading one past the end of an array is legal and guaranteed to work, reading any more than that is not.
    Last edited by no-one; 06-14-2002 at 03:29 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  12. #12
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >no, this truly depends on the compiler,
    >..
    >this memory is usually initialized to a single junk value, virtually
    >all non dynamic memory will recieve the same value...

    If you get the value at the address beyond the scope of the array, you get a value which has a value you don't know. So in that way it is random.

    >int MyArray[5];
    >cout << MyArray[4];

    When the index is between 0 and 4, then the value might be initialized, which is compiler dependent. But MyArray[5] is not initialized, it is not part of the array.

  13. #13
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    >But MyArray[5] is not initialized, it is not part of the array.

    following the rules of C++(maybe C too) it would be my assumption that it would be be initialized as well since it is part of the array. anything past the arrays length +1 is what ever datas there, an illegal operation, or an init value. heheh...
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  14. #14
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >in C++(maybe C now) reading one past the end of an array is legal and guaranteed to work

    Where in the standard is this stated?

  15. #15
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    I think he's confusing vectors with arrays.... or variables that are declared globally being initialized to 0... dunno.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Big Problem With Passing an Matrix to Main Function
    By Maragato in forum C Programming
    Replies: 4
    Last Post: 06-14-2004, 11:06 PM
  4. Really Need Help With My Opengl Camera!!!!
    By psychopath in forum Game Programming
    Replies: 13
    Last Post: 05-28-2004, 03:05 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM