Results 1 to 8 of 8

Thread: memory allocation problem

  1. #1
    Join Date
    Apr 2011
    Posts
    58
    Thanks
    1

    Default memory allocation problem

    Hi.

    I do a memory allocation using 'new', as follow.

    Qt Code:
    1. double *myarray[50];
    2. for (int j = 0; j < 50; j++)
    3. myarray[j] = new double[100000];
    To copy to clipboard, switch view to plain text mode 

    When the codes first run it works. When I rerun it, (e.g. when user click the button again) the program crash. I tried to debug but couldnt find an answer. What is the right way to "trap" such memory allocation codes in QT? And is there a better way to allocate an array of pointers to double in QT?

    I also delete the allocation as needed by the following codes
    Qt Code:
    1. for (int j = 0; j < 50; j++)
    2. delete [] myarray[j]
    To copy to clipboard, switch view to plain text mode 

    Thanks!
    Last edited by Lykurg; 20th May 2011 at 07:31.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: memory allocation problem

    Ehm, that has nothing to do with Qt. It is all about basic C++. See any C++ reference, e.g. http://www.fredosaurus.com/notes-cpp...ynamalloc.html.


    And next time please use code tags when posting source code.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: memory allocation problem

    You "trap" program crashes with a debugger and then look at a backtrace of calls until you find your culprit. How you do that depends on your tool chain and is nothing to do with Qt. Depending on exception handlers in your code a failure to allocate space with new or new[] will terminate your program.

    You could use nested QVector or std::vector structures and save yourself the memory management but they may allocate more storage than you require because they allow for resizing.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: memory allocation problem

    hen I rerun it, (e.g. when user click the button again) the program crash.
    In addition to the previous posters, you are allocating a very large amount of memory (20MB per loop execution).
    If this code is running on an embedded system for example, it is possible you are running out of memory especially if you didn't delete the previous allocation.
    Not deleting the previous allocation before the new allocation happens, also can cause your application to crash.
    If 'new' fails, it returns NULL.
    Thus you can check your allocation before continuing, and handling the case gracefully instead of having the application crash on the assumption that allocation was successful.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: memory allocation problem

    My guess is that you're never getting to the delete, and the second time through there isn't another 20 mb to be had in the heap.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: memory allocation problem

    What was you debugging observations ?
    You need have some kind of exception handling, when dealing such big memory allocations.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: memory allocation problem

    Doubles are 8 bytes, by 50 sets of 100,000 is 40,000,000 bytes to allocate (in contiguous blocks of 800,000 bytes)

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: memory allocation problem

    Doubles are 8 bytes
    Ah... true, sorry.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Widget Memory Allocation
    By ArlexBee-871RBO in forum Qt Programming
    Replies: 5
    Last Post: 9th May 2010, 20:51
  2. Virtual memory allocation problem
    By morfei in forum Qt Programming
    Replies: 1
    Last Post: 27th August 2009, 12:30
  3. QDrag : memory allocation
    By kghose in forum Qt Programming
    Replies: 1
    Last Post: 14th August 2008, 23:57
  4. limit memory allocation
    By magland in forum General Programming
    Replies: 10
    Last Post: 23rd March 2007, 10:21
  5. vector memory allocation
    By TheKedge in forum General Programming
    Replies: 1
    Last Post: 23rd March 2006, 18:27

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.