« first day (87 days earlier)      last day (4855 days later) » 

12:16 AM
wikipedia says "The plot centres around a bank robbery in a virtual world." that's already happened at least once in eve, around 2004-5(?) (and that book was published in 2007), in one highly publicized incident, but only around $20k US was stolen (it was a modified ponzi scheme)
and eve has no way to legitimately move in-game currency outside, with them cracking down hard on that, so maybe only worth 1-2k if he tried to sell it, instead of using the out-to-in exchange rate
 
 
4 hours later…
3:58 AM
anyone around?
 
4:23 AM
Nope
 
5:01 AM
:(
 
 
2 hours later…
7:23 AM
hardly ever
 
 
5 hours later…
12:18 PM
0
Q: comparing iterators from different containers

FredOverflowIs it legal to compare iterators from different containers? std::vector<int> foo; std::vector<int> bar; Does the expression foo.begin() == bar.begin() yield false or undefined behavior? (I am writing a custom iterator and stumbled upon this question while implementing operator==.)

 
12:30 PM
@FredOverflow good question. I'm no expert, but I think it must return false. IMO there's no reason to cause UB, unless of course, the mechanism you use to compare them could possibly return true when that's not the case.
 
12:52 PM
I queried the standard, and the requirements actually depend on the type of iterator.
 
1:06 PM
@jweyrich, do you mean that you can only compare those iterators if htey are a particular type... like const_iterator?
 
@BeeBand no, you can compare different types, otherwise reverse_iterator would never be comparable to its underlying iterator.
 
@jweyrich ah ok right. i was wondering, how do you mean "requirements actually depend on the type of iterator"?
 
@BeeBand I'm not sure about the aspect asked by @FredOverflow, but for input iterators a == b doesn't imply ++a == ++b, while forward iterators require this. His question translates to the legality of operator== on unreachable iterators.
 
@jweyrich, right. i see. that is interesting.
 
1
Q: checking if pointer points within an array

FredOverflowCan I check whether or not a given pointer points to an object within an array, specified by its bounds? template <typename T> bool points_within_array(T* p, T* begin, T* end) { return begin <= p && p < end; } Or do the pointer comparisons invoke undefined behavior if p...

New food for thought ;)
 
1:31 PM
@FredOverflow i just answered your question
 
 
1 hour later…
2:49 PM
@FredOverflow. Ah. Oh well I tried. I suppose even a downvote's useful for other readers to see typical errors in thinking.
 
@BeeBand I don't understand why you added that (incorrect) answer after others had already provided correct answers?
 
3:06 PM
@Alf - well, yes that's a good point. Thing is, Jon was writing his answer while I was writing mine and when I submitted my answer he hadn't yet included that link to the other question about std::less. I guess I didn't understand his answer properly and proceeded with mine! But now I realise ... should I remove my answer?
 
@BeeBand: That's up to you. As far as I know, there are no guidelines about whether or not incorrect answers should be removed. I typically delete my wrong answers. Others don't.
 
@John Dibling. I'll delete it. it doesn't add anything to the discussion. I tried but I can only vote to delete it, not delete it directly?
 
@BeeBand I generally remove if they're wrong and get upvoted. It may lead to confusion.
 
@jweyrich, yeah its just confusing if I leave it there. How can I remove it directly?
 
@BeeBand: Voting to delete does delete it. Your answer is now deleted, and only those with 10k+ rep can see it.
I believe you will always be able to see your own deleted answers, but they are greyed-out
 
3:13 PM
Oh. Ok great.
 
3:34 PM
Is a vector iterator much like a pointer or a pointer.
 
@Mahesh: A vector iterator can be used in much the same way as a pointer.
 
@John Dibling: Then, what ever holds true for pointers should true for iterators and vice versa. Isn't it ?
 
@Mahesh: to what do you refer?
 
like pointer arithematic, dereferencing
 
I'm not sure if you can make such a bold statement. But if you give specific examples, I might be able to help.
 
3:42 PM
If I may jump in here, a vector iterator and pointer types both model the Random Access Iterator concept.
 
Iterators can be dereferenced. I don't think you can employ pointer math on iterators, but you can use comparison, and to find the difference, you can use std::distance
 
To get from the list [E, X, A, M, P, L] to the list [A, X, M, E, P, L], i need to perform three... I don't know the word for that. Like a ring swap. How do you call that in mathematics or programming?
 
@John: Because vector iterators model the Random Access Iterator concept, you can write, for example, it + 3.
 
@Daniel: True. I stand corrected
 
@Mahesh A human is a living thing. Humans can talk. Does that imply every living thing can talk? Now replace "human" with "pointer" and "living thing" with iterator and you should see it.
 
3:50 PM
interators can talk?
:P
 
@Chris: Can't yours?
 
@ChrisBecke Okay, maybe you should also replace "talk" with "can perform addition with integers" or something ;-)
 
wait wait. boost::talking_iterator. got it
 
@FredOverflow: You're trying to apply the permutation (E A M)
 
@DanielTrebbien Ah, so it's called a permutation? That was on my list of candidates, but I wasn't sure. Thanks!
 
3:53 PM
@FredOverflow: I wrote it in cycle notation: secure.wikimedia.org/wikipedia/en/wiki/Cycle_notation
 
@FredOverflow- Thanks
void foo( int *ar, size_t size ) ; ar is a pointer to an array. Is there any way of doing away with the second parameter( i.e void foo( int *ar) ), and access safely all array elements. Nothing at global scope that mentions the size of array.
 
4:08 PM
@Mahesh pass a std::vector& (or std::list&) instead.
 
@jweyrich : But nothing in C - terms
 
you can use a template
yes, but nobody cares about C, we're writing C++
and for good reason
for statically sized arrays, you can use template<size_t size> void foo( int(&ar)[size] ){} to gain the size from the compiler
for dynamically sized arrays, you should use an STL container
 
@Mahesh you may use template <size_t N> void foo(int (&array)[N]) instead.
 
vector being the easy one
 
@Mahesh If the int array is fixed-width (e.g. int my_int_array[6]), then you could write a template function: codepad.org/ZUWF8CP4
 
4:12 PM
but keep in mind this doesn't allow you pass pointers, only references to arrays. This isn't allowed and must give a compile error: const int *array = { 1, 2, 3 }; foo(array);
 
@jweyrich: that's kind of the point
keeps it safe
if you have a dynamically sized array, use a vector
if you have a statically sized array, use a template
if you have a pointer, then you're not passing an array at all and it should be rejected
 
@DeadMG we agree. There's not reason for not using a std container.
 
@DeadMG - An industrial programmer asked for what good reason C++ is used over C. I told about data hiding, inheritance, virtual functions, templates. But he seems unsatisfied. What is there more important than all these ?
 
@Mahesh Another option is to declare the function void foo( int *ar, int *ar_end ) if you want dynamic-length arrays without using STL containers.
 
@Mahesh RAII? My favourite.
 
4:15 PM
@jweyrich What is RAII ?
got it
 
@Mahesh: Don't forget, there's a perfectly good possibility that he was an idiot
 
Resource Allocation Is Initialization :)
 
besides, more importantly, C++ offers safety
 
@DeadMG Could you please give me one example where it is safe on C++ while on C it isn't ?
 
@Mahesh: Easy- a Standard container
memory leak? puft- gone
C doesn't offer anything like that
and, how about the example we had earlier? template guarantees the correct array size
so does Standard container, too
but in C you can't get any of that kind of guarantee
 
4:22 PM
@DeadMG- So in C also we can write the safe code as long as we take the pain to handle memory leaks
 
no, it's not even remotely the same
the C++ compiler makes a guarantee that it enforces automatically
in C, the programmer makes a mistake, or the code changes, now he has to go back and update it manually
and he might make a mistake while updating it, too
whereas in C++, it's guaranteed by compiler
in every condition
it's faster and more reliable to maintain code where the compiler implements that sort of thing
 
@DeadMG To comprehend "in C, the programmer makes a mistake, or the code changes, now he has to go back and update it manually and he might make a mistake while updating it, too whereas in C++, it's guaranteed by compiler " , can you please give a programmatic example. Thanks
 
C++ is certainly much more sophisticated than C, and I must say this isn't even a fair comparison. But this also makes C++ a much more complex language than C, full of undefined behaviours. I'm impartial with that respect, but I generally prefer writing C++ over C, mostly because I don't like reinventing the wheel. C doesn't have standard implementations of common data structures which most programmers need every day.
2
You might argue there are plenty resources available out there, and fully tested, but you still need to integrate and familiarise.
 
