« first day (120 days earlier)      last day (4819 days later) » 
00:00 - 23:0023:00 - 00:00

12:02 AM
I was going to go to bed, but nooooo, you had to go a waft a fancy paper at me... which I will now consume...
 
¬_¬
 
12:29 AM
good read that
save it for arguing my case in the future :P
 
12:55 AM
I am currently in the process of rewriting and extending my FAQ on arrays. @James will be happy to hear that "Arrays are not pointers" is now written in bold letters :)
9
A: How do I use arrays in C++?

FredOverflowArrays on the type level An array type is denoted as T[n] where T is the element type and n is a positive size, the number of elements in the array. The array type is a product type of the element type and the size. If one or both of those ingredients differ, you get a distinct type: #include &...

Tomorrow I will finish the part on creating and initializing arrays.
 
@FredOverflow thanks. I might actually see the difference now
 
@Raynos The difference between what?
 
Pointers & arrays
 
In that case, the FAQ could help... hopefully :)
 
Should do. Does the FAQ also apply for C ?
 
1:09 AM
Mostly yes. I even mention C99 in two places :)
 
When I learn C++ should I "forget" what I know about C ?
 
Just ignore the static assertions and comparisons to std::containers and std::algorithms and you should be fine.
@Raynos That is a very good question.
 
I mean do people that go from C to C++ have a habit of using C when there are more obvouis C++ alternatives?
For example C arrays vs std::vector
 
Note that if your goal is to learn clear, modern C++, then my FAQ on arrays is a waste of time just now.
 
@FredOverflow i want to understand what the difference is between a C array and a C pointer.
Always thought they were the same.
 
1:11 AM
@Raynos Then my FAQ won't be a waste of time just now :)
 
Apparently that's a really trivial thing I'm supposed to know ;)
 
It is trivial. Famous example:
int a[] = {2, 3, 5, 7, 11, 13, 17};
int* p = a;
std::cout << sizeof(a) << std::endl;   // prints 28
std::cout << sizeof(p) << std::endl;   // prints 4
 
By what logic does overriding << on cout to output data to it make sense? I mean what's the logical correlation between bit shifting and "pushing" data to the stream?
 
@Raynos Nothing.
Using << for output is the most prominent abuse of operator overloading in the history of programming.
 
Oh that's lovely :P I expected there to be some clever comparison between bit shifting
 
1:14 AM
@Charles: Oh wait, was it you who insisted on an "arrays are not pointers" in bold letters?
I think I mixed you up with @James.
Sorry guys.
 
Nah, it was me.
I mean, maybe @Charles did too. But I'm pretty sure that's something that I would say.
 
Yeah, it was probably both of you.
 
Are the C++ debugging and error handling tools as "easy" to use as say C#. I've gotten to use to "error on line x" statements and having to debug "Segmentation Fault" is just going to be demoralizing.
 
What? Is someone suggesting that arrays are pointers again?
2
 
@Raynos This message kinda did @Charles ;)
 
1:19 AM
"thought" ! I'm correcting my ways.
 
Hence the "kinda" and the smiley ;)
 
I just wanted to defend myself.
 
@Raynos Yes, definitely. For example, arrays and pointers should rarely occur in modern C++ code, but it's hard to let go of them if you're a proficient C programmer. I know, it was a hard journey :)
 
I'm usually against singletons, but I think this one might be ok: twitpic.com/3z5x2p
4
 
Can you also make a joke about "I'm usually againts bad whiskey, but I think this whiskey thing used in C++ might be ok: <image>"
 
1:26 AM
I wanted to buy two, but I was prevented from doing so.
 
@JamesMcNellis I have made the exact same joke at least once in this channel. You can probably find it if you search for "expensive brain surgery" ;)
 
Do you guys also think that C++ should be used instead of C in almost all cases. I'm curious whether it's a common opinion.
 
@FredOverflow Oh; sorry to re-joke. I was amused when I found it at the store today and couldn't resist buying it.
 
Sadly, every time I want to read "Modern C++ Design", I notice that it wastes almost 30 pages on the Singleton pattern, and I immediately put the book away in disgust.
@JamesMcNellis Oh, you did actually buy it? How is it?
 
@Raynos Choice of language depends on many things. Most important is the experience of the team with the language. Most other considerations pale in to insignificance.
 
1:30 AM
Oh, you mentioned it like last week even. I should pay more attention!
@FredOverflow I don't know yet; I just got home with it. I'll let you know.
 
@CharlesBailey from a large company point of view you might want to choose the language suited for the task and build a team with the correct skills afterwards.
 
@JamesMcNellis I was thinking about buying it for a friend. By the way, would that even be possible, now that you have already bought it? ;)
 
@Raynos I find it hard to imagine a realistic situation where this would hold. Most tasks can be done in many languages; the suitability of a language that no-one has experience of is unlikely to be well judged. Putting together a team is hard and time consuming.
 
@CharlesBailey Unless the team only has one member. I call that the "Singleton team pattern".
 
