string - C++ copy of char* data in struct to another struct -


I have a problem when I try to copy data from one strat here is the structure

  struct connection_details {char * server; Four * users; Four * password; Four * databases; };   

And what I'm trying to do is to copy the data from one object to another (connection_setup is a personal connection_details object) This is the code from the second object creator :

  Settings * tmp = new settings (); This- & gt; Connection_setup.server = strdup (tmp-> getSQLSettings (.) Server); Removing TMP;   

I maintain the fault of division which makes sense because I probably touch the stuff that I should not do.

Actually both the settings object and the object in which I am a private member variable of type connection_details I.e

  class setting {public: settings (); ~ (Setting); Connection_details getSQLSettings (); Private: connection_details sqldetails; };   

Thanks for any advice!

First of all, you are using it for new and free, this is a very bad idea because in general the district You will not be invited to take place for free with the removal.

In addition, you are getting ownership for that return value of that streamup, even then you do not free it anywhere, but hopefully it is done in an ineffective code.

The problem here is that a copy has been made for the return of the acquired SQL method, which will make the copy of the indicator, so that you now get two pointers.

This is a problem because when it is destroyed for the first time, it also expects that the pointer will be destroyed in your destructor, and when the other will run, you will try to remove a hanging indicator ... Perhaps it is possible that you are having an accident.

There are several ways to resolve this problem:

  • Return references to avoid duplicates.
  • Make a proper copy to copy the data, and not the pointer.
  • Use high level C ++ elements, such as std :: string, or at least smart pointers, etc.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -