Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Proper array memory management in C++

Proper array memory management in C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structuresperformancehelptutorial
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Don Guy
    wrote on last edited by
    #1

    I have an application that randomly displays images on a screen. There's a capture button, and when user clicks on that button, the image that is currently displayed is stored into a buffer. User can click the button 5 times. i.e., the buffer should be storing upto 5 image data. I have code like this, .h File: unsigned char* m_ImageBuffer[5]; .cpp File: while (1) { ---------- ---------- m_ImageBuffer[counter] = new unsigned char[size]; memcpy(m_ImageBuffer[counter], pImageData, size); ---------- ---------- } The variable "counter" goes from 0 to 4. Inside the WHILE loop a bunch of images are constantly displayed in the screen. The purpose of m_ImageBuffer is that, i will store the user selected image data in this buffer, so that it could be later used for display. i.e., in a different screen. The problem with the code in WHILE loop is that, i am not able to use DELETE properly. So the code logic basically ends up eating so much memory. I am looking for a better logic, by which i can store the image data, but still does better memory management. Not sure the problem is explained properly. I am expecting some suggestions on how to manage this situation. Thanks in advance.

    L M M 3 Replies Last reply
    0
    • D Don Guy

      I have an application that randomly displays images on a screen. There's a capture button, and when user clicks on that button, the image that is currently displayed is stored into a buffer. User can click the button 5 times. i.e., the buffer should be storing upto 5 image data. I have code like this, .h File: unsigned char* m_ImageBuffer[5]; .cpp File: while (1) { ---------- ---------- m_ImageBuffer[counter] = new unsigned char[size]; memcpy(m_ImageBuffer[counter], pImageData, size); ---------- ---------- } The variable "counter" goes from 0 to 4. Inside the WHILE loop a bunch of images are constantly displayed in the screen. The purpose of m_ImageBuffer is that, i will store the user selected image data in this buffer, so that it could be later used for display. i.e., in a different screen. The problem with the code in WHILE loop is that, i am not able to use DELETE properly. So the code logic basically ends up eating so much memory. I am looking for a better logic, by which i can store the image data, but still does better memory management. Not sure the problem is explained properly. I am expecting some suggestions on how to manage this situation. Thanks in advance.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      At the beginning of the program initialise the buffer so all pointers are NULL. Then every time you save an image, check if the pointer is not null and if so delete[] it. Something like:

      if (m_ImageBuffer[counter] != NULL)
      delete[] m_ImageBuffer[counter];
      m_ImageBuffer[counter] = new unsigned char[size];
      memcpy(m_ImageBuffer[counter], pImageData, size);

      Veni, vidi, abiit domum

      1 Reply Last reply
      0
      • D Don Guy

        I have an application that randomly displays images on a screen. There's a capture button, and when user clicks on that button, the image that is currently displayed is stored into a buffer. User can click the button 5 times. i.e., the buffer should be storing upto 5 image data. I have code like this, .h File: unsigned char* m_ImageBuffer[5]; .cpp File: while (1) { ---------- ---------- m_ImageBuffer[counter] = new unsigned char[size]; memcpy(m_ImageBuffer[counter], pImageData, size); ---------- ---------- } The variable "counter" goes from 0 to 4. Inside the WHILE loop a bunch of images are constantly displayed in the screen. The purpose of m_ImageBuffer is that, i will store the user selected image data in this buffer, so that it could be later used for display. i.e., in a different screen. The problem with the code in WHILE loop is that, i am not able to use DELETE properly. So the code logic basically ends up eating so much memory. I am looking for a better logic, by which i can store the image data, but still does better memory management. Not sure the problem is explained properly. I am expecting some suggestions on how to manage this situation. Thanks in advance.

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        Proper c++ memory management would tell you to use standard c++ classes and collections instead of arrays and pointers to data. Change the array to either a std::vector or a std::array (if c++11). If possible I would replace the unsigned char* with a proper image class (if on Windows, CImage, CBitmap, ... ) If you need something portable and want to keep the unsigned char* , create a wrapper class around it with proper constructor/destructor/copy/assignment methods.

        I'd rather be phishing!

        J 1 Reply Last reply
        0
        • D Don Guy

          I have an application that randomly displays images on a screen. There's a capture button, and when user clicks on that button, the image that is currently displayed is stored into a buffer. User can click the button 5 times. i.e., the buffer should be storing upto 5 image data. I have code like this, .h File: unsigned char* m_ImageBuffer[5]; .cpp File: while (1) { ---------- ---------- m_ImageBuffer[counter] = new unsigned char[size]; memcpy(m_ImageBuffer[counter], pImageData, size); ---------- ---------- } The variable "counter" goes from 0 to 4. Inside the WHILE loop a bunch of images are constantly displayed in the screen. The purpose of m_ImageBuffer is that, i will store the user selected image data in this buffer, so that it could be later used for display. i.e., in a different screen. The problem with the code in WHILE loop is that, i am not able to use DELETE properly. So the code logic basically ends up eating so much memory. I am looking for a better logic, by which i can store the image data, but still does better memory management. Not sure the problem is explained properly. I am expecting some suggestions on how to manage this situation. Thanks in advance.

          M Offline
          M Offline
          Malli_S
          wrote on last edited by
          #4

          Apart from the solution that Richard has given, I'd like to suggest you to go through the code snippet from WHILE block and ensure that the array index variable 'counter' is increased properly whenever an image is selected. Because, looking at the code snippet you gave, the index variable 'counter' is not incremented immediately after allocation index element (or you didn't include that part while pasting the code). Looking at the logic you explained, it seems that you may not be deleting the image data in the while loop. Or are you? If the user has the choice of deselecting the image, you should be able to delete the respective indexed image item, and should maintain the empty slot index data. That involves extra bookkeeping. I would suggest to give a try using STL container classes. For that, this[^][^] may help you. If possible, provide the full WHILE loop code snippet. That will help to find out memory leak (as your app is ending up much memory).

          [Delegates]      [Virtual Desktop]      [Tray Me !]
          -Malli...! :rose:****

          1 Reply Last reply
          0
          • M Maximilien

            Proper c++ memory management would tell you to use standard c++ classes and collections instead of arrays and pointers to data. Change the array to either a std::vector or a std::array (if c++11). If possible I would replace the unsigned char* with a proper image class (if on Windows, CImage, CBitmap, ... ) If you need something portable and want to keep the unsigned char* , create a wrapper class around it with proper constructor/destructor/copy/assignment methods.

            I'd rather be phishing!

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            Maximilien wrote:

            Proper c++ memory management would tell you to use standard c++ classes and collections...

            None those suggestions however would fix the problem of increasing memory usage. Where the first response does.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups