![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Is this undefined behaviour?
I was reading "The C++ Programming Language" by Bjarne Stroustrup and I came across this:
char vc1[200];
char vc2[500];
void f()
{
copy(&vc1[0],&vc1[200],&vc2[0]);
}EDIT: Here's the copy function: template<class In, Class Out> void copy(In from,In too_far, Out to)
{
while(from != too_far)
{
*to=*from;//copy element pointed to
++to; // next output
++from; //next input
}
}
__________________
PFO - My daily dose of technology. Last edited by InfoGeek; Jun 25th, 2006 at 6:30 AM. Reason: I thought I should include the definition of copy as well. |
|
|
|
|
|
#2 |
|
Programmer
|
it would write into the next memory location after vc1[200] and it would most probably corrupt some other data in the memory or possibly cause the program to crash if your really unlucky. Either way you definatly should not address vc1[201] or any other element past 200.
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
It results in a canonical fencepost error, to put it bluntly. You actually might write into the vc2 variable since it's located right after in a declaration. Although I can't say you wouldn't crash because where the compiler allocates memory like that is really dependent on things like the compiler, settings, system, etc..
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
while from != too_far: since you're incrementing by one, you will not enter the loop if from is vc1 [200]. Consequently, you will not write into that location. Since you are not referencing that location, but just testing a number representing its address, you should not have any problems. Adding a number representing the location of vc1[0] and the number, 200, isn't a bad thang. (That's what pointer arithmetic for vc1 [200] is doing.) After all, the number you get for vc1[0] might very well be the population of Singapore, and you can certainly add 200 to that. Just don't try to dereference the population of Singapore (or Cleveland, for that matter).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 3
![]() |
>Does that result in undefined behaviour?
No, as long as you don't actually try to access that memory location, you can still use its address and not invoke undefined behavior.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
So, does that mean It's Ok to get the address of 100th or 1000th element of an array that may well be just 10 elements long?
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Okay, listen up. An address is just a number. You can 'get' any number you like, so long as you don't try to USE it as an address. For that purpose, it must be valid. When you were using the number for the 200th (non-existent, or at least not valid) element of that array, you were just calculating with it, not USING it. You prevented your code from attempting to USE it by writing, "if it equals this, DO NOT USE". Nothing wrong with that. Calculating the address of an element, valid or not, does not require referencing it (and thus maybe performing a no-no). You are merely performing arithmetic. The notation, myArray [anyNumber], calculates the address by adding those two thangys, the address of myArray plus anyNumber. Now you have a new address. You can compare it to anything you like. If you dereference (USE) it, it must be valid.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 3
![]() |
>So, does that mean It's Ok to get the address of 100th or 1000th element of
>an array that may well be just 10 elements long? No, the exception *only* applies to one past the last element. The C++ standard requires that one past the last element must be reserved and owned by the array, but the contents are indeterminate and you're not allowed to actually access the address. So for "int a[10];", &a[10] is legal and well defined, but a[10] and &a[11] are undefined behavior.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#9 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
out of curiosity, isn't &vc2[0] just a roundabout way of saying vc2? Why not &(&vc2[0])[0]?
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials. --WilliamSChips on Slashdot |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I apparently gave a wrong answer, here. I hate that shit. I ask Narue: is it now required that compilers backtrack to the original declaration prior to performing the arithmetic, so as to avoid undefined behavior? I don't see the point, other than to attempt to circumvent what might lead to a trouble-making dereference.
To answer Uman's question, yes. &vc2[0] is the same as vc2. The first method is, actually, more technically accurate, in my estimation (see my pointer tutorial). The use of "vc2" to yield an address, rather than content, is a "favor" by the compiler, based upon the fact that you can't deal with an array as a single entity.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|