Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 25th, 2006, 6:05 AM   #1
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
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]);
}
vc1's range is 0-199. But we need to specify the element one past the end. Does that result in undefined behaviour?

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.
InfoGeek is offline   Reply With Quote
Old Jun 25th, 2006, 6:19 AM   #2
nemesis
Programmer
 
nemesis's Avatar
 
Join Date: Aug 2005
Location: Bristol, England
Posts: 71
Rep Power: 3 nemesis is on a distinguished road
Send a message via MSN to nemesis
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.
nemesis is offline   Reply With Quote
Old Jun 25th, 2006, 6:24 AM   #3
Mad_guy
Hobbyist Programmer
 
Mad_guy's Avatar
 
Join Date: Oct 2004
Location: Sandstorm, Techno Club
Posts: 239
Rep Power: 4 Mad_guy is on a distinguished road
Send a message via AIM to Mad_guy Send a message via MSN to Mad_guy
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..
__________________
os: mac os 10.5.4
revision control: git
editor: emacs

site
Mad_guy is offline   Reply With Quote
Old Jun 25th, 2006, 7:46 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 25th, 2006, 8:59 AM   #5
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 Narue is on a distinguished road
>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.
Narue is offline   Reply With Quote
Old Jun 25th, 2006, 11:56 AM   #6
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
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.
InfoGeek is offline   Reply With Quote
Old Jun 25th, 2006, 12:09 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 25th, 2006, 12:52 PM   #8
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 Narue is on a distinguished road
>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.
Narue is offline   Reply With Quote
Old Jun 25th, 2006, 3:22 PM   #9
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
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
uman is offline   Reply With Quote
Old Jun 25th, 2006, 3:39 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:17 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC