Problem with control arrays
-
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 setvoid 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 -
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 setvoid 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. ;-PI just had this problem yesterday (sorta). Instead I used MFC
CArray
and the problem I was having was thatCButton
doesn't have a default copy constructor and I wasn't interested in writting one, so I did what was most obvious :) UseCArray
like this:CArray<CButton*, CButton*> m_pButtons;
I'm drinking triples, seeing double and acting single :cool:
-
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 setvoid 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. ;-PMost 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 thatrealloc
, when moving to a larger block of memory, copies the original contents as if withmemcpy
, and this is not guaranteed to work for types which provide their own copy constructors. My suggestion is that you store pointers toCButton
instead ofCButton
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 aboutrealloc
. C++ provides for instancestd::vector
which does the work in a far easier way. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
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 thatrealloc
, when moving to a larger block of memory, copies the original contents as if withmemcpy
, and this is not guaranteed to work for types which provide their own copy constructors. My suggestion is that you store pointers toCButton
instead ofCButton
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 aboutrealloc
. C++ provides for instancestd::vector
which does the work in a far easier way. Joaquín M López Muñoz Telefónica, Investigación y DesarrolloIt 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 -
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. ;-PI 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