Dynamic multidimensional array, deallocation of pointer not malloced..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • welch.ryan@gmail.com

    #16
    Re: Dynamic multidimensiona l array, deallocation of pointer not malloced..

    On May 12, 4:49 pm, Ian Collins <ian-n...@hotmail.co mwrote:
    welch.r...@gmai l.com wrote:
    >
    Interesting.. okay, so let's suppose top is reporting it incorrectly,
    and that code actually does truly successfully allocate all of that
    memory. Then the question is, why is it that my original code (using a
    multidimensiona l approach) fails, yet allocating it as one large block
    seems to work?
    >
    The evidence is building a good case for a bug in your allocator, time
    to try a tool/platform specific forum to see it it is a known problem.
    >
    --
    Ian Collins.
    I'm starting to think you're right.. any suggestions for such a forum?

    Comment

    • welch.ryan@gmail.com

      #17
      Re: Dynamic multidimensiona l array, deallocation of pointer not malloced..

      On May 12, 4:10 pm, Branimir Maksimovic <b...@hotmail.c omwrote:
      On May 12, 6:35 pm, welch.r...@gmai l.com wrote:
      >
      >
      >
      On May 11, 9:15 pm, Branimir Maksimovic <b...@hotmail.c omwrote:
      >
      Try following and see if works:
      >
      const unsigned rows = 635000;
      const unsigned cols = 2350;
      int (*p)[cols] = new int[rows][cols];
      for(unsigned i = 0; i<rows;++i)
      {
      for(unsigned j=0;j<cols;++j)
      p[i][j] = 0;
      }
      cin.get();
      delete[] p;
      >
      Greetings, Branimir
      >
      This is what happens:
      >
      HugeMemory2.cpp :4: error: expected unqualified-id before 'for'
      HugeMemory2.cpp :4: error: expected constructor, destructor, or type
      conversion before '<' token
      HugeMemory2.cpp :4: error: expected unqualified-id before '++' token
      HugeMemory2.cpp :9: error: expected constructor, destructor, or type
      conversion before '.' token
      HugeMemory2.cpp :10: error: expected unqualified-id before 'delete'
      >
      I thought maybe it was because there's no 'int' after 'unsigned' but
      that didn't help. :(
      >
      Strange. Are you sure you entered code correctly?
      I guess that error is triggered by something else in your code,
      as I tried with comeau online, g++ 3.4.4 and latest vc++.
      >
      Greetings, Branimir.
      Maybe it's a bad line ending or something. I can't find my handy dandy
      perl script to fix them..

      Comment

      • Paul

        #18
        Re: Dynamic multidimensiona l array, deallocation of pointer not malloced..


        <welch.ryan@gma il.comwrote in message
        news:1178928147 .269919.77720@q 75g2000hsh.goog legroups.com...
        Hi all,
        >
        Having a problem with addressing large amounts of memory. I have a
        simple piece of code here that is meant to allocate a large piece of
        memory on a ppc64 machine. The code is:
        >
        /*
        Test to see what happens when we try to allocate a massively huge
        piece of memory.
        */
        >
        #include <iostream>
        #include <string>
        #include <stdexcept>
        using namespace std;
        >
        int main(int argc, char** argv) {
        cout << "Attemping to allocate.." << endl;
        >
        const int ROWS = 635000;
        const int COLS = 2350;
        >
        // Allocate.
        try {
        int** test = new int*[ROWS];
        for (int i = 0; i < ROWS; i++) {
        test[i] = new int[COLS];
        for (int j = 0; j < COLS; j++) {
        test[i][j] = 0;
        }
        }
        >
        How long does it take for your code to run? I'm asking this because the
        code you posted seems very inefficient.

        You are invoking "new[]" 635,000 times. An alternative is to make only two
        calls to "new[]". One new[] for the row pointers, and the second new[] to
        allocate the pool of memory for the int data. Then in the loop, you point
        the row pointers in the right place in the int data pool.

        Not only will this more than likely bypass your problem with the allocator,
        your code would more than likely see a significant increase in speed, both
        in allocation and in deallocation (at least in this area of code). However,
        you should test this (but I would be very surprised if there isn't a
        significant speed increase)

        Here are the internals of your code snippet rewritten making only two calls
        new[] and then two calls to delete[] to deallocate the memory.

        int *pool;
        int** test = new int*[ROWS]; // allocate for row pointers
        pool = new int [ROWS * COLS]; // allocate memory pool for data
        for (int i = 0; i < ROWS; i++)
        {
        test[i] = pool;
        pool += COLS;
        }

        // Deallocate.
        delete [] test[0]; // deallocate pool
        delete [] test; // deallocate row pointers.

        - Paul




        Comment

        • welch.ryan@gmail.com

          #19
          Re: Dynamic multidimensiona l array, deallocation of pointer not malloced..

          You're right, that's definitely way more efficient. For my instance,
          though, I perform one massive allocation initially and then the code
          can run for hours to weeks, so the initial time for allocation wasn't
          a real issue for me.

          However, the interesting thing is, I don't get that malloc() error
          anymore upon deletion. I still don't know why that was happening
          originally in the first place, but at least this method works.

          Thanks for the help everyone, I really appreciate it!

          Cheers,
          Ryan

          Comment

          Working...