« first day (1460 days earlier)      last day (1384 days later) » 

4:17 AM
@user13783520 Its good practice because it ensures that fflush is called, now in most implementations there is some black magic trickery that calls fclose on exit().
Now, the fun thing here is that it is not always possible to flush on abort :-)
All this garbage means that if your writing the kind of logger that needs to function when the process is crashing, you need to basically use std error (which is unbuffered) or disable buffering.
 
user13783520
4:37 AM
@Mikhail Ok thanks
 
8:24 AM
Hey, how does one access the std::cin buffer, in the fashion that we access the std::cout buffer?
 
@d4rk4ng31 I don't understand the question, you mean like rdbuf() ?
 
See, its like this:
If we have a vector vec
(Just a sec)
@PeterT, so,
as we access the elements of a vector using std::vector<int>::iterator
Can we access the elements of cin using (something equivalent of) std::istream::iterator?
 
Is it by default an iterator to std::cin?
Like if I have std::cin>>num1>>num2;
Can I access the values of num1 and num2 using that std::istream_iterator?
 
std::istream_iterator<int> in_num(std::cin);
num1 = *(in_num++);
num2 = *(in_num++);
 
Ah.... great
Thanks a lot :)
 
 
1 hour later…
9:49 AM
@PeterT, in your example, std::istream_iterator<int> in_num(std::cin);, if I want to use std::count to count the number of 2s, how would I get the second argument to std::count?
 
you wouldn't. There is a sentry value for std::istream_iterator but it just reads until end-of-stream which you typically wouldn't reach in std::cin
 
I have been working with C++ for a little while now, creating applications in XCode and AppCode.

As a self-taught developer with no CS background, this has been a steep learning curve.

Fortunately, the language aspect has been easy to research, and the compiler gives a lot of helpful feedback that allows me to learn quickly. It has helped me expand what I know, as well as what I know I don't know.

However, there is another aspect to this which I might loosely define as compilers, libraries, frameworks, project settings, etc. This has been much harder to understand. The information is har
 
Oh, also, what happens to the previous argument after we increment the iterator? Like does the buffer value under the iterator get cleared after we've incremented the pointer?
@BenHogan I suggest you go with the official docs for whatever framework you're using
 
@d4rk4ng31 yeah, you read it and it's gone. If you want to store the values then read them first and then process them
 
Oh, so I cannot directly use the buffer for finding the count of 2 in the input?
 
10:02 AM
std::cin isn't going to keep storing all the things you put into it, what would happen if you pipe in gigabytes of data into a program?
 
Hmm.. You're right, but for small input (like 5-10 numbers?)
 
if you want to keep it, store it yourself
 
Oh! okay, thanks :)
 
do you understand why they use dynamic allocation to create new classes?
how could you do this without using the new bit?
 
@Permian how would you do it without dynamic allocation?
 
10:14 AM
i have no idea, but you can create construct classes without using new?
 
If a Node contains the Next Node and the Next Node contains the Next->Next Node by value then how would you know how large to make the Node class?
 
ahhhh!!!!!
 
Sure if you just do "Node n;" you constructed a new node
 
i get it now
 
But you can't make a class contain an instance of itself by value as a data member, that's just infinite recursion
 
11:07 AM
@PeterT what do you mean by value here?
 
by value as opposed to by reference or by pointer
 
ok
Node(int val) {
        this->val=val;
        next=NULL;
    }

why use "this" here?
i thought there was another way to do this?
 
to differentiate the parameter "val" from the data member "val"
 
Node(int val) {
val=val;
next=NULL;
}
 
you can just give the parameter a different name
 
11:10 AM
why doesnt that work
 
because you're using the wrong "val"
 
lhs val is from the class, rhs val is from the ()
no?
 
no
why would it be
 
you can do that in python haha
i think
 
I'm gonna doubt that
 
11:14 AM
Node(int val) {
        Node.val=val;
        next=NULL;
    }
is that ok?
 
na
Node::val
 
omg why?
1
Q: Constructor Initialization and private variables set inside constructor

Kelvin DavisMy constructor is suppose to only take one variable. But I'm curious if you ca initialize other variables that arent in the constructor defintion. class WordAnalysis{ private: int timesDoubled; word *words; int wordCount; int index; void doubleArrayAndAdd(string); bool c...

 
because that's how scope resolution was made to work in C++
 