@Mahesh When you malloc a block of memory, but forget to free it before returning. That's pretty common.
@Mahesh In C++, using a RAII type such as auto_ptr or boost::scoped_ptr will help to avoid memory leaks because the destructors of those types automatically invoke the delete operator to free the allocated memory.
@Mahesh Once you learn RAII, then C's lack of support for it becomes painful. It's why I developed a macro solution for RAII in C :) github.com/dtrebbien/raii_in_c
 
4:42 PM
@DanielTrebbien: auto_ptr concept is too good. I didn't knew that earlier. Thanks
 
@Mahesh You're welcome :)
 
@DanielTrebbien: They don't teach anything like this to us at university. How do you guys know all these?
 
@Mahesh Practice. I learned a lot of this by programming personal projects in C++ and asking questions when I was stuck.
 
@Mahesh: The did you a disservice at university then. This is a pet peeve of mine. Universities should do a better job of training C++ programmers.
 
@Mahesh Others also learn on the job.
 
4:47 PM
@DanielTrebbien: Even to practice, I should know of their very existence
 
@Mahesh: Learn, learn, learn. Read books. Follow blogs (like SO). Read through your compiler's documentation when you're not looking for anything in particular.
 
Should have followed SO from long before.
A grave mistake ;)
 
@Mahesh Yes. You're right. I think that I also learned by reading parts of a book.
 
Does SO has blog too? I didn't knew that
 
@JohnDibling John's advice is excellent. Read books and read code.
 
4:50 PM
@Mahesh: No, I used the term "blog" loosely
 
@Mahesh I think that he means the C++ questions feed: stackoverflow.com/questions/tagged/c%2b%2b
 
Right, what @Daniel said
 
Ok
by discussing to you all guys, it seems cplusplus.com is mere waste. What can I do when google ranks it high on the search list ;)
 
@Mahesh Well, the reference section can be helpful: cplusplus.com/reference
 
@DanielTrebbien: Tutorials ?
 
4:56 PM
i use cplusplus.com for quick reference, but take it with a grain of salt. the One And Only true C++ reference is the Standard itself.
 
@Mahesh I'm not even close to be a great C++ programmer. I truly started studying C++ recently. Until that, I was just a C programmer using C++ to make my life easier. Today I realise how much I missed.
 
@jweyrich How did you handle exceptions in C ?
 
@Mahesh I just found a free book about C++ on-line that looks pretty good: uow.edu.au/~nabg/ABC/ABC.html
@Mahesh Well, nevermind. That text is really old, probably pre-STL containers. It also tends to use the style of programming that I call "C in C++".
 
@Mahesh I'm not a big fan of exceptions, and I rarely rely on it. I always tried to avoid longjmp and I still avoid exceptions in C++ when possible, but it imposes more work than I'd have by simply using exceptions. For instance, throwing an exception from a constructor is much simpler than setting an internal flag and checking it later.
 
@JohnDibling Agreed. cplusplus.com has a lot of subtle mistakes and its code samples are usually pretty bad, but sometimes you just need the answer to "what are the parameters of that function?" type questions and it's great for that.
 
5:06 PM
@Mahesh Do you mean with a catch block (literally "handling exceptions") or how to make use of exceptions in your code?
 
@jweyrich: you may be due for another eureka moment. exceptions can be horrid or the answer to all your prayers. depending on how much you know about them and the environment in which you're using them
 
@DanielTrebbien: try, throw, catch is C++ way. I mean is there anything similar in C.
 
@Mahesh I'd mention the C++ FAQ Lite. You can learn A LOT from it.
 
One of the first very useful things I did when I started studying C++ (back then, computers were made of stone) was to purchase the print version of the C++ FAQ. I learned a ton from it.
 
5:10 PM
@JohnDibling you nailed it! I understand it quite well, but I don't really know when I should and should not use them.
 
@James: Much like diplomacy. The worst form of government, except all the others.
 
@JohnDibling Is the paper one worth paying for? I've only read the C++ FAQ Lite website. (Nevermind; it's on Safari; I can check it out there.)
 
Well when I got the print version it was in version 1, and the WWW didnt even exist. :) The website is probably sufficient these days.
 
@JohnDibling: Then why is it given so much importance by interviewers?
 
@JohnDibling Or C++ in general: the worst programming language except all the others ;-)
 
5:11 PM
Maybe I exxagerate. But it was a long time agop
@Mahesh: To what do you refer?
Oh. :)
 
Sorry. That to @JamesMcNellis
"C++ in general: the worst programming language except all the others ;-)"
Then why is it given so much importance by interviewers?
 
Why is what given so much importance?
 
Exception Handling
 
I don't know; I've never been asked a question about EH.
 
@JamesMcNellis: You told in general about C++
I thought you meant on Exception Handling
 
5:15 PM
I'm sorry; I am confused.
 
@JamesMcNellis: Most of C++ programmers ask me to care much about EH.
So I thought it is industrially very imp
 
Well, a proper understanding of exception handling and how exceptions work is fundamental to understanding how to write good C++ code. The problem of writing correct C++ code can in many cases be reduced to the problem of writing exception-safe code (not always, of course, but often, especially in single-threaded programs).
Exceptions suck, but that doesn't mean they are bad. They suck because error handling in general sucks.
 
generally the least of all available evils, I'd say
 
Agreed 100%. (Except for the best way of handling errors: manual using longjmp, of course!)
 
how do I list a list-type container ?
efficiently;d
should I copy all node addresses to an array, and then sort this array with specified compare function ?
 
5:31 PM
@Glorian what does the list verb mean in that context?
 
something like std::list
just a container based on nodes
double-linked
 
copy( mylist.begin(), mylist.end(), ostream_iterator<string>(cout, "\n"));
 
SHIT sorry I didnt say what I meant corrently
I DO NOT WANT TO LIST A LIST LOL
SORT IT
xd
I JUST WANT TO
 
@Glorian depends on the container and the type of sorting you want.
 
mylist.sort()?
 
5:32 PM
yes I know this method exist
but I've got implemented my own list for higher performance
 
@Glorian: No need to yell. I have a caffiene headache you're making worse. :)
 
;p
 
glorian, that's what I'd do
 
"I've got implemented my own list for higher performance" orly?
 
yes
 
5:33 PM
Anyway. Mergesort is ideal for sorting linked lists. It doesn't require random access to the elements and the fact that it isn't in-place is mitigated by the fact that elements are stored in nodes and don't need to be moved.
 
@JamesMcNellis nice article! (The Worst Form of 'Error' Handling Except For All The Others)
 
maybe its not as functional as this one from std::
but its pretty faster
 
@Glorian Out of curiosity, why weren't any of the standard containers sufficient? What makes your list faster?
 
it's simplier - it's better
 
More importantly, are you really sure you want a linked list? Linked list performance characteristics are not very good for most use cases.
 
5:35 PM
and I don't have to include stuff i don't need
 
@Glorian How is it better?
 
@Glorian sorry, I find it difficult to believe in such statement. You need to provide real and plausible arguments.
 
@jweyrich I liked it a lot. Overload publishes a lot of really good articles. There's a part two as well: Exceptions for Practically-Unrecoverable Conditions.
 
ok, so
std::list is 16 bytes big if i am not wrong
and mine is only 12
and could be smaller if I don't want to cache size
 
@Glorian It could be as small as 12 if you have 32-bit pointers and 32-bit size type. Furthermore, 16 bytes is so small that it almost certainly falls into the "so small it doesn't matter" size range.
 
5:39 PM
and if I want to find for example, a duplicate element in std::list
there is too much code
 
Well, inevitably you'll have to write that code for your list too...
 
if I want to do anything - there is too much code using it
 
@JamesMcNellis I'll check it. Thanks for the tip. Months ago I read a great article advocating in favour of exceptions usage, but I couldn't find it on my bookmarks. IIRC, it uses pretty much the same arguments used by the article you provided. Will let you know when and if I find it.
 
Glorian
it really, really doesn't matter
4bytes saved?
that's not gonna make a big difference in the execution of any real program
the heap allocator on a 64bit system will use hundreds of bytes per allocation
 
@Glorian Your standard allocator will probably allocate 8 byte-aligned blocks anyway, leading to a non-savings; an allocation of 12 bytes will probably end up being at least 16.
 
6:07 PM
@jweyrich Thanks. I'm always interested in good articles to read.
 
 
1 hour later…
7:09 PM
yay
I pixel shaded my multicoloured spheres
 
applause
 
thanks
 
italics!
 
@Runcible: You need more asterisks and less [i]'s.
 
or not
 
7:24 PM
or not. Ok, thanks. =)
 
sbi
8:05 PM
@Runcible Again, if you read the newbie hints on the right, you'll read that formatting here works most as in comments, where * italic text * and _ italic text _ works as well as ** bold text ** and __ bold text __.
 
Or just click the help link in the bottom-right side of your screen.
 
sbi
@jweyrich Wow! I had never seen this!
(And to think that I posted a new version of the newbie hints just yesterday...)
 
@sbi that's the bad thing about big cinema screens.
 
bold text
never knew that one
 
sbi
@jweyrich What? That their edges are way off what your eyes see at a glance?
 
8:19 PM
@sbi exactly. My main display has been a humble 13.3" for at least 4 years, and I occasionally switch to a 22"+, but feel like I need to spin my head way too much to notice "lateral details".
 
sbi
In the chat FAQ, it says: "If you need to invite someone to your room, do it from the room edit menu." I can't find this. Can any of you?
a test:

newbie hints

21 hours ago, 23 seconds total – 5 messages, 1 user, 0 stars

Bookmarked 21 hours ago by sbi

Oh. It will inline bookmarks, too. Somewhat, anyway.
 
@sbi I didn't find it either. Even if we find it someday, it's a quite unclear explanation. Shall we report (maybe at feedback)?
 
@sbi No, but you can invite a user from the user's chat user page.
 
sbi
@AlfPSteinbach But that would require that user to have been into chat before, tight?
 
@sbi Yes, and it seems you need to be admin for the room also?
 
sbi
8:31 PM
@AlfPSteinbach I can't find that on your chat page. Is that because you are in the one room I'm in?
@AlfPSteinbach Ah, yes, I can do this for users that are not, currently, logged in.
@AlfPSteinbach The thing is, though, that I understood that sentence to apply to someone who hadn't logged into the chat at all. No re-reading it, it seems this could also mean that they have to have logged into the chat, but aren't currently logged into the room I would want to invite them to. Exact wording:
"Note that you can only mention @someone who has been in the room at some point. If you need to invite someone to your room, do it from the room edit menu."
So I went to the chat feedback room:

Chat feedback

Problems with chat? Let us know...
and asked my question there:
in Chat feedback, 1 min ago, by sbi
In the FAQ, it says: "Note that you can only mention @someone who has been in the room at some point. If you need to invite someone to your room, do it from the room edit menu." Where would I find this "room edit menu"?
@AlfPSteinbach No, it seems not. At least I could invite you to the chat feedback room, where I'm not an admin.
 
The only "edit" button that I see anywhere is the button to edit the tagline.
 
sbi
@JamesMcNellis According to this message, the room edit menu would be the one in the sidebar here in this chat window:
in Chat feedback, 4 mins ago, by Tim Stone
@sbi Hm, that's a good question. I feel like the room menu drop down in the top right used to have more links on it...
I, too, think it has shrunk sometime around Nov/Dec.
 
8:46 PM
@sbi: most things shrink around Nov/Dec
 
sbi
Shrunk? Shrank? Shrunken? (My English grammar module is broken.)
@JohnDibling Uh?
 
well, it gets cold, ya know
 
sbi
@JohnDibling Ah, that one. Note that this is an international forum. There might be any antipodes here.
 
@sbi: Un-possible. the world is flat & suspended by turtles. Everyone knows this. en.wikipedia.org/wiki/Turtles_all_the_way_down
 
sbi
@JohnDibling Wasn't it four elephants standing on a turtle's back?
 
8:51 PM
Nope. It's turtles all the way down.
 
sbi
in Chat feedback, 3 mins ago, by Marc Gravell
I will fix the FAQ
 
LOOK! It's my favorite soapbox topic! stackoverflow.com/questions/4662482/…
2
 
@JohnDibling What's your problem anyway? volatile is great for thread synchronization! </sarcasm>
 
:)
i'm gonna let this one go
 
Let's not talk about volatile friends with access to your private parts
2
 
8:54 PM
oh no you di-uhnt
 
sbi
@JohnDibling Wimp!
 
I busy wait on an std::atomic<bool> is_it_beer_o_clock; most afternoons.
 
@James: lolo
@James: Sneaking up on it. Although lately it has been old-fashioned-o-clock
 
@JohnDibling The Intel blog post is very good.
 
@JohnDibling oh, I feel better for not being alone on the various failed attempts to insert formatted-links in comments. If anyone here knows that, please reveal. I probably skipped these lines during the help reading.
 
sbi
9:08 PM
@jweyrich They need to be in a message of their own. (Read the newbie hints!)
@JohnDibling I proposed to close stackoverflow.com/questions/4662482/…
 
@James: Yeah, that post may be what turned me around.
 
@sbi does the FAQ apply to comments outside the chat?
 
@sbi: As a dupe? Is it a dupe?
 
sbi
@jweyrich Oh, that!
Well, [link text](http://example.com "optional title") works in questions, answers, comments, and in the chat. So...
@JohnDibling Well, the answer to the other question does answer this question as well, doesn't it?
 
@sbi: I believe so. At least, that's how I would have answered if I wasn't lazy.
@sbi: But it's not exactly the same question. Rather it's the question the OP should have asked.
 
9:15 PM
@sbi oh, I was under the impression I already tried that in comments but it somehow didn't work. But it works like a charm, thanks!
 
sbi
1
A: "Close as duplicate" - what if only the answer is a duplicate?

sbiWe've already discussed this in the chat and my take on the issue is that it isn't really clear whether "duplicate" means "duplicate question" or "duplicate answer", I don't like having many identical answers to what's essentially the same question, and I'm fine with having many different que...

@JohnDibling How about you pick a good candidate, and we turn this into an FAQ? It would best, if you provided the accepted answer. Never mind the question's quality, we can pimp that easily.
 
I think the stackoverflow.com/questions/4557979/… question (itself) is best of all, because it is the question that all these other questions should be.
That said, 1) The quality of my answer isn't FAQ-worthy, 2) I'm not sure this question is truly frequently asked
I could do some extensive work on it, though
None of my answers are complete enough to be canonical
 
"Can I use volatile for synchronization in a multithreaded program?" is definitely a frequently asked question.
 
Maybe. I will work on building up my answer. Then we can review it & see if it is FAQ-worthy.
 
9:34 PM
From a standards perspective, this code is safe, right?
    char* s = 0;
    std::copy(s, s+0, s);
 
Oh my.
 
I'm asking because I want to know if I can eliminate what appears to me like a redundant check for null.
 
@wilhelmtell: No. 25.2.1/3 : Requires: result shall not be in the range [first, last).
 
ok sorry, ignore that. copy into a valid target.
 
(Speculation:) I don't think that it's valid even if you were copying it into another target. A null pointer can be neither the start nor the end of a range.
 
9:37 PM
    char *s = 0, *t = new char[newlen];
    int len = 0;
    std::copy(s, s+len, t);
so len is known only at runtime. also improtant..
 
That's an interesting question.
 
but it's known at runtime to be zero.
 
@wilhelmtell it won't copy anything because first == last, thus there's no dereference either.
 
Complexity: Exactly last - first assignments.
 
What I wasn't sure of was whether it's valid to do pointer arithmetics with a null pointer. Whether I can add an integer (even if of value zero) to pointer 0.
 
9:42 PM
But is [NULL,NULL) a valid range?
 
Yes yes, the common implementation will do nothing. That's what I want. I just ask if I can count on it.
 
So long as the pointer is not dereferenced, yes its a valid range
 
Yes; you're right.
 
Here are the stated effects:
Effects: Copies elements in the range [first, last) into the range [result, result + (last
- first)) starting from first and proceeding to last. For each non-negative integer n <
(last-first), performs *(result + n) = *(first + n).
 
So, on a related note: is it valid to say int *n = 0[&otherInt];
@JohnDibling if it's a valid range then it should be standard code.
 
9:44 PM
Yes. That is the same as *(0 + &otherInt)
 
@JamesMcNellis I think that [NULL, NULL) is valid as long as they are the "same" NULL pointer (e.g. both the NULL pointer for int* and not one for int*, the other for char*)
 
@DanielTrebbien Yeah, I am convinced.
 
@willhelmtell: Yes, I think it is standard & you can rely on it. Because if you pass copy(0,0,xxx) then (last-first) = 0, so there can be no non-negative integer < 0
therefore, the pointer will never be dereferenced in a conformant implementation
 
@JohnDibling so is it a guanratee that copy() won't dereference if the range is empty?
 