@FredOverflow No space for team mis-communications.
 
1:37 AM
@CharlesBailey Right, especially if one likes talking to oneself... :)
 
@CharlesBailey I miscommunicate my intentions to the compiler all the time, even when I'm working alone.
 
@JamesMcNellis So you consider the compiler part of your team? :)
By the way, brand new video on STL internals.
 
@FredOverflow I do talk to it a lot; usually it involves me cursing at it though.
 
Would the concept of a learning compiler, which picks up on corrections of code between multiple successive compilations and then attempts to learn which are "common miscommunications" and suggests alterations be a bad idea? You could avoid mis communications with your compiler.
 
@Raynos I think it would be a terrible idea, because it would make programs a lot less portable.
 
1:47 AM
@FredOverflow Oh the compiler doesn't fix your code, it just points out the mistake for you to fix.
but it does so from learning your particular set of common mistakes.
 
I see. Hm, doesn't sound too bad, then.
 
 
3 hours later…
5:14 AM
In the direction of getting this lounge back on track (namely off-topic), consider:

* If you pick a good C++ programmer at random, most likely female or male?
* If you pick a good board member at random, most likely female or male?
* If you pick a good scientist at random, most likely female or male?
* If you pick a good mathematician at random, most likely female or male?
* If you pick a good guitar player at random, most likely female or male?
If the answers all point strongly in some particular direction, is the cause of that mainly

(A) discrimination or
(B) cultural or
(C) innate differences between the sexes?
 
5:31 AM
(D) Your random number generator doesn't produce a uniform distribution.
 
 
2 hours later…
7:27 AM
hi
 
@Raynos - C++ should be preferred over C except for systems programming where c++'s implicit code generation and forced usage of exceptions render the language unsuitable.
kernel mode programming typically requires execution in environments where compiled code must be placed in memory sections with specific properties - code executed to handle an interrupt must be in non paged memory for example, as paged memory triggers interrupts.
Data structures need to be explicitly placeable too as the kernel mode stack (during interrupt handling) is small and non growable, and the normal heap likewise is pageable.
c++ does not have any mechanism to allow implicitly generated code or data structures to have a specific memory placement.
while specific calls to new can be (std::nothrow), none of the implicitly owned objects will honor that, the STL explicitly will not work with a non throwing new.
 
 
2 hours later…
sbi
9:33 AM
@ChrisBecke What do you mean with "implicitly generated code or data structures"?
@ChrisBecke The STL can use customized allocators all over, so using something else than the "normal" new is no problem at all. But the question is what you do in your OS kernel when you run out of memory?
 
 
2 hours later…
11:46 AM
What is the correct term for a "global" variable in C++?
 
12:12 PM
@FredOverflow What do you mean by global? :P
 
@CharlesBailey Right above main, for example :) I believe it is called "at namespace scope" or something?
 
Do you mean a variable with potential global namespace scope or just a variable referring to an object with static storage duration?
 
The former.
I don't mean a local variable with the static keyword.
 
"A name declared in the global namespace has global namespace scope (also called global scope)." 3.5.4 [basic.scope.namespace]
"The potential scope of such a name begins at its point of declaration and ends at the end of the translation unit that is its declarative region. Names with global namespace scope are said to be global."
Although this is looking at things from a scope point of view. Most people would only think of the object as "global" if it also had external linkage.
 
12:35 PM
@Charles: Okay, let me rephrase: A variable that is neither a local variable nor a data member is "a variable at namespace scope", right?
 
@FredOverflow If it doesn't have block or class scope then it has namepaces scope (or function prototype scope).
By block, I mean local, of course.
 
12:57 PM
As promised yesterday, the FAQ on arrays has grown :) Let the bug reports come in!
0
A: How do I use arrays in C++?

FredOverflowArray creation and initialization As with any other kind of C++ object, arrays can be stored either directly in named variables (then the size must be a compile-time constant; C++ does not support VLAs), or they can be stored anonymously on the heap and accessed indirectly via pointers (only the...

 
sbi
1:10 PM
@FredOverflow "Initialization of the array's elements is performed in ascending order of their indexes"?
@FredOverflow "Static non-POD arrays are initialized the first time the flow of control passes through their definition." When would control flow ever pass through an array at namespace scope?
I like how you link to specific SO questions and answers. However, linking "uniform initialization" to >2hr talk of Bjarne on youtube is somewhat of a link bomb there. I wonder if there isn't a question on SO to link to?
 
oh hai
 
sbi
@FredOverflow The link of "undefined behavior" when using delete instead of delete[] links to a question that got closed as a dupe. You might want to link to the one it was closed as a dupe of.
@FredO: In general, I wouldn't consider a treatment about dynamic (anonymous) arrays complete without a hint at std::vector being what you should normally use for this in C++.
@FredO: The way I learned the graphical pointer notation the arrow starts with a dot in the box:
        ^
        |
        |
      +-|-+
      | o |
      +---+
:)
Anyway, my critique notwithstanding, I consider this a very valuable addition. +1 from me.
 
