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. Problem with control arrays

Problem with control arrays

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiondata-structuresperformancetutorial
5 Posts 3 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.
  • S Offline
    S Offline
    Stephan Poirier
    wrote on last edited by
    #1

    Hi everyone! I need to create a button array that can be resized. I tought it was easy like in VB but I got a problem. I'm unable to create buttons! For example, I placed this is my header file : //Pointer to button array CButton* myButton; //Hold number of buttons in the list int nbButtons; //Add a button in the list void InsertButton(LPCTSTR lpszText); Then in the implementation file, I've set void CLinkList::InsertButton(LPCTSTR lpszText) { // Memory allocation ( preserve existing!! ) if(nbButtons<1) // First memory allocation myButton=(CButton*)malloc(sizeof(CButton)*(nbButtons+1)); else // Dynamic realoccation myButton=(CButton*)realloc(myButton, sizeof(CButton)* (nbButtons+1)); // Set location CRect rect(0,nbButtons*25,60,20); // Create the button control myButton[nIndex].Create( lpszText, WS_VISIBLE | WS_CHILD, rect, this, GetDlgCtrlID() ); // Increment number of buttons in the list nbButtons++; } But I'm not able to create any button. I didn't get any assertion error but it stop within the CButton::Create function. I don't have any idea of what is the problem. I know that if I create a static array like : CButton myButton[30]; it works very well... What's wrong with the way I do, and how can I solve it? Notice that I want my array to be resized because I don't want to hold a "big" static array. And probably because I'm curious to know how it works too! :-D It will be very appreciated if somebody here could help me!! Stef Progamming looks like taking drugs... I think I did an overdose. ;-P

    A J 2 Replies Last reply
    0
    • S Stephan Poirier

      Hi everyone! I need to create a button array that can be resized. I tought it was easy like in VB but I got a problem. I'm unable to create buttons! For example, I placed this is my header file : //Pointer to button array CButton* myButton; //Hold number of buttons in the list int nbButtons; //Add a button in the list void InsertButton(LPCTSTR lpszText); Then in the implementation file, I've set void CLinkList::InsertButton(LPCTSTR lpszText) { // Memory allocation ( preserve existing!! ) if(nbButtons<1) // First memory allocation myButton=(CButton*)malloc(sizeof(CButton)*(nbButtons+1)); else // Dynamic realoccation myButton=(CButton*)realloc(myButton, sizeof(CButton)* (nbButtons+1)); // Set location CRect rect(0,nbButtons*25,60,20); // Create the button control myButton[nIndex].Create( lpszText, WS_VISIBLE | WS_CHILD, rect, this, GetDlgCtrlID() ); // Increment number of buttons in the list nbButtons++; } But I'm not able to create any button. I didn't get any assertion error but it stop within the CButton::Create function. I don't have any idea of what is the problem. I know that if I create a static array like : CButton myButton[30]; it works very well... What's wrong with the way I do, and how can I solve it? Notice that I want my array to be resized because I don't want to hold a "big" static array. And probably because I'm curious to know how it works too! :-D It will be very appreciated if somebody here could help me!! Stef Progamming looks like taking drugs... I think I did an overdose. ;-P

      A Offline
      A Offline
      alex barylski
      wrote on last edited by
      #2

      I just had this problem yesterday (sorta). Instead I used MFC CArray and the problem I was having was that CButton doesn't have a default copy constructor and I wasn't interested in writting one, so I did what was most obvious :) Use CArray like this:

      CArray<CButton*, CButton*> m_pButtons;

      I'm drinking triples, seeing double and acting single :cool:

      1 Reply Last reply
      0
      • S Stephan Poirier

        Hi everyone! I need to create a button array that can be resized. I tought it was easy like in VB but I got a problem. I'm unable to create buttons! For example, I placed this is my header file : //Pointer to button array CButton* myButton; //Hold number of buttons in the list int nbButtons; //Add a button in the list void InsertButton(LPCTSTR lpszText); Then in the implementation file, I've set void CLinkList::InsertButton(LPCTSTR lpszText) { // Memory allocation ( preserve existing!! ) if(nbButtons<1) // First memory allocation myButton=(CButton*)malloc(sizeof(CButton)*(nbButtons+1)); else // Dynamic realoccation myButton=(CButton*)realloc(myButton, sizeof(CButton)* (nbButtons+1)); // Set location CRect rect(0,nbButtons*25,60,20); // Create the button control myButton[nIndex].Create( lpszText, WS_VISIBLE | WS_CHILD, rect, this, GetDlgCtrlID() ); // Increment number of buttons in the list nbButtons++; } But I'm not able to create any button. I didn't get any assertion error but it stop within the CButton::Create function. I don't have any idea of what is the problem. I know that if I create a static array like : CButton myButton[30]; it works very well... What's wrong with the way I do, and how can I solve it? Notice that I want my array to be resized because I don't want to hold a "big" static array. And probably because I'm curious to know how it works too! :-D It will be very appreciated if somebody here could help me!! Stef Progamming looks like taking drugs... I think I did an overdose. ;-P

        J Offline
        J Offline
        Joaquin M Lopez Munoz
        wrote on last edited by
        #3

        Most likely the problem lies in your use of realloc. Simply put, realloc won't work except if the types contained in the array are primitive (integer types, pointers, little more.) The reason is that realloc, when moving to a larger block of memory, copies the original contents as if with memcpy, and this is not guaranteed to work for types which provide their own copy constructors. My suggestion is that you store pointers to CButton instead of CButton objects. This implies an extra allocation per button:

        myButton[nIndex]=new CButton;
        myButton[nIndex]->Create(...);

        and don't forget to delete these objects when done. Also, do yourself a favor and forget about realloc. C++ provides for instance std::vector which does the work in a far easier way. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        S 1 Reply Last reply
        0
        • J Joaquin M Lopez Munoz

          Most likely the problem lies in your use of realloc. Simply put, realloc won't work except if the types contained in the array are primitive (integer types, pointers, little more.) The reason is that realloc, when moving to a larger block of memory, copies the original contents as if with memcpy, and this is not guaranteed to work for types which provide their own copy constructors. My suggestion is that you store pointers to CButton instead of CButton objects. This implies an extra allocation per button:

          myButton[nIndex]=new CButton;
          myButton[nIndex]->Create(...);

          and don't forget to delete these objects when done. Also, do yourself a favor and forget about realloc. C++ provides for instance std::vector which does the work in a far easier way. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          S Offline
          S Offline
          Stephan Poirier
          wrote on last edited by
          #4

          It doesn't work... It stop at: myButton[nIndex]=new CButton; I have declared myButton like this : CButton** myButton; Any Idea. I have an headhache so, my brain doesn't work as much as I want !:cool: Progamming looks like taking drugs... I think I did an overdose. ;-P

          S 1 Reply Last reply
          0
          • S Stephan Poirier

            It doesn't work... It stop at: myButton[nIndex]=new CButton; I have declared myButton like this : CButton** myButton; Any Idea. I have an headhache so, my brain doesn't work as much as I want !:cool: Progamming looks like taking drugs... I think I did an overdose. ;-P

            S Offline
            S Offline
            Stephan Poirier
            wrote on last edited by
            #5

            I get it!! :eek: I just forgot size the array of CButton pointers... myButton = new CButton*; myButton[nIndex] = new CButton; Now, I just have to find out how to resize it ! Tanks a lot Stef Progamming looks like taking drugs... I think I did an overdose. ;-P

            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