How should I store wxStrings in memory? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
psion_revo
Earned a small fee
Earned a small fee
Posts: 12
Joined: Sun Feb 07, 2016 10:13 am

How should I store wxStrings in memory?

Post by psion_revo » Sun Feb 14, 2016 11:00 am

Hi,
I'm new in C++.
I need to load string values from a database table into an array.
And then I need to get a string value pointer by an index with function get().
Here is my class.

Code: Select all

class db_setup {
public:
  db_setup(wxSQLite3Database * db);
  virtual ~db_setup();
  //return pointer to wxString with index i in array
  wxString* get(int i); 
private:
  //pointer to array with wxString*
  wxString** a; 
  //array length
  int len; 
};
And implementation.

Code: Select all

//return pointer to wxString with index i in array
wxString* db_setup::get(int i){
  return *(a+(i*sizeof(wxString*)));
}
//
db_setup::db_setup(wxSQLite3Database * db){
  try {
    wxSQLite3Statement ls=db->PrepareStatement("select max(id)+1 from setup");
    wxSQLite3ResultSet   lr=ls.ExecuteQuery();
    len=atoi(lr.GetString(0));
    ls.Finalize();
    a=(wxString**) malloc(len*sizeof(wxString*));
    ls=db->PrepareStatement("select id,data from setup order by id");
    lr=ls.ExecuteQuery();
    while (lr.NextRow()){
      wxString** lp= (a+(lr.GetInt(0)*sizeof(wxString*)));


      //here I need to malloc new wxString
      //fill it from lr.GetString(1)
      //and store pointer to *lp=


    }
    ls.Finalize();
  } catch (int le) {
   //
  }
}
//
db_setup::~db_setup(){
  free(a);
}
How should I store wxStrings in memory?
I can get a wxString length.
Then malloc length bytes to ldest.
Then copy every byte from wxString to *ldest++=...
But I'm thinking that I'm wrong.

Thanks.

User avatar
doublemax
Moderator
Moderator
Posts: 14519
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How should I store wxStrings in memory?

Post by doublemax » Sun Feb 14, 2016 11:09 am

Make your life easy and use an STL container like std::list or std::vector instead of managing everything yourself.
http://www.cplusplus.com/reference/vector/vector/

If you want to stay in the wxWidgets "world", use wxArrayString to store strings.
http://docs.wxwidgets.org/trunk/classwx ... tring.html
Use the source, Luke!

psion_revo
Earned a small fee
Earned a small fee
Posts: 12
Joined: Sun Feb 07, 2016 10:13 am

Re: How should I store wxStrings in memory?

Post by psion_revo » Sun Feb 14, 2016 11:21 am

doublemax wrote:Make your life easy
Thanks!

But I want a faster life too :)
I'm not sure, that vector, list, etc. will be faster than wxString * array[].
I want a direct access if the array will not grow.
Am I wrong?
Should I forget about direct access?

User avatar
doublemax
Moderator
Moderator
Posts: 14519
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How should I store wxStrings in memory?

Post by doublemax » Sun Feb 14, 2016 11:28 am

Accessing elements in a std::vector is fast:
Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.
In general, when using C++ you will have to forget about counting clock-cycles, because performance is wasted everywhere. Concentrate on optimizing for speed where it's crucial.

But as a former assembly programmer, i know the pain ;)
Use the source, Luke!

psion_revo
Earned a small fee
Earned a small fee
Posts: 12
Joined: Sun Feb 07, 2016 10:13 am

Re: How should I store wxStrings in memory?

Post by psion_revo » Sun Feb 14, 2016 11:45 am

doublemax wrote:But as a former assembly programmer, i know the pain
My first prog was for PDP11. And next was Pascal.
I want to use C++ everywhere in my projects (for AVR,Win,Linux) to share parts of my code.
Hope, I will feel good inside C++ before my last day :)
Your help saves hours for me. Have a good health!

Post Reply