« first day (298 days earlier)      last day (2555 days later) » 

4:17 AM
can anybody pls tell why sizeof returns 1 when applied to boolean expression
like in this:
int a = 9, b = 9;
printf("%d\n", sizeof(a==b)); //prints 1 not 4
while as per the documentation sizeof doesn't evaluate expressio
n
 
3 messages moved from Lounge<C++>
@cbinder sizeof(a==b) == sizeof(bool), which is 1 on your compiler
if you expect it to be the same as sizeof(int), use a C compiler
also %d expects an int, not std::size_t
so your code has UB
 
thanks @milleniumbug
 
 
8 hours later…
11:52 AM
hi
why are C style casts harder to read than C++ style casts?
 
nwp
because they can do more and are therefore less precise
 
harder to grep for, unnecessary parens
often requires such usage ((fucking_type)(just_fucking_kill_me_now()))
also don't use C style casts in general
pass -Werror=old-style-cast and remove as soon as one appears
 
12:27 PM
I have one more question. Which one looks better:

switch (type) {
    case A:
    {
        // variables go here
    }
        break;
    ...
}

or

switch (type) {
    case A:
    {
        // variables go here
        break;
    }
    ...
}
 
switch is garbage in C++ which is why I don't use it, but if I have used it, it would be the latter
 
I am getting this error .
C:\Users\Windows\Desktop\New folder\character recongnition.cpp|63|error: 'it' does not name a type|
 
2 messages moved from Lounge<C++>
@IccheGuri please post the relevant code
 
I have this code :
map<pair<int,int>,int> M;
for(auto it = M.begin() ; it!=M.end() ; it++)
        {
            if(it->second > maxi)
            {
                P = it->first;
                maxi = it->second;
            }
        }
 
Enable C++11 mode with -std=c++11
 
12:33 PM
how can I do that ?
 
@IccheGuri use *it instead of it, if you want to refer to std::pair
 
what will be the code ?
 
if you're using the compiler from the command line, pass -std=c++11 as a flag
if you're using an IDE, search for the appropriate option in the project settings
 
I am using codeblocks
 
then the latter applies to you
 
12:35 PM
map<pair<int,int>,int> M;
for(auto it = M.begin() ; it!=M.end() ; it++)
        {
            pair<int,int> myPair = *it;
            if(myPair->second > maxi)
            {
                P = myPair->first;
                maxi = myPair->second;
            }
        }
@IccheGuri check this code ^^^^^ I haven't tested it yet, because I didn't know how to
 
same error
C:\Users\Windows\Desktop\New folder\character recongnition.cpp|63|error: 'it' does not name a type|
 
2 mins ago, by milleniumbug
if you're using an IDE, search for the appropriate option in the project settings
 
(starting with the basics) are you including all of the needed headers? <map> for std::map, <utility> for std::pair
 
5 mins ago, by milleniumbug
Enable C++11 mode with -std=c++11
 
12:39 PM
Yes that solves my issue .
 
@IccheGuri for codeblocks this should help: stackoverflow.com/questions/18174988/…
 
@milleniumbug
thanks @login_not_failed
 
@login_not_failed @IccheGuri Also you may check this code: coliru.stacked-crooked.com/a/46d67a61f04e9b93
 
I have written a simple program in codeblocks .
#include<iostream>
#include<string>
using namespace std ;

int main()
{
    cout<<"Please enter your name";
    string str="Hello";
    cin>>str;
    cout<<"Your name is "<<str;
    return 0;
}
But I am having a set of errors
 
@IccheGuri I don't have any. As @milleniumbug metioned, make sure you've added -std=c++11
 
12:44 PM
I have added -std=c++11
@SzymonMarczak
 
nwp
@IccheGuri change gcc to g++
 
create an actual C++ project, don't try to compile a single file
 
in where @nwp
 
@IccheGuri This is working for me: coliru.stacked-crooked.com/a/96a212e679acf374
 
The problem is that you are compiling your code as C. That is a different language to C++
 
12:48 PM
Yes that works @ratchetfreak @nwp
Thanks to all
 
@milleniumbug What would you use instead of switch? if/else if/else ?
 
probably an if/else ladder, yes
 
Can I want to know technique to solve a programming question ?
 
@SzymonMarczak compilers are good at optimizing though
 
12:51 PM
fallthroughs are garbage, and clang can recognise the potential optimization too
it doesn't need your help by using switch
 
@SzymonMarczak in the end, assembly code with switches and if-else ladders will look like a bunch of conditional jumps anyway
so it's a matter of code style which approach will work for you
 
@login_not_failed depends on what you are switching on
 
@ratchetfreak @milleniumbug I don't use fallthroughs.
 
@ratchetfreak I'm sure in this case there would be nothing too criminal
 
@SzymonMarczak that's the point
 
12:53 PM
and whether you put an __assume(false) in the default case
 
you don't use fallthrough, but switch still feels the need to waste your brain cells by forcing you to remember to add break;
 
and you have to add extra braces to avoid scope leak across cases
 
there is literally one thing switch is good at
and this is warning on missing enum values
 
if you have, let's say, an enum with HTTP status codes coming in, it's better to build a huge switch
 
it's inferior everywhere else
 
12:56 PM
yup
 
@milleniumbug So this is the reason you don't like switches :P
 
so it ends up looking something like
switch(c){
case a:{

break;
}
case b:{

break;
}
}
while the equivalent if-else looks like:
if(c == a){

} else if(c == b){

}
 
needing to use break; isn't only a problem because of "you can forget it"
now you can't use break; inside a switch to exit a loop if you have a switch statement inside a loop
 
labeled break
 
a.k.a. goto
 
1:07 PM
struct list_t {
void *data;
list_t *prev;
list_t **next; /* array of next(children) */
uint32_t next_num; /* number of next(children) */
};

typedef struct queue_t {
list_t *head;
list_t *tail;
uint32_t length;
} queue_t;