@wilhelmtell if it dereferences, it's an ugly (and erroneous?) implementation. I can't imagine what perils you'd find in other parts of such implementation.
 
9:46 PM
@wilhelmtell It can't. If the range is empty there is nothing to dereference (you can't dereference an end iterator either).
 
@wilhelmtell: I believe so, because the standard says what the effects are and, by association, what the effects are not
 
@JamesMcNellis good one, true, so no algorithm may dereference an empty range. ok, so the check for null is redundant, like i thought.
awesome. :)
 
its an interesting question though, because nowhere does it say "nothing else will be dereferenced"
@james excellent point
 
this btw from an implementation of a string copy ctor in growing the buffer.
 
@JohnDibling No, just a null point. :-)
 
9:50 PM
Ha, I see what you did there
 
@wilhelmtell I don't think that you can omit the check that s is not NULL before performing the copy.
[6.5.6] paragraph 8 of the C standard states:
 
@DanielTrebbien mm?
 
> If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.
A NULL pointer does not point to an element of any array.
I think that you need:
//char *s = 0, *t = new char[newlen];
//int len = 0;
if (s != NULL)
    std::copy(s, s+len, t);
@wilhelmtell because NULL + 0 invokes undefined behavior.
 
@DanielTrebbien Ouch, I really don't want NULL + 0 to be UB :-(
 
C++ says:
> If the value 0 is added to or subtracted from a pointer value, the result compares equal to the original pointer value. If two pointers point to the same object or both point one past the end of the same array or both are null, and the two pointers are subtracted, the result compares equal to the value 0 converted to the type ptrdiff_t.
So, NULL + 0 is ok.
2
 
10:00 PM
@CharlesBailey Hah! C++ saves us.
 
Well, rather, T* p = NULL; p + 0; is ok, since NULL + 0 is really just the null pointer constant itself :-P
 
Oh @JamesMcNellis beat me. Was about to paste that (§ 5.7/7).
 
@JamesMcNellis You found it before I did, but that's what I was going to post.
 
Not sure about that. C++ also says (5.6/5 Additive Operators): "If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined."
 
/me is happy now.
 
10:02 PM
interesting question
 
It's the C programmers who must worry about that.
 
@JohnDibling in this case they don't point to anything.
 
Here is the full clause:
"When an expression that has integral type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integral expression. In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n) point to, respectivel
@jweyrich: That, taken in the context of the clause I just posted, suggests to me that NULL + 0 is UB
On the other hand: (5.7/7 Additive Operators)
"If the value 0 is added to or subtracted from a pointer value, the result compares equal to the original
pointer value. If two pointers point to the same object or both point one past the end of the same array or
both are null, and the two pointers are subtracted, the result compares equal to the value 0 converted to the
type ptrdiff_t."
 
@JohnDibling the standard seems to have a contradiction then.
 
So I guess, as with most things in C++, I just don't know. :)
@jweyrich: Wouldn't be the first time. I wonder if there's a defect report
 
10:09 PM
I don't think there is a contradiction
the original clause is the general case, and 5.7/7 clearly deals with 0 specifically
 
Someone ask this a few times and we have a FAQ: I've ported the following code from C++ to C but I seem to keep getting these small demons flying out of my nose: char * pzero = (char*)0 + 0;
 
why would anyone port code from C++ to C?
just compile it with an extern "C" declaration and call it from C, if you need that
 
@jweyrich I don't think so either. The "; otherwise the behavior is undefined" I take to apply to the case where the operand and the result don't point to elements of the same array (or one past the same) but the whole sentence is still talking about the case "If the pointer operand points to an element of an array object..." that the whole paragraph is about.
A null pointer doesn't point to an element of an array object so nothing in that paragraph applies. Oh, apart from that first sentence about the type of the result.
@DeadMG </joke>
 
what is this <joke> tag
actually, I'd much rather have
{ joke j; }
don't like html
 
@CharlesBailey I meant considering that @JohnDibling had a point about the "otherwise, the behavior is undefined.", as I said earlier that NULL points nowhere, it doesn't fit that clause.
 
10:25 PM
I'm not sure I made myself clear, I was trying to explain why I think the ";otherwise" doesn't peel all the way to include NULL and why I, therefore, don't think there is a contradiction. Did I mess up?
 
@CharlesBailey SURE! But I forgive you :-)
 

« first day (87 days earlier)      last day (4855 days later) »