Results 1 to 4 of 4

Thread: Recommend memory management literature

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2009
    Location
    Belgrade, Serbia
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: Recommend memory management literature

    First thanks for your answer. I was starting to think that nobody will answer. Second sorry for posting in the wrong topic.

    Concerning parent role I am aware it is a Qt thing. What I found on the web is pretty much when parent destructor is called it goes though the list of its children and deletes them all. I was hoping to find something more detailed but this will have to do.

    Now concerning me shooting myself in the foot. The example above concerning the weird behavior of copy constructor is not something I have ever done. I am aware that it is dangerous because it will lead to confusion and for almost any practical code it is down right stupid. I am not trying to be difficult just trying to understand what is happening under the hood so to speak.

    I don't know if it is legal to type part of the Lipmmans book so I will try to make my own example.

    Let say that we have created class Book.

    Qt Code:
    1. class Book
    2. {
    3. public:
    4. Book(std:string);
    5.  
    6. private:
    7. std::string sName;
    8. };
    9.  
    10. //Constructor version one
    11.  
    12. Book::Book (std::string name) : sName(name)
    13. {
    14. //some code
    15. }
    16.  
    17. //Constructor version two
    18.  
    19. Book::Book (std::string name)
    20. {
    21. sName = name;
    22. //somecode
    23. }
    To copy to clipboard, switch view to plain text mode 

    In first constructor sName is initialized using std::string copy constructor. The program then enters the constructors body and execute rest of the code. In second constructor sName is initialized using std::string default constructor. Only after program enters the constructor body and change sName value using the std::string assignment operator. I was trying to write about this in my previous post but I failed and I apologize for that.

    Wysota, sorry for putting you on the spot like this I mean no disrespect, but you are no doubt much better programmer than me. And things like this are probably trivial to you. Like if exception is thrown during construction of an object that object is only partially constructed and is in invalid state. I only read this on the net but I don't know what to do if this happens.

    What I know are two things:

    1. Proper memory management is important. Most runtime error occur because of improper memory management.

    2. I don't know how to manage memory correctly.

    I am currently working on an application which need to dynamically manage list of Job object that I will create. Idea that I have and this is just and idea because I will probably find better ways to implement the final desing is following.

    Every job object should be some king of widget that has internal data, buttons for opening the dialogs for changing jobs internal data, tree view to show that internal data after the successful change. All job objects should be in a container in something that should be like a parent layout. Parent layout should have button to add new job, button to delete the last job from the container. Also parent layout should draw only the jobs that exist. When the job is deleted it should be removed from the window. The parent layout for job objects should look something like this and be part of the larger window:

    Qt Code:
    1. | |
    2. | AddJobbutton RemoveJobButtom |
    3. | |
    4. | |
    5. | ________________ |
    6. | | | EditJobButton11 |
    7. | | TreeView1 | EditJobButton12 |
    8. | |_______________| |
    9. | |
    10. | ________________ |
    11. | | | EditJobButton21 |
    12. | | TreeView2 | EditJobButton22 |
    13. | |_______________| |
    14. |_________________________________________________|
    To copy to clipboard, switch view to plain text mode 

    I don't know how to done this right. First version of my application will have fixed number of jobs because I don't know if I will be able to ensure integrity of my application if some "genius" user starts just adding and deleting jobs. Most probably the container in which I decide to keep my Job objects will deal with this.

    Qt Code:
    1. if ( container.size() < maxSize) //One of Qt container class haven't decided yet which one is the best for my application
    2. container.add(new Job);
    3. if ( !container.empty()) //Probably don't need to check this
    4. container. delete();
    To copy to clipboard, switch view to plain text mode 

    If I am boring and annoying I apologies. I am just trying to learn and I am aware that most of my questions are not very interesting for you. And by you here I don't me only Wysota but all of the experience programmers on this forum. You know all this stuff. I, on the other hand, am still learning.

    So if you could recommend to me literature concerning proper memory management which is not out dated and hopefully is relevant for Qt development I would appreciate it very much.

    Sorry if my post was to long. Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Recommend memory management literature

    Quote Originally Posted by frenk_castle View Post
    I was hoping to find something more detailed but this will have to do.
    Qt's source code is available for public, just look there.

    Now concerning me shooting myself in the foot. The example above concerning the weird behavior of copy constructor is not something I have ever done. I am aware that it is dangerous because it will lead to confusion and for almost any practical code it is down right stupid. I am not trying to be difficult just trying to understand what is happening under the hood so to speak.
    Ok, but giving an argument that you might implement "i++" to pre-substract a value is not an argument for me - you want to do weird things, do it, that's your choice.

    In first constructor sName is initialized using std::string copy constructor. The program then enters the constructors body and execute rest of the code. In second constructor sName is initialized using std::string default constructor. Only after program enters the constructor body and change sName value using the std::string assignment operator.
    Yes, that's true. The second version is a bit slower (might be much slower depending how expensive constructing an empty object is in a particular case).

    but you are no doubt much better programmer than me
    You don't know that. Believe in yourself.

    Like if exception is thrown during construction of an object that object is only partially constructed and is in invalid state. I only read this on the net but I don't know what to do if this happens.
    That's why we tend to avoid exceptions in C++ because they are broken by definition.

    1. Proper memory management is important. Most runtime error occur because of improper memory management.
    Not really. Most runtime errors occur because somebody assumed something which wasn't actually true. Memory management is quite trivial these days, especially with such great tools as Valgrind and debuggers.

    I am currently working on an application which need to dynamically manage list of Job object that I will create.
    So encapsulate everything in a class and forget about memory management.

    I don't know how to done this right.
    In the first iteration make a try with something you think is fine right now. Then when you learn more about particular case, go back and redesign your architecture. The first iteration will be a prototype, the second will be a final (or almost final) result.

    First version of my application will have fixed number of jobs because I don't know if I will be able to ensure integrity of my application if some "genius" user starts just adding and deleting jobs.
    Don't care too much about border cases in the beginning. That's what we do testing for to discover them.

    If I am boring and annoying I apologies.
    No, although this post is quite long...

    I am just trying to learn and I am aware that most of my questions are not very interesting for you.
    If that was the case, I wouldn't be answering.

    You know all this stuff.
    You'd be surprised

    So if you could recommend to me literature concerning proper memory management which is not out dated and hopefully is relevant for Qt development I would appreciate it very much.
    You already have Lippman. That's all you need.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Memory debugging in windows
    By txandi in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2009, 13:45
  2. Memory management
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2008, 20:48
  3. Memory management questions (im new to Qt)
    By scarvenger in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2007, 07:41
  4. Memory management in QT
    By Gayathri in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2006, 07:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.