« first day (232 days earlier)      last day (2627 days later) » 

3:23 AM
@towc use a static analysis tool (I didn't even read your post but you should be using one)
 
 
5 hours later…
nwp
8:20 AM
@towc try running the program under gdb and see if you can ctrl+c from there
 
9:11 AM
Good day. When using third party libraries from c++, is there a way to redirect stdout and stderr from those libraries to my own logger? My assumption is that this should be something common for all products that use third party libs
 
nwp
@SergioBasurco there is only one stdout for the whole application, libraries don't get their own
 
right, let me rephrase. Some of the third party libs I use use stdout directly instead of their logging class, that's totally fine. I would like to channel stdout to my own logger
That's not precisely the issue
 
nwp
37
Q: C++: Redirecting STDOUT

CharxIn my application, I want to redirect the output that would normally go to the stdout stream to a function I define. I read that you can redirect stdio to a file, so why not to a function? For example: void MyHandler( const char* data ); //<<Magical redirection code>> printf( "test" ); std::c...

 
I'm sure I can find solutions to do that.. my question is: Is channeling stdout to my own logger the best approach? How do other programs deal with third party lib's output?
 
nwp
@SergioBasurco hmm, I think libraries are not supposed to print to stdout by themselves without being told to do so
 
9:16 AM
if source available they patch the lib so it doesn't use stdout
 
What should I expect from a library then? That they use their own logging sink?
 
or let you insert your own logging sink
 
nwp
@SergioBasurco that they have a function to turn logging on/off and that you can set the stream/file to output to
 
Very reasonable. I probably haven't looked deep enough into the libraries I'm using
 
 
1 hour later…
10:45 AM
Hello, I have a namespace which is declared in many files in a project 1. It works perfectly well in a project 2 of the same solution. Now I try to add that namespace to use in another solution by adding the project and setting dependency and there is a namespace error. Anything to check for that?
 
@dtxd what namespace error
my crystal ball tells me you simply forgot to add include paths
 
@dtxd IIRC, Visual Studio references only pull in the binaries for linking but do not set up include paths.
 
name followed by :: must be a class or namespace name
so I should include all the .h files?
 
hop on over to the C++ Q&A chatroom and include some code.
 
@dtxd no, add the header location for project 1 to the include paths in project 3
if you think this is annoying and/or unnecessary you're obviously completely right
but that's a consequence of primitive build modeled used by C++
 
10:52 AM
should I check the "External Dependencies" of the project too? It has tons of header files there.
 
@dtxd I don't think that's necessary
 
It still doesn't work but I feel assured somehow. I'll try checking it all again. Thank you all.
 
Joe
11:32 AM
what is better c or c++
 
nwp
better for what?
 
C++ obviously
but note that you're in the "C++ Questions and Answers" room
if you had asked the same question in the C room, you'd probably get a different answer
13 messages moved from Lounge<C++>
 
 
1 hour later…
nwp
12:56 PM
is this supposed to crash?
 
it doesn't crash
I'm assuming ideone says "execution error" is that you return a non-zero code from main
 
nwp
oh, that makes sense
 
 
5 hours later…
6:16 PM
About std::deque
1
A: How to release memory from std::deque?

MSaltersstd::deque will return memory to its allocator. Often this allocator won't return the memory to the OS. In such cases, it appears as if memory is not "released". Good memory leak detectors will be satisfied as soon as memory is returned to the allocator, and understand that not all memory is rele...

From this I understand that memory released by the deque is considered free by the program, but allocated from the OS?
There's actually no way to give back that memory to the OS unless you destroy the deque object?
 
std::deque is not special
it delegates the job to allocator, which, unless you use the non-default one, is std::allocator, which uses ::operator new, which on all the implementations I'm familiar with uses malloc.
malloc in general won't give the memory back to the OS if it doesn't need to
TL;DR you don't need to worry about this 99.99% of the time
 
@milleniumbug - "std::deque sobs in a corner"
Okay thanks!
 
6:44 PM
made a "hey look mom I can code" thing:
rm main; g++ -std=c++14 -Wall -Wextra -pedantic main.cpp Maze.cpp Coordinate.cpp Random.cpp -o main; ./main
 Round: 1	 pos: [1,1]

 xxxxxxxxxxxxxxxxxxxxx
 xo          x x     x
 xxxxxxxxxxx x x xxx x
 x x       x x x   x x
 x x xxxxx x x x xxx x
 x   x   x   x   x   x
 x xxx x xxxxx xxx x x
 x x   x     x   x x x
 x xxxxx x x xxx x x x
 x     x x x   x.x x x
 xxxxx xxx xxx xxx x x
 x   x   x x x x   x x
 xxx xxx x x x x xxxxx
 x   x   x x   x x   x
 x xxx xxx x xxx x x x
 x x   x   x   x   x x
 x x xxx x xxx xxxxx x
 x x x   x x x       x
 x x xxxxx x xxxxxxx x
 x         x         x
 xxxxxxxxxxxxxxxxxxxxx
 
@towc cool
 
thanks mom 😀 ofc I'm looking for some kind of feedback. I realize a lot is wrong and non-elegant, but still
if you have time to spare...
 
I'll try to make some code review of this in a day or so
 
just looking for general pointers, don't bother writing any kind of report :)
thanks
I feel like there are some big things that I've completely missed, and hacked my way around maybe
 
nwp
@towc you could post it on codereview.stackexchange.com
 
6:57 PM
that would mean that I think it's worth a thorough review
which I really don't :)
 
nwp
ok, at first glance I'd say Coordinate::add should not take a copy (possibly outdated because compilers are good at removing unnecessary copies) and you should consider using operator +
 
point taken
 
nwp
setX seems unnecessary since x and y are public
 
yeah, I added those just because, I should remove them, right
 
nwp
public is the default for struct, you don't need to specify that
 
7:00 PM
good point again
 
don't add an empty destructor definition
 
oh, didn't know that was not a good idea
ok
 
it's redundant code
redundant code is always bad because it wastes time of whoever is reading the code
 
yeah, sure
I still am not too sure what destructors actually do, googling it
 
nwp
maybe isPartOf should take a const char (&arr)[2] to reject arrays that do not have size 2, or maybe you should use std::array<char, 2> instead
 
7:05 PM
I think it should be refactored altogether
 
nwp
std::endl causes some OCD for some people, you could avoid that by using '\n' instead
 
Don't use std::endl ever
stream << std::endl; is equivalent to stream << '\n' << std::flush;
if you want flushing, use the latter
if you don't, just use stream << '\n';
 
nwp
std::cin >> w should probably be inside an if or some other error checking should occur
 
std::endl is a beginner's trap
 
do you also not use std::cout?
 
7:10 PM
I use std::cout
I'm not using std::cout; though, or using namespace std; though
 
yeah, sure, but I noticed you wrote stream
 
I wrote stream because there are different output streams than std::cout (like file streams or string streams) so I was being generic
 
I should probably learn about that
 
The isPartOf should be refactored because the purpose of it isn't clear
I'd suggest an alternative but I'm not sure what I'm replacing
 
was trying to use the JS equivalent of array.indexOf
or .contains
it would check if a value is within an array
isPartOf( { 1, 2, 3 }, 2 ) == true
isPartOf( { 1, 2, 3 }, 4 ) == false
 
nwp
7:13 PM
I'm trying to make it a rule to initialize all members in the declaration, so it should be struct Maze{int x{20};}; or struct Maze{int x = 20;};. It would somewhat simplify your constructor too.
 
Random* Maze::random = new Random();
no
 
nwp
@towc that would be std::find
 
Jan 28 at 11:41, by milleniumbug
also don't use new/delete, these are low-level tools, you don't need them
 
ok, so what should I have done then?
Random Maze::random()?
 
Random Maze::random;
 
7:14 PM
oook
thanks
 
nwp
@towc that one looks like a function declaration, aka the most vexing parse
 
also if you use a custom pseudo-random number generator, you'd like to describe the implementation (or a link to an external description if you implement a well-known one), and the rationale why you've chosen this one in a comment
 
nwp
I can't tell if your random numbers can overflow, if they do that would be UB.
 
oh no, I wrote my own because I was offline when I was writing and didn't remember how to do random
then it worked, and that was it
yeah, the random class is really bad, forget about it
I just tried a few things until it felt random enough
 
nwp
Random::printABC has no definition
 
7:18 PM
also a leftover from debugging, nvm about it, but good point
and weird, would have thought that g++ would have complained
guess not because it was never used
and it won't tell me about every property that isn't used
 
nwp
Maze::fillPrintMap should just return the vector instead of using an output parameter
 
is that actually bad practice?
 
@towc <random> is what you want
returning by value is more composable
 
I thought that's what people do when manipulating arrays through functions, and I thought that for historical/consistency reasons, they'd do the same with vectors
 
C-arrays are not fun
I prefer to avoid them, and use std::array and std::vector instead
 
nwp
7:22 PM
@towc Output parameters are generally discouraged because they are confusing. In the specific case there is no real need for it.
 
ok, sure
I'll be using array for most cases from now on
but also, the way I used c-arrays to define keys and directions is fine, right?
or would that also be better off as a list/array/vector?
brb dinner, thanks all :)
keep writing if you want, I'll be reading soon
 
