#How to print everything in a vector to an ostream using std::for_each?

39 messages · Page 1 of 1 (latest)

acoustic marsh
#

When I try and pass the ostream parameter os to the lambda function in my for_each call, it tells me that the os parameter is inaccessible, is there a way to do this that's better and intended?

#

Notice the red squiggle where the error is

rich kettle
#

my guess is that the copy constructor for std::ostream is deleted

#

what you meant was

[&os](lambda params){ lambda body };
acoustic marsh
#

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

amber valveBOT
#

@acoustic marsh has reached level 2. GG!

rich kettle
#

the capture [&, os] means capture everything by reference, except os, which is captured by copy

acoustic marsh
#

Oh

#

Hmm and this "everything" is all the variables outside the lambda scope?

rich kettle
#

all variables with automatic storage duration i believe

acoustic marsh
#

Okay thank you so much for the help! Appreciate it

rich kettle
#

btw, im also guessing your first lambda doesn't do exactly what you want, since you're also getting a copy of stringLength

acoustic marsh
#

Yeah i fixed thst one too

#

I only noticed from some good ol printf debugging

acoustic marsh
#

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?

rich kettle
#

show valgrind output?

acoustic marsh
#

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

rich kettle
#

show

acoustic marsh
rich kettle
#

hmm i expected an overview of exactly what code was originating the leaks

acoustic marsh
#

yeah I tried getting it with the --leak-check=full

amber valveBOT
#

@acoustic marsh has reached level 3. GG!

acoustic marsh
#

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

acoustic marsh
#

@sharp charm do you have any idea possibly, my code is in the .txt above if you want to take a look

rich kettle
#

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

acoustic marsh
#

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 ❤️

acoustic marsh