« first day (1975 days earlier)      last day (884 days later) » 

2:20 AM
Is there a way to store the result of a command for example "glxinfo" to a std::string
Linux specific
 
 
5 hours later…
6:56 AM
Besides we can reassign the passed pointer to another object, is there any other significant benefit of passing a pointer as a reference (as follows)? Any comments are welcome (ping me).
void foo(int* & refptr);
void goo(int* ptr);
Pointers of any types have the same size so passing by value has no significant impact compared to passing by reference. Right?
 
 
2 hours later…
8:55 AM
@Agent_A Yes, but it's somewhat indirect. auto rdr = popen("glxinfo", "r"); will run glxinfo with its output going to a pipe, which is captured in the parent via a C-style FILE *. Using that, you can read the data into a buffer, and append buffers of data to a string.
If you're going to do this frequently, it's fairly easy to create a streambuffer that reads from a C-style FILE *, and provides it to the rest of the program as an istream. That may be worthwhile if you're doing this a lot, but if you're only doing it once, it's probably overkill.
 
9:16 AM
What is the correct way of passing a reference to an array of objects, where the object takes a reference?
#include <iostream>
#include <boost/asio.hpp>

using namespace boost::asio;

struct A
{
    int a;
};

struct B
{
    io_context& ioCtxt;
    ip::tcp::socket sock;
    B(io_context& ioCtxtFromContainer):ioCtxt(ioCtxtFromContainer), sock(ioCtxtFromContainer){}
};

struct container
{
    io_service ioCtxt;

    A aObj;

    std::array<B,2> bObjs = {std::ref(this->ioCtxt), nullptr};
};

int main(void)
{
    container containerObj;

    return 0;
}
 
 
3 hours later…
12:37 PM
Hello all simple question from a newbie to c++
I am receiving this error:
error: invalid operands to binary expression ('std::__1::basic_istream<char>' and 'int')
        if (infile.read(reinterpret_cast<char*>(this), sizeof(*this))>0)
from this function
void Account::read_record() {
    ifstream infile;
    infile.open("record.bank", ios::binary);
    if (!infile) {
        cout << "Error: File Not Found." << endl;
        return;
    }
    cout << "\n**** Data from file ****" << endl;
    while(!infile.eof()) {
        if (infile.read(reinterpret_cast<char*>(this), sizeof(*this))>0)
        {
            show_data();
        }
    }
    infile.close();
}
I cannot for the life of me figure out what is wrong and why this error is arrising
 
that... is a lot of undefined behavior
 
@Mgetz what do you mean
 
I'm 99% sure that Account isn't a POD object that is valid to memcpy and even if it was that would be non-portable anyway due to compiler versioning issues
but your real clue is the reinterpret_cast<char*>
but the error you're getting is happening because read returns std::istream
 
@Mgetz account in this case is a custom class is it perchance I have caused namespace error?
so essentially I am trying to compare integer vs std::istream instead of the size of said i stream
 
A) please don't ping reply when it's not necessary. only use them when there are multiple conversations going on or the reply is to something multiple posts up and the context is necessary
b) that's irrelevant
@Kwsswart Yes
 
12:46 PM
will do ok thank you will try figure out how tog et the size of it instead of the physical stream
 
my point was more that passing something like a whole class to istream.read is extremely dangerous
 
user8616480
if (infile.read(reinterpret_cast<char*>(this), sizeof(*this))>0) is totally incorrect .. why are you nor having a buffer seperately for file...
 
because C++ has very clear rules about what can and cannot be memcpy'd and this falls most likely out of that
 
user8616480
instead of this , you shouold use a char* buffer and allocate it for correct size
 
note: the while loop is also very wrong
17
A: Why does std::fstream set the EOF bit the way it does?

Evan TeranBecause this way it can detect EOF without knowing how large the file is. All it has to do is simply attempt to read and if the read is short (but not an error), then you have reached the end of the file. This mirrors the functionality of the read system call, which file IO typically ends up cal...

 
12:52 PM
hmm I get the feeling I have tried to run before walking here: usually work in python and trying to go over to c++ to learn more and decided to try build something to learn more after a few basic entry level things will need to reade more into all this before attempting more I think
 
just an FYI: if you are using reinterpret_cast and you're not extremely sure that it's the right thing to do because you're a power user and have looked for alternatives... you're probably doing something wrong.
 
Ok thank you appreciate the guidance
 
The object layout in C++ is not quite that easy and it doesn't have any extensive runtime reflection system.
That's why you can't just inspect and serialize objects like with pickle in Python
you need either a very restrictive set of object types or you need to proved the type metadata yourself
 
1:17 PM
Non pointer variables passed to a function by value cannot be changed inside the function. But it is not the case for pointer variables passed by value. To prevent the internal code from changing the pointer variable passed by value, should we always use a prefix const, for example foo(const int* param)?
 
Dumb question: Why would you pass an int as a pointer?
or even by reference
 
@Mgetz I will NOT pass a non-array variable of type int but an array of type int.
 
Please don't ping reply unless the post you're replying too is several posts up or there are multiple conversations going on and you need to give context
...
then use a const std::span
or if you can't use C++ const gsl::span
there are exceptions for ABI level stuff
but in general favor not losing array length and passing random pointers around
derp that should be "if you can't use C++20"
 
OK.Thank you. :-)
 
 
1:45 PM
Hello everyone! It is my first time coming to this lounge. I love C++ and will likely keep lurking here. For my first question, I was wondering if it is correct to say that "C++ compiles to machine code".
 
@MangaD you can compile C++ to machine code. It's not a requirement though. You can configure some compilers to output web-assembly, only compile to assembly, or even other languages like C.
 
2 messages moved from Lounge<C++>
C++ compiles to a target based on the C++ abstract machine. This lets the compilers have different results based on the target architecture.
 
Great answer, thank you! I just noticed that there are two chats for C++. This one is for questions and answers. What is the lounge used for?
 
lounging probably
 
pretty much that
 
1:52 PM
all right ^^'
 
this was on the border of what I generally don't move or do move. But If figured if you were new this would get you here anyway
so you'd know both channels exist
 
Thank you!
 
 
6 hours later…
8:24 PM
Keil MDK Arm uVision IDE produces assembly when you debug C++, where does the assembly come from? I don't see any assembly produced in Visual Studio debugger
@PeterT how to configure visual studio to produce assembly from C++?
 
The assembly inside the debugger is some disassembly from the machine code
the Visual Studio debugger can show you the disassembly just fine, just open the window that's called that
 

« first day (1975 days earlier)      last day (884 days later) »