> keepGoing = --security > 0 && stackIndex > 0;
 
nwp
I think with C++17 template argument deduction is able to deduce the size of an std::array. For now you have to do that manually which is annoying and almost a good reason to use C-arrays.
 
...almost
the above code is modifying security and evaluating a boolean condition
that's too much
do these in separate statements
 
nwp
dirs uses magic numbers, they should probably be replaced by enums
 
7:29 PM
don't use magic numbers
 
nwp
Maze.cpp seems to be cut off
 
Maze::checkNeighbours() has while( i != 4 ) and I'm forced to assume this is the same as 4 in Maze::neighbourDirs[4]
if you had used std::array for neighbourDirs, you could have used while( i != neighbourDirs.size() ) instead
  if( w % 2 == 0 )
    w += 1;
^ that can be made into a function
alignToOdd or sth
std::system( "clear" ); is non-portable
you may already know this, but I'll say it again
there's nothing inherently wrong with using platform specific code, but unless you plan to never port this, you want to isolate such code in a separate module
void clearScreen() { std::system("clear"); } // in case you plan to replace the implementation you can do this in a single place
there's also a problem is that it launches a shell, and an external process and waits for it
it can fail in so many ways it's not even funny
which means it should never be used in actual production code
I wouldn't really care about this in a simple program like this, but I do care if it's not in a separate function so it can be replaced in the future
> random->gen() % availNeighbourNumber
^ it doesn't matter if your random number generator is good or bad, this is a bad way to generate a number from a [0; n) range
I can't be bothered explaining why it's bad, but there's a lot of people who already wrote about this, just look around for "modulo rand bias"
std::uniform_int_distribution will handle this for you if you're using <random>-style PRNGs
that will be enough for today, maybe I'll continue the review tomorrow
 
