« first day (829 days earlier)      last day (2007 days later) » 

5:54 AM
@nwp you gave me advice a bit earlier on how to store memory for a chess move-tree, and right now I have a bunch of vectors full of unique_ptr's... but is that redundant and wasteful of memory/exec time? I think I remember you trying to tell me something like that
@EuriPinhollow MCVE? And I mean input as the program is running, so you can respond differently depending on what the program does
 
6:35 AM
@SethTaddiken stackoverflow.com/help/mcve Yes, you cannot interact with Coliru execution. Just hard-code some input.
 
7:09 AM
@EuriPinhollow thank you!
 
 
2 hours later…
nwp
9:37 AM
@SethTaddiken If it is wasteful depends on if there is way that avoids spending memory on pointers and performance on dereferencing/cache misses. There might not be.
The trick that comes to mind is to use indexes instead of pointers. You have to give up erasing from the front and you must know which vector your object is in. In turn you get to get rid of the unique_ptr. Might be a viable alternative.
Another trick that is commonly done is to have 1 single vector for all the boards, reuse instead of erase elements and wire them up with raw pointers. That way memory management is much easier and it is still somewhat cache-friendly, depending on local the connections in the vector are.
 
9:50 AM
class B{
private:
    static int j;
};


int B::j = 3;
So this static member j is not static linkage but external linkage?
I've been always considering as the same meaning as static int i = 3; sort of thing
 
nwp
10:13 AM
The reason is that you normally put your class declaration in a header and then you would get multiple definitions errors because of j.
 
10:31 AM
Yes, but what you said is the result, not the reason I think. I can put non-class static variables in the header files.
The meaning of static for class members is different from static variables declared outside a class. And that's very misleadind T_T.
 
nwp
@Rick Because those get their storage later, when you create an object. That doesn't apply to static data members.
 
Ok. For now I would just memorize this
 
nwp
It's not that big of a deal. If you forget to define you static member the linker will tell you.
 
hello
i'm sitting hours in front of a code and i don't understand what i did wrong. could somebody take a look at it please?
not a complicated code, but for some reason i made a stupid mistake and it doesn't compile
 
nwp
Put it on coliru.
Make sure it shows the error you care about.
 
10:41 AM
Yes, but I have to figure all this static const inline , external linkage internal linkage things out so that I can know how exactly redefinitions error would occur.
 
how can i put different files there? (.cpp and .h) ?
 
Hmmm. It seems that you can only upload images
 
nwp
@BeginningMath You copy the content of the header over the #include "header.h" in the .cpp and post only the .cpp. Try to make it not overly long.
If it absolutely requires multiple files use onlinegdb instead.
 
thank you very much. the latter seems nice
 
Yes, I didn't find a way to add header files in coliru.
 
10:45 AM
it gives me really weird errors
 
FTR, you can use coliru for multiple files, but it needs a bit tinkering around: share one of the files, and use the path below coliru.stacked-crooked.com/a/a1ff5c5a1010cf74 coliru.stacked-crooked.com/a/1727580e1d805cbd
 
i am quite sure i've done a really stupid mistake somewhere and i can't spot it
 
nwp
TBST.h:48:9: error: redeclaration of ‘TNode* ::root’
  TNode *root;
         ^
TBST.h:32:12: note: previous declaration ‘TNode* ::root’
     TNode* root;
            ^
That seems fairly understandable.
 
thank you, fixing it
 
#ifndef IOSTREAM
#define IOSTREAM
#include <iostream>
#endif
you don't need this
standard headers already have include guards
 
nwp
10:50 AM
#define TBST completely breaks your code.
 
also ^
 
okay, i thought it's a good practice to put it there
thank you very much for correcting me. so just remove the define TBST?
 
nwp
#define TBST means "Replace TBST with nothing". Then you write class TBST and the preprocessor does what you told it to and does the replacement and turns it into class.
Either remove it or use TBST_H instead.
 
i'll try to remove it first to just make the code running, and after it will run successfully i will try to learn how to optimize it and deal with the small but important things
 
nwp
And later it sees
TBST();
~TBST();
which gets turned into
();
~();
and the compiler is super confused what that is supposed to mean.
 
10:56 AM
lol, this is a funny mistake. i'm fixing those. in addition i did some mistakes with function and variable names, but the compiler can spot them easily after removing the #define like you said
 
11:25 AM
i got a reallllllly weird error message now
error: ‘cout’ was not declared in this scope, what does it mean? how is this possible?
i am quite sure i've added the correct library
 
std::cout
You need to refer to it as std::cout, not as cout
 
right, because of globality?
 
because it's in a namespace
(in fact, you do refer to it as std::cout in the rest of the program)
 
thank you very much. fixed it and made it running thanks to your suggestions
would you mind giving me a few tips to improve it and make it better? i also want to learn new stuff and i wish to improve from your suggestions
 
avoid UPPERCASE_NAMEs for non-macros
 
11:39 AM
okay, i'll fix that
 
nwp
You are also not supposed to use new in modern C++.
Things like std::string* student_name; should simply be std::string student_name;.
NULL should be nullptr.
 
i have a question: instead of using wrappers like i did, if i created a new cpp file and put error checking functions into it, and then call it from main instead of a big wrapping function, would it be a good practice?
okay, fixing your remarks
 
nwp
Things like struct TNode* left; should be std::unique_ptr<TNode> left;.
 
(modern cpp is > cpp11?)
or 14?
 
nwp
You don't need to typedef your structs. Unlike C C++ does that implicitly.
c++11 counts as modern.
 
11:46 AM
can you please answer my question regarding the wrappers?
 
nwp
You are not supposed to use try/catch for control flow. If not having a successor is not an exceptional situation it might be better to make a function has_successor or return a nullptr or an std::optional<Result>.
If that is not what you meant by wrappers clarify what you meant.
 
suppose i have a function like this: coliru.stacked-crooked.com/a/4e7df814d4fd5768, but it is veryyyy long. and a lot of its internal functionality can be used(and in fact is used by other functions). so is it a good practice to create smaller aiding functions in a separate cpp file, so that that long function would look something like: coliru.stacked-crooked.com/a/60223b262ef16702
will it be good use of encapsulation?
 
nwp
The first code looks weird because you catch exceptions you are not supposed to catch.
You are only supposed to catch exceptions that you can handle.
For example if the arguments are invalid then insertHandler fails and there is nothing it can do about it.
It cannot handle the exception because it cannot make the arguments valid.
So it simply doesn't catch errors and instead lets some upper function deal with it.
Additionally if TBST* tree is required to be valid you write TBST& tree instead. That way it is impossible to even pass an invalid tree (without making some coding error first).
 
