I have an issue when I try to copy char* data from one struct to another. Here is the struct
struct connection_details
{
char *server;
char *user;
char *password;
char *database;
};
And basically what I am trying to do is to copy the data from one object to another (connection_setup is a private connection_details object) this is the code from the constructor of another object:
settings *tmp = new settings();
this->connection_setup.server = strdup(tmp->getSQLSettings().server);
delete tmp;
I keep getting segmentation fault which is understandable since I probably touch stuff that I should not be doing.
Basically both the settings object and the object I am in contains a private member variable of the type connection_details. I.e
class settings {
public:
settings();
~settings();
connection_details getSQLSettings();
private:
connection_details sqldetails;
};
Thanks for any advise!
std::string? Would make that whole problem (and quite a few more) essentially vanish.connection_details getSQLSettings()toconnection_details& getSQLSettings()then it will work out fine. Alternatively, you can add a proper copy-constructor tostruct connection_details.std::strings for each of the 4 data members ofconnection_detailsgenerates some overhead due to the four-fold allocation and de-allocation. If this is performance critical, you should only make one allocation (and hence not usestd::string).std::stringis great if you don't care about efficiency.