void *queue_pop_head(queue_t *q)
{
if (!q)
return NULL;

if (q->head) {
list_t *node = q->head;
void *data = node->data;

if (node->next) {
q->head = node->next[0];
free(node->next);
node->next = NULL;
} else {
q->head = NULL;
}
if (q->head) {
q->head->prev = NULL;
 
user406009
@TrishanshBhardwaj What's your question?
 
I am getting SIGSEGV at "q->head->prev = NULL; "
 
user406009
Could you post the entire code sample? gist.github.com might be a good place to put the code.
 
user406009
(Like the entire code including whatever main method is used to cause the crash)
 
@milleniumbug There should be big banner here "remember to use fixed font button when you post code" :P
 
@SzymonMarczak like the pinned rules post in lounge :)
 
SIGSEGV is at line 32.
 
pinned rules post is always ignored
so it's pointless
 
6 signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xd
and address generated is 0xd.
 
1:12 PM
clearly you're dereferencing a null pointer
 
This must be a NULL pointer deference.
 
run your program in the debugger
 
But NULL check is present at line 31.
@mill
@milleniumbug: It's not comming everytime.
 
user406009
@TrishanshBhardwaj Can you post your entire crashing code example? The code you posted isn't complete.
 
Rarely I see this crash.
 
user406009
1:14 PM
For instance, we don't have whatever code is pushing things onto the queue or the particular main instructions.
 
1:27 PM
How can I solve this problem ?
What will be the technique to solve this problem ?
 
user406009
@IccheGuri Do you get any hints, such as the size of N and M as well as the number of characters?
 
Hi Guys. I'm trying to build ICU from sources. When I used standalone toolchain, it's pass configure step fine and correctly identify that my tool chain do not support nl_langinfo, however when I try to compile it with integrated toolchain (which is all the same, version, all the stuff) it's say that nl_langinfo supported and than fail on the step when trying to link code to nl_langinfo. And I have no idea why this happen. Could you give me some hints, how I could identify the problem?
 
I know the size of the matrics
@Lalaland
I also know the number of characters
 
user406009
@IccheGuri I can think of the greedy approach, which is simply to find the pixel which most evenly splits the current set of characters. I'm not sure if it's optimal though.
 
I have got a solution .
But I can't figure out the technique behind this solution .
 
user406009
1:31 PM
What's your solution then?
 
I am giving the solution . wait a minute
@Lalaland
Can you tell me the technique of this solution ?
 
wow this code looks like someone barfed it
 
user406009
@IccheGuri That's a greedy approach. It might or might not be correct
 
what do you mean by barfed >
My solution is also a greedy approach ?
 
@milleniumbug What do you think about this? :>
 
1:38 PM
nice
 
@Lalaland
 
user406009
@IccheGuri You seem to just be taking the pixels which are involved in the most differences. That doesn't seem to imply that you are always going to get the least number of pixels.
 
user406009
Let me try to come up with a counter-example.
 
@SzymonMarczak although I like the self-explanatoriness of the "long multiline text" in my example
so if your text area had it there, it would be perfect
 
ok @Lalaland
 
1:41 PM
@milleniumbug What about this one? :P
 
excellent
(improved fixed font help image - for future reference)
 
user406009
1:55 PM
1 4 6
1 0 0 1
1 0 1 0
1 1 1 1
0 1 1 0
0 1 0 0
0 1 0 1
0 1 1 1
 
user406009
Your code outputs 3, with the points (1, 1), (1, 2), and (1, 3)
 
user406009
Oh shoot, actually that's my mistake.
 
user406009
Invalid input.
 
user406009
2:15 PM
@IccheGuri It's a greedy solution, but I can't come up with a proof for why it is correct.
 
user406009
Sorry.
 
user406009
It does appear to be correct though
 
Ok .
what is your solution then ?
 
user406009
I don't have a solution. The one you posted is correct as far as I can tell.
 
user406009
I just can't come up with a proof for why.
 
user406009
2:18 PM
(Or a counter-example)
 
user406009
@IccheGuri cs.stackexchange.com/questions/59964/… gives a good example of how you can prove greedy algorithms to be correct.
 
user406009
But it's rather difficult.
 
Yeah
Thank you so much .
But I cant understand this code .
Can you please help me to understand this code ?
I have not used set and pair data structure in c++ code .
What does pair do ?
@Lalaland
 
pair is a struct containing 2 member fields, first and second whose types are set by the template param
 
3:10 PM
Hi, I am planning on learning C++. I learned C from K&R book, and the book is pretty good. Is there a book that you prefer for C++?
 
full name for k&r ?
you can see balagurushami book or herberschield .
 
"K&R the c programming language second edition"
 
nwp
3:27 PM
4260
Q: The Definitive C++ Book Guide and List

grepsedawkThis question attempts to collect the few pearls among the dozens of bad C++ books that are published every year. Unlike many other programming languages, which are often picked up on the go from tutorials found on the Internet, few are able to quickly pick up C++ without studying a well-written...

 
I have learnt c++ from Balagurushami and herberschild
@Siliproksi
very nice book
 
user406009
3:51 PM
@IccheGuri Sorry, I missed your previous message.
 
user406009
Here is how their algorithm works:
 
user406009
There is an set in V for every pair of characters.
 
user406009
For instance, if you had the characters A, B, and C, you would have 3 sets in V, one for A-B, one for B-C, and one for A-C.
 
Ok . Then
 
user406009
Each set contains all the points that differ between each intersection.
 
user406009
3:53 PM
So, if A and B differ at pixels (1, 1), (2, 3) and (5,6), the the A-B set is {(1, 1,), (2, 3), and (5, 6)}
 
user406009
The goal of the algorithm is to find a set of pixels such that every element in V contains one of those pixels.
 
user406009
Going back to the previous example of A, B, and C.
 
user406009
Imagine that A-B is {(1, 1,), (2, 3), and (5, 6)}, B-C is {(1, 3), (2, 5), and (5, 6)} and A-C is {(1, 1,), (2, 8), and (7, 7)}
 
user406009
The solution is to use pixels (1,1) and (5,6) as that will cover A-B, B-C and A-C
 
if A and B are sets of pixels A-B would be the difference between the union and the cross-section of A and B.
 
user406009
3:56 PM
It's just the pixels that are different. XOR would be the operation, but you could also just call it the difference.
 
user406009
Anyways, that code builds up the solution of final pixels one by one in a greedy way.
 
difference is not commutative
 
Imagine that A-B is {(1, 1,), (2, 3), and (5, 6)}, B-C is {(1, 3), (2, 5), and (5, 6)} and A-C is {(1, 1,), (2, 8), and (7, 7)}
The solution is to use pixels (1,1) and (5,6) as that will cover A-B, B-C and A-C
I have not understood this .
 
so it would need to be the union of A\B and B\A
 
user406009
@IccheGuri A-B is the set of pixels that differ between A B. Likewise for B-C and A-C.
 
3:57 PM
how can (1,1) and (5,6) will cover A-B, B-C and A-C ?
 
user406009
(1,1) is in A-B and A-C. (5,6) is in B-C
 
user406009
Thus you have at least one pixel from every set.
 
user406009
Like if we had A-D containing {(1,7), (2, 9), (3, 10)}, we would need more pixels.
 
Imagine that A-B is {(1, 1,), (2, 3), and (5, 6)}, B-C is {(1, 3), (2, 5), and (5, 6)} and A-C is {(1, 1,), (2, 8), and (7, 7)} and A-D containing {(1,7), (2, 9), (3, 10)}
The solution is to use pixels (1,1) and (5,6) and (1,7) as that will cover A-B, B-C and A-C and A-D
right ?
 
user406009
Yeah.
 
4:00 PM
But I will have to calculate the set B-D,C-D
 
user406009
Yeah. That example isn't complete because if you have A-D, then that sorta implies there must be B-D and C-D.
 
ok . I have understood .
 
user406009
Anyways, the way it builds up the solution pixels is in a greedy way.
 
user406009
At every step it find the pixel which is in the most sets.
 
If I can get this job , I will give you a treat
 
user406009
4:02 PM
Like from my example: A-B is {(1, 1,), (2, 3), and (5, 6)}, B-C is {(1, 3), (2, 5), and (5, 6)} and A-C is {(1, 1,), (2, 8), and (7, 7)}
 
user406009
(1,1) appears 2 times and (5,6) appears two times. All the rest only appear one time.
 
user406009
So the algorithm chooses either (1,1) or (5,6) (it's a tie, so the result is sorta arbitrary)
 
user406009
Once it chooses that element, it removes all sets which contain that pixel.
 
user406009
So, say it chose (1,1)
 
user406009
It would remove A-B and A-C
 
user406009
4:03 PM
The only remaining set is B-C is {(1, 3), (2, 5), and (5, 6)}
 
user406009
The algorithm repeats and chooses the next pixel which appears the most.
 
user406009
(In this case, they all appear only one time, so you have a tie between (1, 3), (2, 5) and (5, 6))
 
user406009
@IccheGuri If you are applying for a job, you usually don't see greedy algorithms for whiteboard interviews. The problem is that finding the solution for them is more "arbitrary" than for other classes of algorithms like dynamic programming or such.
 
many many thanks bro .
 
 
1 hour later…
user1593881
5:22 PM
What does C++ standard include? The programming language itself or the accompanying libraries also?
 
C++ standard describes the contract between the implementors and the users.
this includes both the language and the standard library
you can download a standard draft to get a general idea
 
user1593881
Many thanks.
 
user1593881
If the order of function parameters evaluation is unspecified how can that affect me and should I care about it?
 
it affects you if a.) evaluation of one parameters throws and you have exception unsafe code or b.) if you do side effects
 
user1593881
Pheew...
 
user1593881
5:28 PM
Thank you again.
 
example of a.) f(std::unique_ptr<Foo>(new Foo()), std::unique_ptr<WillThrowInConstructor>(new WillThrowInConstructor()))
the obvious solution for that is to use std::make_unique
example of b.) int g(int x) { std::cout << x << "\n"; } ... f(g(42), g(21))
 