1:40 PM
@sbi: Thanks for the feedback. I will incorporate it in a couple of days, but first I need a little break from arrays ;)
 
sbi
@FredOverflow I can do it if you want. Just point out which ones you agree with.
 
@sbi Nooooo, it's my baby ;)
Do we have a good question/answer that explains the C declarator syntax?
 
sbi
@FredOverflow I don't think so.
 
So who wants to write one? :)
 
sbi
BTW, since this one came up, I'm thinking about an FAQ explaining when to use ::, ., and ->, which is one thing my students (coming from Java) always had a very hard time with at first. What do you all think about such an FAQ entry?
 
1:51 PM
Yeah, I also found the distinction between :: and . confusing at first. Damn you, Java! :)
@sbi Could you add an extensive answer to an already existing question?
 
sbi
@FredOverflow We've been there. I'd really like for FAQ questions to have the most exhaustive and factually correct answer to be accepted. With an existing question, that's really hard to do, because you would have to persuade the user who asked the question to change whichever answer they already have accepted.
 
> no point in repeating the ordinal numbers that enumaration items are already marked with
I agree, thanks for the edit.
By the way, if you look at the edit, it enumerates up to 7 :)
 
sbi
@FredOverflow I thought this would be more hassle to explain than to just change, and you could always roll back if you disagree.
@FredOverflow Yeah, I saw that. :)
 
Is Nicholas Stroustrup a son of Bjarne?
I stumbled upon the name a few days ago and had never seen it before.
 
@FredOverflow "Unfortunately, C++03 provides no means to initialize arrays in the member initializer list" is incorrect. You can default-initialize an array.
 
2:03 PM
@AlfPSteinbach Really? Cool! Thanks.
 
sbi
@FredOverflow I think so. At least he seems to know enough of C++ to review C++ books. (When I was sent a preliminary copy of Meyers' 3rd edition of Effective C++ for review, he was on the CC list.)
 
@FredOverflow You're welcome. Also I think "For no particular reason, arrays cannot be assigned to one another." has a reason, namely decay to pointer type in operator= expression. Just because = is old C (C semantics).
 
> member1 = *(new Foo());
Now this is an abomination I have never seen before :)
2
Q: C++: Difference between using the new keyword vs not when instantiating class members?

JoeyFor a programming assignment, we are given a template class with two members declared not as pointers, but actual objects: Foo member; In the constructor, I tried member = *(new Foo()); initially, but learned that, at least sometimes, it was copying the new Foo object, and therefore causing me...

 
sbi
@FredOverflow Unfortunately, I have.
 
@FredOverflow sorry, i meant "value-initialize". default-initialization was in C++98. C++03 is value-initialization. or wait. I haven't had coffee yet. look that up!
 
2:10 PM
@AlfPSteinbach noted.
I always get confused between zero, default and value-initialization :)
 
sbi
0
Q: When do I use a dot, arrow, or double colon to refer to members of a class in C++?

sbiComing from other C-derived languages (like Java or C#) to C++, it is at first very confusing that C++ has three ways to refer to members of a class: a::b, a.b, and a->b. When do I use which one of these operators? (Note: This is meant to be an entry to Stack Overflow's C++ FAQ. If you want...

 
@sbi Wow, you are fast :) +1 for you.
 
2
Q: When do I use a dot, arrow, or double colon to refer to members of a class in C++?

sbiComing from other C-derived languages (like Java or C#) to C++, it is at first very confusing that C++ has three ways to refer to members of a class: a::b, a.b, and a->b. When do I use which one of these operators? (Note: This is meant to be an entry to Stack Overflow's C++ FAQ. If you want...

 
I think some real example code would help.
What kinds of posts are fed here automatically, again?
 
sbi
@Feeds Oh, right, I forgot about that!
 
2:18 PM
> Next scheduled event: "C++ IS AWESOME"
LOL? :)
 
sbi
@FredOverflow I don't think so. It's exactly the fact that, just by looking at a->b, you already know a lot about a and b, that I'm trying to get across. However, feel free to answer in whatever way you'd prefer. I might agree once I've seen yours and accept it. :)
@FredOverflow That was a test by, I think, @James, who wanted to find out about this event feature. I don't think we found out much about it, though. :) (Except for one bug, that is.)
 
Easy rep, I just repeated the comment in the OP's code. :-)
5
A: c++ pass by reference?

Alf P. SteinbachThe root pointer is passed by reference since its value can change in the case where the tree is empty. Cheers & hth.,

 
@AlfPSteinbach do you know jave?
 
@Jinjavajin jahve is the israeli god. he used to display burning bushes and other simple illusionist things (one really great thing was when he "split" an ocean). but he's been silent for a few thousand years, probably dead.
 
@sbi Yes, I'm planning to add a fifth part to the FAQ that shows how std::array and std::vector makes things a lot easier and safer.
...also, 15 pages of dense information on arrays isn't enough already, right? :)
 