in that link we can do
WordAnalysis::WordAnalysis(int arrSize){
wordCount = arrSize;
int timesDoubled = 0;
int index = 0;
}
here wordCount doesnt is just by itself, val = val doesnt work
 
yeah, there's not two different wordCount variables in that scope
so there's no name shadowing
 
11:17 AM
ok
 
nwp
Node(int val) : val{val}{} compiles and sorta looks like what you want.
 
val{val}{} why do you need the extra {}?
 
nwp
The extra {} is the constructor body.
 
oh yeah initalizer lists
 
nwp
Maybe Node(int val) : val(val){} looks clearer to you.
 
11:19 AM
yeah
although i think using {} is generally safer
stackoverflow.com/questions/388242/… there is a comment in that link telling the OP to read a good book. The problem I have is it I have C++ primer but it doesnt cover these sorts of examples
 
nwp
Though I wouldn't recommend it. I recommend adding -Werror=shadow which makes this not compile.
 
@PeterT because its a class not an object?
 
kind-of, more like because you're doing scope-resolution and not member-access
 
10
Q: whats the difference between dot operator and scope resolution operator

defiantI just wanted to know the difference between . operator and :: operator?

 
same goes for

namespace abc{int myfunc(int a); int someint;}

int main(){abc::myfunc(abc::someint);}
 
11:28 AM
yeah
class Solution {
public:
    TreeNode* insertIntoBST(TreeNode *node, int val) {
		if (!node) {
			TreeNode *newNode = new TreeNode(val);
			return newNode;
		}
		if (val < node->val) {
			node->left = insertIntoBST(node->left, val);
		}
		else {
			node->right = insertIntoBST(node->right, val);
		}
		return node;
	}
};
TreeNode *newNode = new TreeNode(val);
is this because you dont have how big the BST will be, right?
What would happen if I did instead:
TreeNode newNode = TreeNode(val);
 
Then you would just a single node, that alone is not a problem.
 
ok
interesting
 
you will only get an issue once you're trying to actually store that somewhere
 
what do you mean?
 
TreeNode a;
TreeNode b;
TreeNode c;
a.left = b;
a.right = c;

There's no sane way to make this code work. Try to define `TreeNode` and you will see where the issue is
 
11:41 AM
i cant see the problem
 
struct TreeNode{
TreeNode next;
int value;
}

what is sizeof(TreeNode) ?
 
it wont be defined
like some things dont have a distance or length
 
lol ok, then how much memory will you allocate for it if you write TreeNode t;
 
i dont know
i see
 
well the compiler also doesn't know :P
 
11:46 AM
haha
oop in c++ is hard, theres too many ways to do things
class Node {
public:
    int val;
    Node* next;
    Node(int val) {
        this->val=val;
        next=NULL;
    }
};
back to this... why use this? whats the advantage fo doing this at the object level?
 
doing what?
 
hang on
Node(): val(val1), next(NULL) {}
 
you're missing the parameter declaration in "Node()"
 
i didnt you needed it
you dont need it for next
 
I meant for "val1"
 
11:55 AM
why do i need it? here is an another example
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
you dont declare the things in the () in the initializer list here
 
I just meant you didn't define what "val1" is
in that example it just initializes val with "0"
 
oh!!!!
 
nwp
val1 and NULL have to come from somewhere. NULL is a global macro (probably) which you are supposed to replace by nullptr. val1 could be a global variable and then it's fine. Otherwise the compiler will tell you it doesn't know what val1 is.
 
that has the go in the first () ?
 
nwp
It makes sense to pass it as a parameter. But other options are possible.
 
11:56 AM
such as?
 
