sorry... misunderstood... well is because i didn't know the power of the std::vector... now i'm studing it :p
what a wonderful forum!!! light speed replies and very useful tips!! thank you again!!
Printable View
sorry but what about reallocating matrix?
for example...
a matrix of an HWND array can be declared like that?
if it's right... for adding a line i doCode:typedef std::vector<HWND> HWNDArray;
typedef std::vector<HWNDArray> HWNDMatrix;
anyway when i try to access to an element it crash...Code:HWNDMatrix hCheckBox;
hCheckBox.reserve(hCheckBox.size() + 1);
HWND foo = CreateWindowEx ( 0L, "BUTTON", "", BS_AUTOCHECKBOX | WS_VISIBLE | WS_CHILD | BS_CENTER, left, top, right, bottom, hWnd, NULL, GetModuleHandle(NULL), 0);
hCheckBox[hCheckBox.size()].push_back(foo);
The error is simple -- the vector has size 0. You can't access an element of a vector that is out of bounds.
As a quiz, you tell us what this line does. One clue -- it does not give you hCheckBox.size() elements. Given that you do not have those elements, the next line is an illegal memory access.Code:hCheckBox.reserve(hCheckBox.size() + 1);
There is no such element as hCheckBox[hCheckBox.size()]. That's why you get an error.Code:hCheckBox[hCheckBox.size()].push_back(foo);
There are 4 general ways to add elements to a vector.
1) On construction of the vector.
2) Using vector::push_back()
3) Using vector::insert()
4) using vector::resize().
Look at item 4. Note that the name of the function is resize(), not reserve().
Regards,
Paul McKenzie
Well just for fun I decided to fix your reallocating issue and give you a few pointers ( hopefully valid :) ) First of all you have no need of the static variable and no need to save the length of strings because what you are reallocating is just an array of pointers to the malloced strings, you dont need to reallocate the strings just the array of pointers so all you need to save is count of elements inside which you already use the n parameter for :P Second mistake you have was the dereferencing of the list pointer , *list[n] which in fact dereferences list[n] but you actually need to dereference list and get its n-th element so a pair of brackets can be very useful here (*list)[n]. So the final code should look like this:
Hope I helped :)Code:#include <iostream>
using namespace std;
void add(char ***list, int n, char *person)
{
*list=(char**)realloc(*list,(n+1)*sizeof(char**));
(*list)[n] = (char*) malloc (strlen(person) + 1);
strcpy((*list)[n], person);
}
int main()
{
char **list = NULL;
int n = 0;
add(&list, 0, "jack");
printf("list[0]: %s\n", list[0]);
n++;
add(&list, 1, "jeff");
printf("list[1]: %s\n", list[1]);
free(list[0]);
free(list[1]);
free(list);
return 0;
}
There we go fixed in previous post for your viewing pleasure :)
I just fixed it because he wanted to see how it would look while using realloc and his original snippet didnt bother with freeing memory, and 'huge' is quite an overstatement there.
Just for fun: realloc() can be used in place of free() by specifying a new size of 0.
cool XD well I learned a lot of things from this discussion and I think I don't want to see more than ONE star before a variable for a while XD
personally I hate working with dyn mem because I ever feel there's some leak somewhere...
the realloc for the string has been made because at the beginning I wanted to make an insert possible but (unfortunately / fortunately) std::vector.insert() took all the fun away XD
about my last post I solved declaring a support single var and "push_back"ed it into a support vector and then "push_back"ed the vector in the matrix...
std::vector make me feel much quiet about memory :P
thank you all again!
When you need a matrix of dynamic size which won't *change* size often, you should consider using boost::multi_array.