- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Nope. At the last loop the p will be equal to a. Methinks.
The problem is that incrementing a pointer one past the end of the allocated memory area is an undefined operation per standard (IIRC). a+N-1 does not need to be optimized, it may literally take address of the array, increment it by N elements, and then move back one element. On most platforms this will work as you expect it to. But it's not guaranteed to.
As for filling the array in C, there's no standard way IIRC. If sizeof(char)==sizeof(short) then memset will work. In C++, there's std::fill.
I don't think there are real scenarios where the code would b0rk, unless you're on some crazy architecture. Maybe some big iron would mind such code. I mean architectures contorted enough to have a dereferenceable (w/o violations) NULL pointers.
I can't think of a commonplace "consumer" architecture where pointer arithmetic isn't done modulo some power of two, and pointers can't store crap without further ado. I'd have to look up how Turbo C did pointer arithmetic, but in all models I think this would still work as you expect, even if the segment registers would be tweaked along the way.
Am I missing something obvious (it's late here)?
Admin
Tool.
Admin
The concern of a+N-1 does not actually apply to this case. The calculation will be correct even if a[] takes up the last N addressable words (which is why the spec makes *(a+N) undefined to begin with).
The problem with the original code was
. After the last loop, when p == &a[0], there is no gaurantee that --p will result in a p value less than &a[0]. Specifically if &a[0] = 0 (a[0] is the first addressable word), then the last --p will result in p much larger than a since pointers are unsigned.I don't think that matters,
should always work.If &a[0] < sizeof(short *) then the pointer arithmetic on p will underflow causing p to have a very large value. I'm not sure which OS would give you a data segment with a virtual address starting at 0x00000, but it is conceivable and it would die.
I wouldn't call it obvious.
Admin
Contrary to what was stated, their standards are not "immensely high", they are "exclusive." They may be high as well, but the primary effect is exclusivity. There is a difference.
Most employers make this mistake. They will pass over extraordinarily capable candidates because "they didn't have enough knowledge of Specific Technology X." I'm sure most employers would turn down Donald Knuth for a job writing Ruby code because he didn't have specific knowledge of Ruby even though he is one of the most renowned computer scientists in the world.
Admin
Because the pass-by-value parameter is constructed by the calling function, saving one copy operation in some cases. For instance:
This will create an empty string, fill the string using the stream operator >>, pass a reference to this string on to twistAndPrint, where it will be copied.
On the other hand, if twistAndPrint accepted an std::string, the compiler could easily figure out that since "input" is no longer used after "twistAndPrint" (the only method that will be called on "input" after twistAndPrint is the destructor), the compiler could create "input" on the stack in a location suitable to the subsequent invocation of twistAndPrint, and no copy operation would be necessary.
Of course, given sufficient knowledge of what twistAndPrint does and where it is used, the compiler might make the same optimization in the "const std::string&" case. But the point is that this requires the compiler to perform global optimization, using much more knowledge of the entire program, whereas the original optimization requires only local ("peephole") knowledge including the already known signature of twistAndPrint. Therefore, real world compilers will be much more likely to perform the optimization in the pass-by-value case than in the pass-by-reference case. Also, there could be incompatible uses of twistAndPrint which prevent the optimization in the latter case anyhow (most importantly if twistAndPrint is used in a way which prevents the compiler from altering the calling convention without breaking the linked program, i.e. exported to external programs or passed to external programs as a callback).
For further information, see "Move Constructors" by Andrei Alexandrescu.
Admin
No, that part is ok. It is fine to point one past the end of an array, as long as you don't dereference, and this is what users of the STL do all the time (remember that pointers are iterators!). The problem is that this code tries to move the pointer one past the beginning of the array, and that is illegal.
Yes, segmented memory. The array could be at offset 0 in a memory segment. The corresponding pointer is not a NULL pointer, but either the decrement itself or the following comparison will easily cause mayhem.
Segmented memory may be an oddity on x86 (32 or 64 bit) today because the flat memory model basically won, but no exotic processor architecture is necessary, only a more or less exotic rsp. outdated OS.
In real mode, the compiler-generated code may be smart enough to handle this case. But even then, in Borland C++ it would not work in all memory models because only in the "huge" memory model would the compiler bother to insert expensive segment adjustment code for pointer arithmetic in all cases. In other memory models, you had to use a special type of far pointer if you wanted that to happen IIRC.
In protected mode, segment adjustment isn't even possible in the general case, so all bets are off.
Admin
And she has an penchant for using the wrong indefinite article. On the other hand, she does have a pleasant phony voice. :-)
Oh, right, I almost forgot that MS invented the STL, and SGI (IRIX anyone?) never implemented that sucker, and never donated the code to the GNU libc++ project, and that libc++ is actually not available on a huge range of UNIX platforms be they Solaris, Linux, Mac OS X or many others. Sheesh.
Admin
hmmm... what's that on ur face? oh it's just egg.
Admin
I'm (almost) certain that the interviewed guy said that as a joke (no code reuse, just likes to start over).
Admin
As A C novice, I keep wondering why ++i is used instead of i++. Wouldn't that overflow the array at the last iteration, and keep the short with index 0 uninitialized??
Admin
memset() assigns a value to every byte in the referenced memory, and your "sizeof short" looks like a clue but it's not. The function receives the address of the array, a value, and the number of bytes to fill, period . . . so instead of 002A002A002A002A002A002A the memory will look like 2A2A2A2A2A2A2A2A2A2A2A2A
Admin
is equivilent to
The ++i or i++ is evaluated entirely in expr_3, it doesn't change when expr_3 itself is evaluated.
The difference between ++i and i++ is what value it evaluates to... which in the case of the for loop the value the expression evaluates to is never used.
Here's an example with behavior analogous to the for loop... notice how the choice of i++ or ++i doesn't matter... j matches i.
Here's an example of when it would matter.
And here's another example where it matters.
Hope this helps!
Admin
Admin
Not much difference in C, the difference is in C++. The "++object" form is more efficient when dealing with real objects. Using "object++" instead would require that a copy of the object be made before incrementing the real one. The copy is then returned instead of the original. With "++object" a reference to the real one can be returned.
Not important when you're dealing with ints, or where the value is not being used, but just a good practice to get into, especially with poorly written code that includes dynamic allocations in the object (which you don't want to have to check for). I believe there is a section in Effective C++ about this. The rule is: if you have pointers in your object, you need a destructor, an assignment constructor and a copy constructor (once again, if I remember right).
This is also an area where different compilers may produce different behaviour -- one may produce a copy of the object, another may not. It's done behind the scenes for you, or against you when things go awry. You want to avoid that difference in programs you want to be portable.
Admin
I used gVIM all the time, even in windows. VIM has had syntax highlighting for years...
Admin
By "junior developers", I assume you mean high-school students?
Seriously though, when I first looked at teh codez above, I didn't see the missing "=" either. Of course I know the difference, but "trick" questions like that are retarded.
Admin
In this case ++i is the only op being done so its irrelevant. However dont do "a[++i] = 42;". "a[i++] = 42;" would be OK though... :D Search google for «C unary prefix posfix». First hit will get you:
"The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference."
Admin
If you test candidates for
x=3; if (x=0) cout<<"0"; else cout<<"not 0";
and ask for a verbose explanation of the result than you may start getting some pretty explanations: "0 is diferent of 3; therefore «not 0»!"
Admin
I must say I love these esoteric discussions of the C spec. Every time I think I know the language someone knocks me down a peg.
But it's how I learn. Thanks guys.
Admin
K.D. you did not make me laugh. Why don't you just accept that the brilliant guy has never got a project wrong? As surprising as it may be, skilled and smart developers can get things always almost right (or fixed them on the go) and not let a project failed by incompetence.
I, for one, admire his honesty. He probably knew that what he was going to say could hurt him. I think he tested you. Something like "Let's if my (potential) boss can cope with a guy like me".
Admin
"Always almost right". 2+2 = 3.99999, but consistently so.
And he ended up saying something dumb that hurt him. Zero points awarded.
I wouldn't want someone on my team who had to be "coped with". Why can't you play well with others?
Admin
I'd rather use UltraEdit frankly.
Not a huge fan of vi. Or of emacs.
Admin
Hmmmm. Counting coup, nerd-style.
Admin
That's why an experienced programmer will always write. if ( 5==x ) { } We had this arguement on here before: "An expert programmer shouldn't rely on the compiler to find his mistakes" vs "An expert programmer uses all the help he can get"
Admin
(I'm the original poster/interviewer in the story)
Yes, that in fact, I believe, was what he meant. He didn't believe in ever going back to refer to previous work (of his or anyone else's) but rather wanted to start every single project with a clean slate. Admirable in a sense, but why on earth would you want to re-invent simple common elements over and over?
And to the person who said that asking someone to point out their weaknesses is a flawed interview style -- well, actually, it's worked very well for me. This candidate was horrible flawed, and by opening a door, he pointed it out himself.
Admin
Heh, the job I am about to leave gave the latter as an answer when I asked. Actually, they volunteered it as if it were a source of pride. Gah.
Bonus points if said in-house "source control" is written in VB6.
Admin
I was about to say the same thing. To the best of my knowledge, most university classes do not discuss namespaces. I feel like many C++ programmers just know that they need to put using namespace std; at the top of their files.
Additionally, I have to wonder how important it is that a job candidate understand namespaces on the day that they start if they show overall competence. First, namespaces are easy to understand, so, it wouldn't take long to explain to explain if your organization really is making use of them. Second, I think that maybe half of all organizations actually take care to use them in any meaningful way.
Admin
Admin
Honestly I'm shocked at the number of pple that posted stuff similar to this. Nobody has ever put out 100% perfect work. If you can't accept your own faults, then you'll never improve.
Yes your projects may all have been completed successfully ... that doesn't mean there weren't decisions you made that you had to go back and change, or in hind-sight wished you could have changed.
I hope I get asked that question at future interviews. Then at least I'll know that I'll be working with people that grow ... instead of stagnate in the knowledge that everything they produce is perfect.
Admin
Yes, I agree. This is the perfect kind of question I want my company to be asking in an interview.
Why worry about actual coding ability when they can rattle off obscure facts about the C language specification that are rarely applicable to modern architectures? After all, as we all know, everyone who is worth their salt has been coding in C since the days of segmented memory.
Furthermore, I think it goes without saying that people who enjoy picking apart trick questions are the very kind of people I want designing my algorithms. Sure, its one thing if the algorithm works, but is it optimized? No doubt we would all agree that all algorithms we write should exhibit the same kind of cleverness as these questions. Cleverness = maintainability.
To extend this idea a bit, I think I will ask interview questions about real-mode programming and maybe pepper in a few questions about the ALGOL specification.
Admin
Admin
I was in Jonestown, Tanzania once and saw a strip mall shop sign announcing Kinko-like services and more. The shop was also offering typing services. The name, written in bold letters in front of the shop, was "Helen's Secretalrial Services".
I didln't enquire about their serlvices.
Admin
I actually agree with you that posing trick questions about obscure trivia is not a great interview technique, but it can be a good learning technique. If you're not interested in understanding or learning why something like underflow on pointer arithmetic might result in undefined behavior, then you might want to stay away from C/C++ because those languages love to stick you with runtime bugs that result in rare instances of that undefined behavior biting you in the ass. Without a doubt, this is a major drawback of C/C++; other languages let you largely stop worring about such details.
So, while maybe being able to pick out the obscure potential bug is not something to look for in a potential hire, I'd certainly like anyone who I'm working with on a C/C++ project to at least want to understand or learn about these pitfalls if someone's pointing them out.
Admin
Yes, I agree. It makes me wonder if the author understands the language very well himself. There certainly are cases where a mutable value parameter is preferable.
At companies in the Seattle where I have interviewed - and worked - at (Amazon, Microsoft, Real) there is much more of an emphasis placed on higher level design than on basic language semantics. When I have interviewed for our team I always focus on finding out what the candidate can do well and how they would be able to contribute. Often intellectual horsepower is a better measure of a candidate than language specifics anyway.
Admin
There is a difference between not knowing what a standard library is, and not knowing what its namespace is, or that it has one, since some implementations let you call the entire standard library (or what parts of it are implemented) without it.
Admin
The policy "Can't tell them why they're not hired for legal reasons" has always translated in my mind to "Some of our reasons for not hiring people are illegal".
Admin
This is never true on a normal programming environment (it may be true for some exotic embedded systems) - the reason: sizeof(char) always == 1. sizeof(short) is ALMOST NEVER going to be that, because a short has to be at least 16 bits, and a char is normally eight bits.
Admin
Admin
Fresh out of uni my CV was short on content so I decided to employ a "professional" CV writer to rewrite it for me using all the tricks of the trade. I filled in her questionnaire and sent off the results, a couple of weeks later I received the finished article. There were numerous typos including the following:
"Accurate and eloquent in witten communications."
Admin
Anyay?
Admin
Admin
This is obviously engineered (poorly).
Admin
Oh, please, who can remember what all those acronyms mean? The question is, does she know how to -use- std? (What does it mean, anyway? Standard? And whenever I see stdio and want to pronounce it as "studio"... Talk about poor naming conventions...)
Admin
That's more WTF than what was highlighted although less hilarious