12:02 PM
could you please elaborate your last sentence? it's really important but i'm not sure i'm getting it
so the way i tried to recreate the 'handler' is not correct? (using multiple functions(instead of a try/catch block)
 
nwp
I might write it something like this.
@BeginningMath A pointer can be a nullptr. A reference cannot. If your function cannot deal with a nullptr use a reference instead. That way the signature of the function shows that it cannot deal with invalid objects.
@BeginningMath I'm torn for the functions. If you name them well, then that is a good idea because it abstracts the code. If people need to look into the functions to see what they do you just wasted people's time (probably your own, because you have to read it). checkifobjectinitiliazed for example should not exist. The constructor initializes the objects or throws an exception. There should never be an uninitialized object.
 
the way you wrote it is way more readable and efficient. i thought of reusing some functions that check if the tree is initiliazed, number of arguments correct and etc... but you found a clever way to reduce the amount of checking needed
 
12:41 PM
hmm do you see any other redundant or unnecessary code in TBST.cpp? wondering if i could trim more code lines and make it better or more readable(or efficient)
 
nwp
Did you already switch to std::unique_ptr?
TBST::remove looks very overcomplicated.
There should probably be a rebalance function.
 
how can remove be simplified? and what do you mean about the rebalance function? after the tree was altered to fix it using the traversals?
i switched to nullptr, but i don't know how to use unique ptr correctly
 
1:09 PM
i've tried to use what you showed me, but then void insertHandler doesn't compile
i think it's due to TBST& rather than TBST*
 
1:36 PM
can we say tree == nullptr ? or should it be tree == NULL ?
 
yes
treat NULL as if it doesn't exist
 
thank you very much
 
1:53 PM
if i have a tree and i want to check if it has a predecessor to handle possible errors, and if not retrieve the predecessor, how should i do so? for instance, the easiest way is to create a bool function, to check if exists, and if not throw an error and if exists then show it. however, it doesn't seem very efficient. how can it be made more efficient?
just to retrieve the predecessor and if pointer is nullptr to print it does not exist?
 
nwp
2:24 PM
That's a reasonable design.
Maybe you can make your print function handle that because it can actually deal with non-existant nodes by printing some special string, so you simply print the predecessor and don't even check for nullptr.
 
 
1 hour later…
3:34 PM
Can I detect memory leak in compile time instead of runtime? I read this (clang documentation)[https://clang.llvm.org/docs/LeakSanitizer.html]
`clang -fsanitize=address -g memory-leak.c ; ASAN_OPTIONS=detect_leaks=1 ./a.out`.

And `ASAN_OPTIONS=detect_leaks=1` is a runtime parameter.
 
static analysis obviously won't be able to detect all memory leaks
 
@milleniumbug I think so too = =
 
and if your memory leaks are likely to be caught by static analysis, they're likely to be simply rewritten into equivalent code that doesn't call release functions directly
 
Ok, then I have to paste the runtime parameter into my makefile.
g++ -fsanitize=address -g -Wall main.cpp $(INCLUDEPATH); ASAN_OPTIONS=detect_leaks=1 ./a.out
 
 
3 hours later…
6:34 PM
can somebody help me?
i'm beginning to do really stupid mistakes and would appreciate help to finish with it
 
6:52 PM
i think that the problem is that tree is only instantiated in main() while i'm calling it before it, but i don't know how to fix it :(
:(
 
nwp
I get
main.cpp:10:20: error: ‘id’ was not declared in this scope
     checkIfIdIsNum(id);
Seems a simple enough error.
 
i fixed it here: onlinegdb.com/Hk3VY88i7 but i'm getting a lot of errors as if it doesn't know TBST at all
 
7:07 PM
Shouldn't be the cause of your error, but you do not need to header-guard other peoples' headers. That is, the header guards around <string>, <math.h>, and <iostream> are completely unnecessary
 
thanks justin, i'll fix it afterwards. right now i'm just trying to fix the main program so it could function correctly
 
The problem in main is that you are trying to call checkIfTreeIsInitialized like checkIfTreeIsInitialized(TBST* tree);. That's not how you call a function. Just write checkIfTreeIsInitialized(tree). Similarly for the other functions
 
too many hours on this and i'm beginning to do so stupid mistakes :(
 
Take a break. Go for a 5 minute walk
 
next thing i have to fix is to explain to the functions what 'id' is
(how to take it from the input)
 
7:38 PM
can somebody assist me with fixing functionality.cpp:5:25: error: ‘TBST’ was not declared in this scope
functionality.cpp:5:31: error: ‘tree’ was not declared in this scope
void checkIfTreeIsEmpty(TBST* tree) ?
 
nwp
Well, did you #include "TBST.h" or wherever it was supposed to be declared?
 
functionality.cpp:118:6: error: ‘tree’ was not declared in this scope
tree->insert(id1, name);

is there a way to use tree if it's instanstiated in main but i want to use it in a supplementary file like functionality.cpp ?
 
nwp
8:29 PM
You pass it in a function parameter or make a global variable.
 
https://onlinegdb.com/Hy7-6P8sQ

what's wrong with: 'In file included from functionality.cpp:4:0:
functionality.h:26:31: error: variable or field ‘checkIfTreeIsInitialized’ declared void
void checkIfTreeIsInitialized(TBST*);' ?
tried to do that already nwp, but it did not work apperantly
 
nwp
In that line the compiler doesn't know what TBST is because you forgot to #include "TBST.h".
std::runtime_error << "Given tree is empty. please add students' id and name into the tree" << std::endl; return ; is not correct syntax. This has some explanation and examples.
 
9:00 PM
i did it before using a try catch block
but i'm trying to use multiple small common functions to make it more reusable
rather than a try-catch block for each one
void checkIfTreeIsEmpty(TBST* tree)
{
try{
if (tree->root == nullptr)
}
catch
{
std::runtime_error << "Given tree is empty. please add students' id and name into the tree" << std::endl; return ;
}
}
seems somewhat problematic in this case, and i think that not correct
what would you suggest?
otherwise why not use std::cerr for each time we find a problem, like tree not initialized and such
 
nwp
@BeginningMath That is simply invalid syntax.
@BeginningMath Then you get a bunch of text on the console. How does that help deal with errors?
 
so instead of cerr i should aim for runtime error?
 
nwp
checkIfTreeIsEmpty should return a bool, not throw an exception.
 
9:27 PM
i'll try to use cerr just to make it compile and work, and after that i will try to find a more intelligent way to use runtime errors or bools
functionality.cpp:68:20: error: ‘tree’ was not declared in this scope
TNode* node = tree->tree_predecessor(tree->search(id));

this is my main problem. how can i fix it since tree is only being initialized in main() ?
 
9:48 PM
node is easy to fix, but tree? tree is my main problem
 

« first day (829 days earlier)      last day (2007 days later) »