2:26 PM
@AlfPSteinbach sorry,do you know java?
 
@Jinjavajin I was just about to ask who "Jave" is :)
 
@Jinjavajin i used to
 
@AlfPSteinbach lol :P
 
@AlfPSteinbach LOL love the copy&paste from the comment :)
 
@AlfPSteinbach can i ask a doubt?
 
2:28 PM
@Jinjavajin sure
 
@Jinjavajin Java does not have pass by reference, in case you intended to ask.
 
@AlfPSteinbach ok,if i create a chat program to chat with you with a client and a server,where should the server reside?
 
@Jinjavajin outside of any firewall. that presents a problem for e.g. China. sorry, no, wait. have it inside a firewall. your own firewall.
 
@AlfPSteinbach can it be in my pc or in a remote pc?
 
@Jinjavajin any machine connected to the net
@Jinjavajin i can't see the connection to Java. but you might look up old IRC protocol, and also the general protocol (much newer) used by GMail chat. no need to reinvent the wheel.
 
2:38 PM
Reinventing the wheel isn't so problematic. It's just round, and stuff.
 
What's more important than a wheel?
David John Wheeler FRS (9 February 1927–13 December 2004) was a computer scientist. He was born in Birmingham and gained a scholarship at Trinity College, Cambridge to read mathematics, graduating in 1948. He completed the world's first PhD in computer science in 1951. His contributions to the field included work on the EDSAC and the Burrows-Wheeler transform. Along with Maurice Wilkes and Stanley Gill he is credited with the invention of the subroutine (which they referred to as the closed subroutine), because of which jump to subroutine instruction is often called Wheeler Jump. He was...
(He invented the subroutine.)
Wheel, Wheeler, Wheelest, in case you didn't get the joke ;)
 
Wheelers is a science fiction novel authored by English mathematician Ian Stewart and reproductive biologist Jack Cohen, figures notable for both their personal scholarly work and numerous individual and collaborative contributions to the world of science fiction. The book was originally released in hardcover form in the year 2000, and a more common paperback printing was begun in 2001. It has enjoyed modest commercial success and is perhaps best known for its intriguing conceptions of alien zoology and intelligence—hallmarks of Cohen's renown work as a consultant on exobiology for books, ...
 
lol
 
@AlfPSteinbach Woah, 200 pages... that is going to be an interesting read!
 
@FredOverflow i think but not sure that those two also wrote hilarious book about a religious war. the bombs had built-in electronics that blessed the receivers.
 
2:47 PM
@AlfPSteinbach Actually, I was referring to the technical report on C++ performance :)
Apparently, "Wheelers" is twice as long.
 
Heaven is a science fiction novel written by Ian Stewart and Jack Cohen. It was first published in 2004.
(Not sure if that was the bless-you bomb book, possibly i'm confusing it with some other book)
 
@AlfPSteinbach I think the holy bomb originates from the game "worms" ;)
 
user379888
Hello,I am upto making a signin/signout attendence system online and I am in need of good ideas how to implement it.Please help me out
 
user379888
What should be the idea of my software?Should it be a client software?
 
@sbi - you don't - when doing OS kernel programming - run out of memory.
@sbi - also, ive been assured by ppl in this forum, that its not valid to pass a custom allocator to the SL that returns null, rather than throwing an exception.
@sbi - default class members are implicit. As well as the "real" code that invokes operator new. vtables are implemented as a pointer to an implicitly generated and placed vtable. RTTI's implementation is not required to place the RTTI in the same place as the allocated blocks.
 
2:58 PM
> a->b is, originally, a shorthand notation for (*c).b.
@sbi: You mean (*a).b, right? ;)
 
sbi
@FredOverflow Dammit! I see how this escaped my search pattern...
Thanks. Fixed now.
@ChrisBecke Well, so that's one thing you don't need to worry about then. :)
@ChrisBecke Which is exactly why I asked what you do when this happens.
 
@sbi: The line break in operator-> is unfortunate :-/
 
You don;t run out of memory doing kernel os programming, because (a) drivers have access to heaps where the amount of memory available is known. new and delete do not have a function that returns the amount of available memory in that freestore.
 
sbi
@FredOverflow Yeah, and also absolutely arbitrary. Although I can change line breaking by changing the zoom factor in FF, I can't seem to get one where it breaks at this point. To quote that idiot: There's Nothing I Can Do.
@ChrisBecke IOW, you don't use new and delete, you allocate your memory using other mechanisms. Fine. So why wouldn't you be able to do this in C++?
 
and (b) by allocating fixed sized structs that are associated with specific io requests, the amount of memory used by drivers grows and shrinks only withthe number of outstanding async requests a driver is handling.
 
3:04 PM
@sbi I think he's complaining about raw pointer result of new
 
sbi
Also, I assume you don't have that many dynamic data structures in kernel programming. Still, the STL, for example, is mainly a concept, and you can employ that concept just fine, even if you don't want to employ any of the std lib's containers.
 
because using char[256] in C++ isn't c++. Its doing C in C++
 
@Chris: you can sort of force use of your own suitably typed NEW, for a given class or hierarchy of classes, by overloading operator new (allocation function) so as to completely obfuscate and make completely impractical direct use of new expression
 
sbi
@ChrisBecke For one, that's wrong. But even if we put away with this: just use `std::array<char,256>.
 
why the hell would I make a simple raw array so complicated to use?
 
sbi
3:11 PM
@ChrisBecke Because you considered char[256] "not C++ enough"?
 
I just don't consider it c++. its ideomatic c. in a .cpp file. Which is possible, but why bother with a .cpp file then?
 
sigh... have serious programming distractions. I really should in my windows OS working on my third year project with openCV... instead I find my self arseing around on my ubuntu OS playing with basic openGL and SDL stuff... project is not going well
 
Sorry to jump in the middle of a conversation without even knowing what is being discussed... but the fact that something can be done in one language and is idiomatic in that language, does not mean that it has to be avoided or that it is not the best solution in another language. For the absurd counter example, ++i is probably idiomatic in quite a few languages
 
@ChrisBecke but their are so many things in C++ that are just the same as in C... by your logic, because in C you have declare variables at the start of a function, then when writing C++ it would be 'wrong' to do so.
 
@thecoshman Well, char[256] is not one of those things that are good for both C/C++.
 
3:23 PM
@AmirRachum How's that? How do you store 256 characters in an auto variable?
 
@DavidRodríguezdribeas std::array<char, 256> v;
 
@JamesMcNellis beat me to the punch
 
darn... I knew someone would come up with that... Let's say in C++03
 
@AmirRachum yes, I question your reasoning their. If I know at compile time that in a certain scope I want an array of 256 chars, then char[256]; is the way to go? it may not be using STL, but what's wrong with that? or am I not understanding something here?
 
The array template is about 100 lines of code and really easy to implement. If I was stuck with a C++03 STL implementation and couldn't use Boost, I'd implement my own.
2
 
3:25 PM
(boost::array, of course, but that is hardly as idiomatic as char array[256]) Just a simple question how many here have used that construct (C style array) in real code?
 
@DavidRodríguezdribeas you better ask how many have not used it. might as well ask how many have not used int
 
@thecoshman array is safer, provides iterators and is generally more comfortable.
 
I would more or less always write arrays as type[size]; unless I know I want to do things like add data at runtime, so I would go for a vector
 
I use it consistently. C style arrays are an absolute beating to use: they aren't assignable, they are the same thing as pointers, they are impossible to pass by value, ok, I was just kidding about the pointers thing. An array class template performs just as a C style array but provides a container interface, copyability and assignability, iterators, better runtime checks if your implementation does that sort of thing, and (IMO) is thus easier to use.
 
@AmirRachum yer, see thats where I probably need to up my standards, I tend to use a for loop and let that imply the index. I don't really think of using iterators when traversing an array... always felt a bit overkill, but then I gues it means I can simply swap out the underlying data storage can't I
 
3:30 PM
@thecoshman Exactly. Also, you can use STL's algorithms like for_each, sort, etc.
 
@AmirRachum You can use those algorithms on C-style arrays.
 
@JamesMcNellis Really? I never even thought to try.
 
@AmirRachum Yes. Pointers into an array are usable as random access iterators. They can be used with any algorithm.
 
@JamesMcNellis Stop doing that!
 
@JamesMcNellis Never though of that. Nice.
 
3:33 PM
@CharlesBailey Heh
 
int array[20];
int array2[20];
std::copy( array, array+sizeof(array)/sizeof(array[0]), array2 );
 
@AmirRachum ooooh, I do like the for_each construct :P
 
@DavidRodríguezdribeas It's... kinda ugly.
No offense :P
 
@DavidRodríguezdribeas I like the std::array version better:
 
Which of the following is easier to grasp at first glance:
std::copy( array, array+sizeof(array)/sizeof(array[0]), array2 );
array2 = array;
 
3:34 PM
std::array<int, 20> a;
std::array<int, 20> b;
b = a;
@JamesMcNellis The latter, of course.
 
For working with raw arrays, best to define Dietmar's three access functions (link is to one of my implementations)
 
Also, std::array does not do that stupid array-to-pointer decay. If you want a pointer, you must be explicit about it (which is a good thing).
 
I wrote some time ago basic utils in an "array" namespace:
template <typename T, std::size_t N>
T* begin( T (&a)[N] ) {
return a;
}
template<typename T, std::size_t N>
T* end( T (&a)[N] ) {
return a + N;
}
Then use:
std::copy( array::begin(array), array::end(array), array::begin(array2) );
@JamesMcNellis You already know :)
 
@DavidRodríguezdribeas Do you also have array::size? :)
 
yep
To be honest, I hardly ever use them, though
 