nwp
Node() : val(get_next_value()) {} where you define int get_next_value() { return 42; } for example.
 
    class LRUCache {
    public:
        struct ListNode {
            int key, val;
            ListNode *next, *prev;
            ListNode(int a, int b): key(a), val(b), next(NULL), prev(NULL) {}
        };
        unordered_map<int, ListNode*> map_;
        ListNode *head, *tail;
        int capacity_;
        LRUCache(int capacity): capacity_(capacity), head(new ListNode(-1, -1)) {
            tail = head;
        }
:49881160 why use nullptr? here is some code where they dont
 
nwp
NULL has been replaced with nullptr in C++11 because NULL had various issues.
 
good to know, thanks
 
nwp
For example int i = NULL; sometimes compiles and sometimes doesn't.
 
11:59 AM
what happens if its a null variable not null pointer?
 
what's a "null variable"
 
i dont know
 
nwp
There is std::optional<int> maybe_value; which might be what you are asking about. But that uses std::nullopt instead of NULL or nullptr.
 
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
im going to stick to this way of doing constructors
@nwp no im just stupid :P
ive got to go
thanks guys!!
c++ is mind blowing at times
and the books/blogs ive read just dont seem to have the right level of detail
 
 
2 hours later…
1:51 PM
I'm trying to execute my program that uses jsoncpp with Visual C++ with the cl compiler in windows cmd. However, it complains about "error LNK2019: unresolved external symbol". Anyone know how to link jsoncpp? I use the the amalgamated source for jsoncpp.
 
just add the cpp file to the list of files you pass to cl
 
Thank you!
 
2:19 PM
Hey everyone, quick question : if I create a struct on the stack, give a pointer to it to a new thread (with pthread_create for instance), and guarantee by synchronization that the new thread copies the struct before I exit the scope, everything should be ok right ?
 
nwp
If you are stuck with an ancient C code base then sure. Otherwise I would change the code to use std::thread.
 
yeah yeah I'm working with some legacy embedded stuff, changing dynamic alloc to static alloc for my target
 
nwp
You also can't access the object while the other thread is potentially writing to it or can't write to it while the other thread is potentially reading it.
But you're essentially blocking one thread to allow another to run. That doesn't sound useful. May as well skip the thread creation and synchronization overhead and just call the function.
 
2:40 PM
@nwp True, but the writing is done before the thread is started and it's the only write. Indeed, that's what I'm doing ! The thread is not supposed to start right away
So it just copies its arguments and waits
 
nwp
Ah, I assumed the goal was to avoid a copy. But now I see the goal is to pass a struct through a void * in a way that imitates pass by value.
Dynamic allocation really seems easier, but if you can't because every nanosecond matters I guess you'll have to benchmark the different versions.
 
Yeah, you got it. It's not a question of performance though, more a question of not having an allocator or an easy way to use a heap (and not really needing it)
 
 
1 hour later…
3:55 PM
Hello, I am pretty new at C++ and I have some trouble understanding something. I posted this question and was answered. However, I still don't get it. Could someone explain why this code doesn't work?
int val2 = 38;
int *ptr = &val2;
const int* &ptrRef = ptr;
The last line is giving the error
 
the "const" refers to the int and the types just don't match
 
but why would this work?
int i = 92;
int &ref_i = i;
const int &ref_i2 = ref_i;
 
int* const &ptrRef = ptr;
the conversion rules allow you to make a const& to something that is not const
but you said "a non-const pointer to a const int" and that is not something a "non-const pointer to a non-const int" can be converted to
 
ohhh i see, so its just type mismatch?
 
yes
 
4:00 PM
okok thanks! And also, if theres a const reference, what does that really mean?
 
just means "treat this reference as if it's to something constant", so essentially "forbid mutable operations on this variable in this context"
 
For the first code i sent, I think i was trying to make the ptrRef a constant reference? But that's not correct right?
 
4:17 PM
Ohh so bascially
int* const &ptrRef = ptr;
Means that you want the ref to be const right? Because you are declaring ptrRef as a const pointer?
 
5:06 PM
what is the advantage of doing string(1,S[0]) instead of string(S[0])?
::tolower what is this feature called?
like where is there no thing before the ::?
 
5:58 PM
@Goddrew nope, const binds left so it's a const pointer to int
@Permian the :: is just to make sure it's the global thing.
so ::std::cout for example is the same as std::cout for most cases
it's there so if you have

int a;
namespace b{
int a;
void func(){
std::cout << ::a; //<prints global a
std::cout << a; // <prints b::a
}
}
 
 
3 hours later…
8:41 PM
#include <iostream>

class ObjectA { size_t size : 1; };
class ObjectB { size_t size : 2; };
class ObjectC { size_t size; };
Why do all Object's have the same size (presumably 8)?
 
8:57 PM
...
 
9:19 PM
hey guys, I remember something in c++ like this: int function() -> 0; // returns 0
hows that again?!
or am I crazy?
 
there's "auto function()->int{return 0}" or "[](){return 0;}" "auto function = [](){return 0;};"
but there's no returning without the return keyword in C++
 
damn, thanks peter
 
9:39 PM
Just realized the size of type member : width; is dependent on the type (e.g.: the size of size_t is different from unsigned char)
Are bit fields platform-dependent?
 

« first day (1460 days earlier)      last day (1384 days later) »