Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array already filled? 1

Status
Not open for further replies.

russland

Programmer
Jan 9, 2003
315
CH
Hi,

QUESTION #1
Header:
QPushButton *pbArray[3][3];

CPP:
pbArray[3][3] = new QPushButton(this);

How can I check if I've already put a QPushButton into an array as I just did in the cpp file (pbArray[3][3] = new QPushButton(this);)?

I need to know because IF then I would loop and delete (deconstruktor) all objects (GUIs) in order to remove them from the screen.

QUESTION #2
Is there a better way to remove GUI objects/widgets in C++ from the parent widget than looping end 'deconstruct'?
 
Set all pointers in the array to NULL before using them.
 
Question #1: Your array holds pointers that are probably not initialized. Valid subscripts are [0][0], [0][1], [0][2], [1][0], [1][1], [1][2], [2][0], [2][1], and [2][2]. The [3][3] position that you attempted to use is out of bounds of the array and will probably lead to a crash.

As for your real question, the best way to do this would be to initialize the contents of the array to 0 (null pointers). Then after you have added a couple QPushButtons to the array you can check to see if the pointer is 0 to know whether you have added the QPushButton to that location. When you delete the pointers in the array, it is safe to delete a null pointer, so you don't even have to check at that point if you initialize them all to null in the beginning.
 
set them to null works like this?
pbArray[3][3] = null;

any clue on how to verify if there something inside?
 
okay
- i checked for 0 (not null)
- i hide it (hide())
- i set it to 0

do I still have to "destruct" it after setting it to 0?

Thanks to both of you.
 
No, the code you added again accesses memory outside the bounds of the array.

To set them all to null you could do several things depending on whether this array is a member of a class or a global variable, etc. Hopefully it is a member of the class. If it is, then you would make a double loop that sets each member to 0. In C++, null is not a valid keyword. Some people use NULL, but using 0 is probably the best way:
Code:
for (int i = 0; i < 3; ++i) {
  for (int j = 0; j < 3; ++j) {
    pbArray[i][j] = 0;
  }
}
 
My "no" answer was to your previous post.

If you allocate memory with new, then you have to delete that memory by calling delete. If you just set the pointer to 0 instead of deleting it, then you have a memory leak.
 
You could also set them all to NULL like this:
Code:
memset( pbArray, NULL, sizeof( pbArray ) );
 
cpjust,
some remarks: it's unportable way to initialize pointers via memset instead of explicit null pointer assignment (better 0 than old NULL).
A null pointer may have not-null bits representation...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top