Which data types, when declared as arrays, are null-terminated? Is it just the char type? Or do numerical data do it to?
Which data types, when declared as arrays, are null-terminated? Is it just the char type? Or do numerical data do it to?
When I cout the last place in an "int array", these numbers appear "39124504". I have no idea why.
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.
Did you do this:Originally posted by Zewu
When I cout the last place in an "int array", these numbers appear "39124504". I have no idea why.
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).Code:int MyArray[5]; cout << MyArray[4];
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.
>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.
I did this: int Array[] = {2, 8}; cout << Array[2];
I am aware of that an array starts counting at 0.
>int Array[] = {2, 8}; cout << Array[2];
You are aware?
You have two elements in the array, but try to read from the third element, thus a "random" value.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.
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.
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".Originally posted by Zewu
Well, then that's another way to get randomize numbers. Or?
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.
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.
>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.
>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.
>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?
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