3:37 PM
Can I see the function declaration?
 
@FredOverflow see the link i posted (as best I know those three functions were identified as necessary support by Dietmar Kuehl; also, begin and end, but not size, is in C++0x)
 
...because I just love passing arrays by reference without giving them a name :)
I mean look at T(&)[n], how sexy is that? :)
 
for size()? There are two versions, compile time and runtime:
template <typename T, std::size_t N>
std::size_t size( T(&)[N] ) { return N; } // not-compile time constant
template <typename T, std::size_t N>
char (&size_impl( T (&)[N] ))[N];
#define ARRAY_SIZE(x) sizeof( size_impl(x) )
 
@AlfPSteinbach What is Size?
 
@FredOverflow A typedef for ptrdiff_t. That's the proper result type for a sizeOf function. The standard, however, is mired in backward compatibility, essentially for supporting the i8086 processor...
 
3:41 PM
The second can be used as a compile time constant:

void foo() {
int x[100];
//...
int copy[ ARRAY_SIZE( x ) ];
};
(And there is a bug in the posted code, if you consider that there is an array namespace)
 
@AlfPSteinbach The second bullet point says:
 
@DavidRodríguezdribeas You forgot to support array size for array of local type for C++98/C++03. Increasing the number of size functions to three.
 
> make sure that you’re only using signed arithmetic in expressions that involve unsigned type numerical values.
Shouldn't that be signed there?
 
user379888
Can someone please help me out in deciding either I should make a web-app or a client-server app? I am trying to make a sign-in/sign-out attendence system
 
@FredOverflow no :-)
 
3:44 PM
Just wondering, did anybody try out the Intel OpenCL SDK alpha?
 
@AlfPSteinbach So you suggest casting to signed before doing the arithmetic?
 
samples here do not work in debug mode, and some seem not to work at all
 
@AlfPSteinbach Interesting, never hit that problem before... Can you make a typesafe implementation of 'size' for arrays of local type?
 
@FredOverflow i am (or was) instead suggesting avoiding all that rigmarole.
@DavidRodríguezdribeas needs to be macro
 
The simple solution (not typesafe) would be the nasty macro that I used above: sizeof(array)/sizeof(array[0]), but that is error prone as it can be applied to a pointer, the compiler will gladly digest it but will produce weird results...
 
3:47 PM
or with open cl in general?
 
@AlfPSteinbach Hm, I still prefer unsigned in many cases, because overflow on unsigned types is well-defined (the extra bits are simply ignored), whereas overflow on signed types invokes undefined behavior...
 
@DavidRodríguezdribeas i think possibly you can check type of address of argument
@FredOverflow that's precisely the reason not to use unsigned type for numbers. with automatic promotion you get silliness like s.length() > -5 almost always false...
hey
 
@AlfPSteinbach You mean false, right? I don't think any string is that long :)
 
unsigned types are a very very cheap substitute for precondition checking
dont do it
 
3:51 PM
@ChrisBecke So is any other type. Should I just pass void stars around from now on? ;)
 
@FredOverflow the thing is, in C++ they don't do checking. the standard guarantees that you wil not get any checking. but in Pascal they do. so habit from Pascal is direct opposite of practical in C++
 
@AlfPSteinbach I don't get what you're trying to tell me.
 
@FredOverflow Francis Glassborow once remarked that the nice thing about syntax coloring is that you can set comments as white on white. implicit in that, that comments get stale and are generally misleading: programmer adds comment for code that is difficult to grok without the comment, comment may not reflect code. similarly for unsigned types used for numbers: in C++, as opposed to Pascal, the info suggested by the type is just a comment, the standard guarantees that there's no checking
 
no, what Im saying is, always use a signed type thats large enough to not overflow in your domain.
unless you are specifically doing modulus arithmatic
 
@FredOverflow if you want a suggestive name, just use a typedef, not unsigned
 
3:57 PM
@AlfPSteinbach are you saying that if I have data that wouldn't make sense having a negative value, I should still use a signed value?
 
@AlfPSteinbach Okay so you're complaining about the implicit conversion from signed to unsigned? In that case, I agree with you.
 
@thecoshman yes
 
@thecoshman and ill second that
 
I've been trying to understand what you've been saying about this, but can you explain nice and simple for me please?
unsigned data types are bad...
 
The are not really bad (and I do use them often), the problem is that integral promotion of signed/unsigned types might differ, and you have to be aware of what you are doing... as an example, with "unsigned a, b; ... assert( a-b <= a );" the assert may fail, in particular if b>a
 
4:01 PM
unsigned types are bad because they don't do what you expect.
 
@DavidRodríguezdribeas This hurts my head... how can that fail?
 
@DavidRodríguezdribeas On the other hand, a - b will invoke undefined behavior for signed types if the result overflows :)
@thecoshman Just put in a=0 and b=1. What is the result of a-b?
 
@FredOverflow yes, and that's good :-) and is very rare case
 
whereas the defined overflow is better?
 
