#How to print everything in a vector to an ostream using std::for_each?
39 messages · Page 1 of 1 (latest)
my guess is that the copy constructor for std::ostream is deleted
what you meant was
[&os](lambda params){ lambda body };
Yeah I managed to figure that part out thankfully, but i don't understand why my original way didnt work, from what I could understand from cpp reference, adding the & at the start of the captures would change the default from copy to reference
@acoustic marsh has reached level 2. GG!
the capture [&, os] means capture everything by reference, except os, which is captured by copy
all variables with automatic storage duration i believe
Okay thank you so much for the help! Appreciate it
btw, im also guessing your first lambda doesn't do exactly what you want, since you're also getting a copy of stringLength
For some reason my code has memory leaks according to valgrind, even though i'm handling destructors exactly like the suggested solution in my question sheet does it. Can anyone test it and maybe help me figure out where the memory leak is?
show valgrind output?
I'll get on that later today I think when I get back to my c++ work
I tried to make the valgrind give more detailed output but all it told me was that some allocated memory was definitely lost
show
hmm i expected an overview of exactly what code was originating the leaks
yeah I tried getting it with the --leak-check=full
@acoustic marsh has reached level 3. GG!
oh i just needed to put the flag before the file
whoops my info was in it haha
I wish it could tell me where in the code it happened, the new(unsigned long) must be when I create the new Plot objects, but I have destructors just like the solution does so I'm confused
@sharp charm do you have any idea possibly, my code is in the .txt above if you want to take a look
i just installed valgrind on a virtual machine
and compiled your code with debug info (-g flag)
and valgrind does show the exact line where the problem happens
oh lol it's obvious
well, since you're storing pointers in a std::vector the destructor for a pointer is basically a no-op
this happens because the std::vector has no way of knowing whether those are owning pointers or not
so, at scope end you need to explictly loop over each element and delete it
alternatively, consider using std::unique_ptr
Oh so that was an option?? I thought about doing that but figured it wasn't needed since the suggested solution didnt do it
Well thank you for helping me quim, so nice of you ❤️
Need to remember this flag lol