PDA

View Full Version : Null Terminated Arrays



sean
06-14-2002, 01:11 PM
Which data types, when declared as arrays, are null-terminated? Is it just the char type? Or do numerical data do it to?

Zewu
06-14-2002, 01:25 PM
When I cout the last place in an "int array", these numbers appear "39124504". I have no idea why.

Magos
06-14-2002, 01:55 PM
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.

Magos
06-14-2002, 01:57 PM
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:


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).

Shiro
06-14-2002, 02:00 PM
>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.

Zewu
06-14-2002, 02:13 PM
I did this: int Array[] = {2, 8}; cout << Array[2];

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

Shiro
06-14-2002, 02:20 PM
>int Array[] = {2, 8}; cout << Array[2];

You are aware?

Magos
06-14-2002, 02:28 PM
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.

Zewu
06-14-2002, 02:37 PM
Well, then that's another way to get randomize numbers. Or?

Magos
06-14-2002, 02:40 PM
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".

no-one
06-14-2002, 03:26 PM
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.

Shiro
06-15-2002, 03:31 AM
>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.

no-one
06-15-2002, 11:22 AM
>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...

Sorensen
06-15-2002, 11:26 AM
>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?

Mario
06-15-2002, 11:34 AM
I think he's confusing vectors with arrays.... or variables that are declared globally being initialized to 0... dunno.

no-one
06-15-2002, 12:08 PM
>>in C++(maybe C now) reading one past the end of an array is legal and guaranteed to work


i havn't got $250 to buy a copy of the standard from http://www.ansi.org, and point it out to ya, and i don't feel like hunting down an illegal copy.

so, get a C++ book that covers arrays(The C++ Programming Language by Bjarne Stroustrup) or even the STL, and read it, or read THE STL HEADERS!!!! A LOT OF THE STL RELIES ON THIS TO WORK!!!!FOR ONE THE VECTOR CLASS!!!!!!!

Mario
06-15-2002, 12:13 PM
I have the Stroustrup book. What you say is wrong. You are wrong. So stop yelling and study. It's ok to be wrong. What is not ok is to adamant.

sean
06-15-2002, 12:24 PM
Well let me explain the problem and maybe there a better way to solve it then with /0. If have someone pass my function (it's in a library) a pointer to the start of an array. Now this array could be multidimensional. So I was going to have them give me the number of dimensions, and then a pointer to an array with all the numbers for the dimensions. I was just looking for an easier and more convenient way for the user. Any ideas?

Sorensen
06-15-2002, 12:27 PM
>so, get a C++ book that covers arrays(The C++ Programming Language by Bjarne Stroustrup) or even the STL, and read it, or read THE STL HEADERS!!!! A LOT OF THE STL RELIES ON THIS TO WORK!!!!FOR ONE THE VECTOR CLASS!!!!!!!<

It just so happens I've got that book (luckily mine's not all caps, it makes it easier to read you know).

I believe you are slighly confused. What I think you are refering to are iterators, whereby a sentinel value is used to mark the end. This isn't the same as being allowed to use the contents one passed to end of the array. Even if you use bog standard arrays in the STL algorithms, you will never actually use the address of the element one past the end of the array. It is just used as the address to stop at. For instance if you have an array

int a[10];

and then a for loop

for (i=0;i!=10;++i)
//do something with a[i]

10 is used as the location of the element one past the end of the array. It would be used as array.end() in iterator speak, but would never be assigned from/to.

btw, the draft standard is freely available.

Zewu
06-15-2002, 01:05 PM
Originally posted by Mario
I have the Stroustrup book. What you say is wrong. You are wrong. So stop yelling and study. It's ok to be wrong. What is not ok is to adamant.
I can't study, I'm just not patient enough:( I can maybe read 10 minutes every hour at max!

Now, does anyone know where I can get sum sweet amphetamine to keep me up for 96 hours, so that I can cram all the **** into my head once and for all?

sean
06-15-2002, 01:44 PM
Well that adds up to be exactly half a full work day every day.

no-one
06-15-2002, 10:19 PM
>I was just looking for an easier and more convenient way for the user. Any ideas?

unfortunatly no, unless you designate a value(s) for the end the lengths must be supplied.

>
I have the Stroustrup book. What you say is wrong. You are wrong. So stop yelling and study. It's ok to be wrong.
<

don't preach to me i've been doing this for years... and i still get confused...

>What is not ok is to adamant.

you are quite right, but as i exlain in a sec i didn't realize it.

>btw, the draft standard is freely available.

where at? i have yet to see one.

Sorensen, Sean, everybody else...

im sorry, you are right, i have a tendancy to get confused sometimes especially when im tired and stressed(i try to avoid anything requiring thought), and i get a little overboard... i realized my error about an hour ago and i see that Sorensen has pointed out ONE of them... anyways i feel stupid now!

thanks a bunch.hehe...

not to make excuses im under an extraordinary stress lately so im a bit testy, tired, sick, and over stressed. i apologize.

my blood pressure is way to high for no apparent reason 146 over 82 which is not good at all and i was calm and relaxed when they took it, im very certain it goes much higher... i can feel it when it does.

i'm way to young to be dealing with the **** im dealing with now, so please don't judge me to harshly.

sean
06-16-2002, 10:10 AM
Wow... that is high.... I have really low blood pressure. It's usually something 100/60

Zewu
06-16-2002, 12:15 PM
I have worried much about my heart lately:p

adrianxw
06-16-2002, 01:00 PM
My blood pressure used to go sky high from time to time. I did a series of experiments, and discovered the sky high values often occurred after I had eaten, narrowing down, after I had eaten cheese. When I went to my quack with my results, she did some tests and found I was lactose intolerant, i.e. allergic to milk.

Zewu
06-16-2002, 02:26 PM
This makes me think of a really strong magnet, which almost drags your heart out, so that you can see the shape of it, like in a cartoon, and then loses it's magnetism. Then the heart goes back and pumps out the blood in hyper speed, so that all the blood vessels burst at the end, and thus, all the organs burst, and you die.

sean
06-16-2002, 07:29 PM
So much for iron being an essential part of your diet....

Nick
06-16-2002, 08:20 PM
I've heard of this too. I belive code such as

int a[10];
int*p = a;
p--;

Is undefined since p points before the array. But code like

int a[10]
int* p = &a[10];

is correct but to dereferance the pointer is undefined of course.
This is useful in implementations of the stl since for something
like



vector<int> a;

for (vector<int>::iterator i = a.begin(); i != a.end(); ++i)
continue;


a.end() could return a pointer to the end of the array.

no-one
06-16-2002, 11:50 PM
> I did a series of experiments, and discovered the sky high values often occurred after I had eaten, narrowing down, after I had eaten cheese.

cheese huh? man that sucks... no milk.

i live on cheese...

>a.end() could return a pointer to the end of the array.

yes, this is what i was thinking of... using the adress is legal but not accessing, i'm not doin well of late.

sean
06-17-2002, 11:39 AM
Yeah - but that would actually call for mor arguments. I'll think of something...