Hint: It is not -1, because there is no such thing as -1 in unsigned types.
 
4:03 PM
theres still no warning that your bank balance is now wrong
 
oooh..... like if a = 0 and b = 10, a - b = -10 (should do) but it overflows to like 4 billion or something
 
@thecoshman Correct
@ChrisBecke How is something well-defined not better than nasal demons?
 
@FredOverflow Much better! Then you can create a new question in SO and get language-lawyers vs. pragmatic programmer fights! UB is such a great invention
 
because 4 billion is worse than undefined.
 
but if I wanted to ensure that I was only passed a positive (or zero) value, I could use an unsigned int as the parameter and (in theory) not have to code a check for a negative value
 
4:05 PM
so, what if i wanted to pass a number than is only 1-100 then?
whats so special about 0, and negative numbers
 
@ChrisBecke I can hardly agree that anything is "worse" than undefined. For starters, you do know the answer, while in UB you might even get the same result today but not tomorrow
 
Can't we just use double for everything and stop whining? ;)
 
well (a) its undefined because c++ is implemented on platforms that don't use 2s complement notation for negatives.
 
(just kidding!)
 
but youre not really going to encounter those, so go ahead and expect the result to be defined.
(b) just because the result is defined, doesn't help you catch it.
and "Defined" doesn't mean meaningful
 
4:07 PM
@ChrisBecke well, obviously in that case you are going to have to just bounds check it. Which fair enough you should do any way.
 
@FredOverflow Perfect. I vote for the double solution, then not only we won't know what happens with overflows and underflows but we will not even know if two values are the same! I like that
 
which means I don't need to worry about the spectre of undefined behaviour anyway
 
What about the python evergrowing int? Just have integer types that never overflow, they "upgrade" to a bigger type
 
just because the c++ standards committee doesn't make my bank manager any happer when his statement tells me the bank owes me 4billion
 
@DavidRodríguezdribeas All integral values up to 2^53 can be represented exactly in an IEEE 754 double, no inaccuracies whatsoever.
(Yes I know, C++ does not mandate IEEE 754. Also, I was kidding.)
@DavidRodríguezdribeas Haskell's Integer type does the same, I like it.
 
4:11 PM
I use unsigned types quite frequently.
 
its not particularly hard to use templates to make a "biginteger" type
why it isnt in the stl i dont know
 
@DavidRodríguezdribeas As a practical matter, the UB for signed overflow is either useful (seldom, because of the prevalence of two's complement representation) or well defined by the compiler (in practice always). So your argument about UB being a negative is based on talking about irrelevant generalization. Which is fallacy, sorry.
 
@AlfPSteinbach Either you are referring to someone else, or I have been misunderstood... I did not even know that the overflow was undefined. I have made a comment that anything is better than undefined, since among other things, undefined might produce the same result as the "worse than undefined" option
 
@DavidRodríguezdribeas huh
@DavidRodríguezdribeas i don't understand how you could talk about undefined, <<I can hardly agree that anything is "worse" than undefined>> if you <<did not even know that the overflow was undefined>>. Sounds confused to me. Again, sorry, but I can't make sense of it.
on the bright side, there's nothing that enough communication can't straighten out
 
Ok, Chris made a comment above: "4 billion is worse than undefined", so I commented that nothing is worse than undefined since undefined can be anything, including 4 billion, that on one side
 
4:26 PM
@DavidRodríguezdribeas yes, ok. the nice thing is that it isn't really undefined. the overflow for signed is just formally UB. that formal UB is there to allow trapping. however, nearly all extant computers (the one exception being Unisys) use two's complement and no trapping. so well-defined by the compiler. namely, wrapping, not trapping.
 
Then, on your comment about UB for signed overflow, I just said that before FredO. mentioned it here, I had not known/considered that issue
 
sorry for getting caugt up ina bit of hyperbole.
my point is, simply, that the result of undefined or defined overflows that are not trapped are pretty much the same.
a whole heap of shit from the guy who commisioned the software.
and is not buying the line that 4 billion on an account that should not go negative, but somehow did, is ok, because at least its "defined"
 
4:49 PM
@ChrisBecke No, my position is not that it is a better result, only that it is better to debug. Assuming UB was proper UB, that is a fully random number (which does not seem the case here), seeing in the system a pure random number is way harder to debug (what part of the algorithm went wrong to produce 42?) than an unwanted but defined behavior: 2^32 minus a small amount is almost crying out that the number has wrapped around in an operation, so it is probably easier to debug.
Of course, a trap would be better, as it would fail faster, and it will be easier to debug
 
UB is never "proper UB".
and is normally quite deterministic on a particular compiler / build environment
so, pragmatically speaking, determining the result of an undefined behaviour simply requires working knowledge of how the underlying platform implements the various data types the c++ abstractions wrap
 
5:05 PM
@DavidRodríguezdribeas here's one way to check for array-ness in macro:
#include <typeinfo>
#include <iostream>
using std::cout;
using std::endl;

int main()
{
    int a[1];
    int* p;

    cout << std::boolalpha;

    cout << (typeid(*&p) == typeid(p+0)) << endl;
    cout << (typeid(*&a) == typeid(a+0)) << endl;
}
but it only works at run-time
thinking...
 
I think that can be made to work with decltype in C++0x, but then again, local types can be passed into templates in C++0x, so that would not add any value, the other solutions should work.
 
@DavidRodríguezdribeas Here's best that I can do for C++98 (where you can't template on local type), but it's only runtime check:
#define COUNT_OF( a ) \
    (assert( typeid(*&(a)) != typeid((a)+0) ), sizeof(a)/sizeof(*(a)))
 
:) It will at least cry out for help during testing
 
was this a FAQ thing?
 
5:35 PM
so why doesn't the stl have a 'infinite' width integer?
 
@ChrisBecke IMO, much the same as why it doesn't have a practical fixed point type (it has number types for scientists and engineers, but not for bankers). namely, it's just a hodge-podge collection of really basic stuff. i think, whatever was available and seemed to be in much demand.
 
don't go calling the STL hodge-podge
ill start questioning its design.
 
@ChrisBecke no, the STL is different. it was adopted almost whole-sale (Bjarne teamed up with Stepanov to do that). and it was at that time very nicely fleshed out, designed from scratch by one man (Stepanov).
Hm, funny how Bjarne's forename is the one I think of first, while I can't even remember Stepanov's forename.
 
a man with one flaw. a complete misunderstanding of what any other science discipline might interpret vector to mean
 
well, i think the mathematicians got it wrong. :-)
 
5:48 PM
lol
 
actually, i was horrified to discover that mathematicians have redefined the term "number system", so that henceforth we have to say "numeral system" where we previously said just "decimal number system" or such. I refuse!
(Hue & Cry, "I refuse!", popular at piano bars when I was student, like 1986)
heh, version with orchestra -- very very 80's :-)
re earlier macro of mine, that *& was just silly: who ordered that?
#define COUNT_OF( a ) \
    (assert( typeid(a) != typeid((a)+0) ), sizeof(a)/sizeof(*(a)))
 
6:14 PM
harro i ams chinese
 
@AlfPSteinbach Alexander, FWIW.
 
@JerryCoffin rhx!
@allthewayapps how do you do? I'm Norwegian
@JerryCoffin i meant, thx!
 
6:33 PM
@AlfPSteinbach Surely....and Rurely.
 
 
2 hours later…
8:08 PM
@AlfPSteinbach why +0 and * instead of [0] in both?
ah, the +0 is for pointers
 
or arrays
:P
given that they're the same, but different
 
8:33 PM
> Remember that C puts its parameters from right to left on the stack.
That's not true, is it?
 
sbi
@DavidRodríguezdribeas Yes, I also have that in my toolbox. ISTR that this will be in the std lib in C++1x?
 
8:50 PM
@FredOverflow that is true. leftmost parameters are first on the stack and for varargs remember you pass the rightmost named parameter (which is just to the left of the ellipsis) to va_start
@FredOverflow: also I've heard from more than one source that various functions, like from stdio, are optimized so the rightmost parameters don't have to be popped and repushed
 
@FredNurk they're probably optimized but the reuse of stack space is performed by any competent compiler. i know that g++ does this. it just reserves a suitable good chunk of stack and then for a sequence of calls just emplaces parameters in that block, no time wasted on push and pop and stack pointer correction (for args)
@FredNurk the +0 is just a way to force decay to pointer type, if isn't already pointer
 
0
Q: What does "cdecl" stand for?

FredOverflowYes, I know that "cdecl" is the name of a prominent calling convention, so please don't explain calling conventions to me. What I'm asking is what the abbreviation (?) "cdecl" actually stands for. I think it's a poor naming choice, because at first sight it reminds one of "C declarator" (a rathe...

@FredNurk Does the C standard even use the word "stack"?
 
@FredOverflow "c declare" like "i declare this function to be employing the c calling convention"
 
@AlfPSteinbach Will you stop editing your answer already, it makes an unnerving sound every time you do it ;-)
 
he he :-)
 
9:03 PM
The acoustic version of a bump :-)
 
@FredOverflow why does it matter if the C Standard uses the word "stack"?
 
@FredNurk Because it does not make sense to say that "C puts its parameters on the stack from right to left" if there is no such thing as a stack in the C programming language.
God I love playing with cdecl.org :-)
 
@FredOverflow yes, it does make sense. it does not make standardese
@FredOverflow standardese leaves many things, perhaps most, up to the implementation
 
10:05 PM
@FredOverflow LOL, 4 deleted answers already :)
 
10:27 PM
am I missing something here? stackoverflow.com/questions/4987016/…
or is he just over-reacting because he hates that I used the words "bad example"?
 
00:00 - 23:0023:00 - 00:00

« first day (120 days earlier)      last day (4819 days later) »