8:13 PM
all good points, thanks
and yeah, the random thing is far from ideal when used like that sure. I would normally do (int)(((float)random->gen / random->max ) * (availNeighbourNumber))
but my number generator is far from uniform from min to max, so that wouldn't work
 
nwp
@towc that wouldn't fix the problem
 
if random->gen is uniform, it would
or not?
 
nwp
no it wouldn't
 
oh, I'll read into it then
 
nwp
@towc I would recommend watching this
 
8:25 PM
cheers :)
 
 
1 hour later…
9:28 PM
ok, a good idea would be to start fresh with a new small project
like the maze I guess
ascii mandelbrot navigator?
 
nwp
9:42 PM
sudoku solver? Implement A-star path finding?
 
I don't know the algorithms for either :/
more worried about learning syntax and best practices than algos for now
 
9:59 PM
one stupid question not related to C++..
My school has created git shit so I have to create a new project should I add all the files in git cloned folder? or first create them in some directory and then copy them to git folder(this is pain).. I have seen a video on doing this?
 
nwp
@towc wikipedia knows, and they should be decent candidates to play with standard containers and algorithms
@DenisKa do you want to work with your school's git repository or make your own?
 
oh no, point was that I don't really want to spend time concentrating on that as well
 
@nwp work with school..
 
nwp
@DenisKa so the first thing you need to do is clone your school's git repository, so you want to do git clone myschool.edu/gitrepository
 
yeah then I get a folder..
 
nwp
10:04 PM
then you do your changes, git add file for the files you changed or added and then git commit to commit the changes
maybe sprinkle some git status around to get an idea what is happening
 
let me give my  directory tree
    -cmake-build
    -main.cpp and others
    -st.johnsEDU
so should I copy all cpp and .h files into that `stjohnsedu` and then add them or create a new project in stjohnsEDU directory (I know it is stupid)
now I have to create a new project and few more so should I create all projects in stjohnsEDU directory or I have to create them in separate folder and copy, make sense?
 
nwp
@DenisKa well, the idea is that your project is inside the git repository, either way should work
 
if I make a new project using code::blocks for example in my cloned directory, will my xml files also get uploaded?
no one needs that ide stuff
 
nwp
@DenisKa you need to git add them before they are added to the repository, just being in the folder is not enough
git status should tell you about files that are in the folder but are not in the repository
 
oh now I get it. :P stupid me
thanks @nwp
 

« first day (232 days earlier)      last day (2627 days later) »