Results 1 to 3 of 3

Thread: glibc detected main: malloc(): memory corruption: 0x0818f070*** on a simple program

  1. #1
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default glibc detected main: malloc(): memory corruption: 0x0818f070*** on a simple program

    why this part of code:

    Qt Code:
    1. int rows = 3, cols = 2, temp, j;
    2. double ** dataArray = new double* [rows];
    3. for(int i = 0; i < rows; ++i)
    4. dataArray[i] = new double(cols);
    5.  
    6. for (int i = 0; i < rows*cols; i++) {
    7. temp =int(i/2);
    8. j = i%2;
    9. dataArray[temp][j] = temp*j;
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    gives me the following error in Qt???

    &"warning: GDB: Failed to set controlling terminal: Invalid argument\n"
    *** glibc detected *** /home/tgf/qtExamples/cpp/main6-build-desktop/main6: malloc(): memory corruption: 0x0818f070 ***
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6(+0x6b591)[0xfe7591]
    /lib/tls/i686/cmov/libc.so.6(+0x6e395)[0xfea395]
    /lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x5c)[0xfebf9c]
    /usr/lib/libstdc++.so.6(_Znwj+0x27)[0xefec07]
    /usr/lib/libQtGui.so.4(+0x6ccc8d)[0x7fac8d

    ........ (lots of error lines like the above)

    I tried this part of code in Geany and it worked normally.

  2. #2
    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: glibc detected main: malloc(): memory corruption: 0x0818f070*** on a simple pro

    This has nothing to do with Qt. This is a C++ question.

    Line 4 is allocating a single double with the value cols, not doing what you think it is doing, i.e. allocating an array of cols doubles. Consequently you end up writing outside the bounds of the allocated memory. This will probably also cause issue when you try to delete[] the memory.

  3. The following user says thank you to ChrisW67 for this useful post:

    fatecasino (7th December 2010)

  4. #3
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: glibc detected main: malloc(): memory corruption: 0x0818f070*** on a simple pro

    thanks so much!
    I was struggling for 5+ hours to solve this and I couldn't see the obvious

    Qt Code:
    1. dataArray[i] = new double(cols);
    To copy to clipboard, switch view to plain text mode 

    I just wanted to create a 2d array of doubles (dynamic allocation of memory) and what happened is this (using Creator):
    when i typed "double" the editor automatically typed () thinking that it is a kind of a function. I didn't notice it, and after that, my eyes always were seeing () as [] !!!!
    Qt Code:
    1. dataArray[i] = new double[cols];
    To copy to clipboard, switch view to plain text mode 
    That's (and much more) what happens after continuous hours of coding. The strange bit is that Geany was not complaining at all and I thought it was a Qt thing.

Similar Threads

  1. Replies: 0
    Last Post: 29th September 2009, 01:16
  2. Replies: 3
    Last Post: 5th June 2009, 19:19
  3. *** glibc detected ***: free(): invalid pointer
    By codebehind in forum Qt Programming
    Replies: 4
    Last Post: 20th September 2008, 22:45
  4. glibc detected
    By raghvendramisra in forum Qt Programming
    Replies: 5
    Last Post: 28th May 2008, 03:29
  5. Error: **** glibc detected ****
    By Manohar in forum Qt Programming
    Replies: 2
    Last Post: 9th May 2008, 05:32

Tags for this Thread

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.