user1593881
Awesome.
 
5:55 PM
Say I just spent a good long while trying to debug an issue in QML, which I've been learning as of late. Last time this happened I posted a question and answered it myself once I figured it out so that I would be able to find it next time by googling. That question got downvoted, though: stackoverflow.com/questions/43788391/… Is stackoverflow the wrong place for me to be trying to store this information?
 
nwp
@JonMcClung in the part "The error I get is ..." it is unclear how you arrived at that error. I suspect you had some C++ code to read the QML, but you didn't show it, so nobody can reproduce the problem, so nobody can answer it, so it is a bad question.
 
Okay, so the issue was that it wasn't copy-pasteable into an IDE? I assume most people familiar with Qt would understand that you could run it by simply opening a new project and sticking Square {side: 100} inside the ApplicationWindow
 
nwp
I've used Qt for years on a daily basis and never used QML.
 
I suppose I should have said "most people familiar with QML". My application is essentially QML-only at the moment. The C++ portion is exclusively that which was created by the IDE.
 
user406009
@JonMcClung seems reasonable to me. Don't know why people down voted.
 
6:04 PM
Thanks. I've gotten a lot better at posting questions and answers since I started, but often I still collect a downvote or two. It seems impossible to placate everyone.
 
user406009
Yeah. Some people on here can be quite temperamental and pedantic. Thanks for contributing anyways.
 
Thanks. I've got to be off now. Goodbye!
 
map<pair<int,int>,int> M;
        for(auto it = M.begin() ; it!=M.end() ; it++)
        {
            if(it->second > maxi)
            {
                P = it->first;
                maxi = it->second;
            }
        }
What is this code doing ?
 
user406009
It's finding the point with the max "value" in the map.
 
user406009
For that particular code, the values are the number of times that point appears the in A-B, B-C, etc sets
 
6:15 PM
incidentally it's also shit
 
user406009
So you get the point which had appeared the most amount of times.
 
because it can be replaced with a call to std::max_element
 
Yeah Thanks
 
 
3 hours later…
9:19 PM
is there any tool that detects what lines are causing inifinty loops?
 
if you suspect you are in one, you break into the debugger, stop the program, and see which line is it executing
otherwise, no
it's not possible to detect you are in an infinite loop
and not to mention which part of the program will loop infinitely
 

« first day (298 days earlier)      last day (2555 days later) »