-
allocating bidimensional matrix
hi all!! I need to create a function that by reference reallocate an asymmetrical bidimensional
character matrix...
I've done something like that but it crash...
================================CODE================================
#include <iostream>
using namespace std;
void add(char ***list, int n, char *person)
{
static int GlobStrLen = 0;
*list = (char**) realloc(*list, GlobStrLen * sizeof(char**));
*list[n] = (char*) malloc (strlen(person) + 1);
strcpy(*list[n], person);
GlobStrLen += strlen(person) + 1;
}
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]);
return 0;
}
================================END CODE============================
thank you for yours replies!!!
-
Re: allocating bidimensional matrix
1. Please, use Code tags. See http://www.codeguru.com/forum/misc.php?do=bbcode#code
2.
Quote:
Originally Posted by EvIl_DeViL;1865712[CODE
]#include <iostream>
using namespace std;
void add(char ***list, int n, char *person)
{
static int GlobStrLen = 0;
*list = (char**) realloc(*list, GlobStrLen * sizeof(char**));
*list[n] = (char*) malloc (strlen(person) + 1);
strcpy(*list[n], person);
GlobStrLen += strlen(person) + 1;
}[/CODE]
So, reallocate some buffer with the size zero? :confused:
3. Is there some reason to use malloc/realloc rather than new/delete? :confused:
or it is just a plain C (not a C++) code?
-
Re: allocating bidimensional matrix
'list' is NULL, so how do you want to REalloc that ?
-
Re: allocating bidimensional matrix
Quote:
Originally Posted by
VictorN
Or it is just a plain C (not a C++) code?
It's definitely supposed to be C++, as <iostream> is included. The problem is that the code is written as if it was 'C'.
Regards,
Paul McKenzie
-
Re: allocating bidimensional matrix
Quote:
Originally Posted by
Skizmo
'list' is NULL, so how do you want to REalloc that ?
realloc behaves like malloc if its first argument is NULL.
I would advise using a vector<string> for this problem.
-
Re: allocating bidimensional matrix
thank you for your many replies!
sorry for the CODE tag... I didn't find it :P
quoting lindley for the behaving of the realloc.
I used realloc instead new/delete beacause with the C++ operator you can't "realloc" memory unless you create another memory area of the same size of the one you want to realloc to be able to store your data and then re-copy in the "resized" original "area".
basically I can't see to much difference from the C operator and the C++ operator to manage the memory (talking about char type) so I thought I'd better use realloc instead creating a self-made one with new/delete.
I think it doesn't matter if I use C or C++. the only thing to pay attention for is use only malloc/realloc/free or new/delete.
for the goal i'm trying to reach i think I can do the same thing with the C dynmem stuff or the C++ dynmem stuff...
about this line:
Code:
*list = (char**) realloc(*list, GlobStrLen * sizeof(char**));
i made it for keeping the memory contiguous.
anyway all this code work only for the first element...
p.s. I'd prefer a C style solution for the problem... i feel it much lower and manageable instead a C++ style one.
THANK YOU!!
-
Re: allocating bidimensional matrix
Quote:
realloc behaves like malloc if its first argument is NULL.
Ah... never used it, hence the confusion :)
-
Re: allocating bidimensional matrix
Quote:
Originally Posted by
EvIl_DeViL
hi all!! I need to create a function that by reference reallocate an asymmetrical bidimensional
character matrix...
1) What do you mean "by reference"?
2) Do not name your variable "list", as there already is a std::list class in C++.
3) Using vector<string>
Code:
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
typedef std::vector<std::string> StringArray;
int main()
{
StringArray theList;
theList.push_back("jack");
printf("list[0]: %s\n", theList[0].c_str());
theList.push_back("jeff");
printf("list[1]: %s\n", theList[1].c_str());
return 0;
}
This code does the same as yours, and there are no pointers involved.
Regards,
Paul McKenzie
-
Re: allocating bidimensional matrix
Quote:
Originally Posted by
EvIl_DeViL
p.s. I'd prefer a C style solution for the problem... i feel it much lower and manageable instead a C++ style one.
If you take a look at my post, and your first post, which one seems more manageable to you (or to most other C++ programmers)?
Regards,
Paul McKenzie
-
Re: allocating bidimensional matrix
Quote:
Originally Posted by
EvIl_DeViL
the only thing to pay attention for is use only malloc/realloc/free or new/delete.
Why? What are the reasons for this requirement?
Regards,
Paul McKenzie
-
Re: allocating bidimensional matrix
interesting solution... I stopped studing C++ after class... so i miss all the strange template stuff, the std library, and so on... good excuse for getting an upgraded!!! many thanks!!!
(it's still be interesting viewing the realloc version :P)
Quote:
Originally Posted by
Paul McKenzie
Why? What are the reasons for this requirement?
Regards,
Paul McKenzie
don't use delete for unlocating an area allocated by malloc/calloc/realloc and don't "free()" an area allocated by new because they manage memory in different ways!
-
Re: allocating bidimensional matrix
Quote:
(it's still be interesting viewing the realloc version :P)
Same code sample.
Vector will automatically expand as required.
Great stuff!
-
Re: allocating bidimensional matrix
Quote:
Originally Posted by
JVene
Same code sample.
Vector will automatically expand as required.
Great stuff!
can you post some example code please? I can't figure...
-
Re: allocating bidimensional matrix
Quote:
Originally Posted by
EvIl_DeViL
I used realloc instead new/delete beacause with the C++ operator you can't "realloc" memory unless you create another memory area of the same size of the one you want to realloc to be able to store your data and then re-copy in the "resized" original "area".
A good reason, right up until you realize that std::vector::resize() is C++'s answer to realloc----plus it makes things even easier by completely hiding the fact that you're working with dynamic memory at all.
Unless you're working with strings specifically, in which case std::string has some behaviors which are more intuitive than std::vector<char>.
-
Re: allocating bidimensional matrix
Quote:
Originally Posted by
EvIl_DeViL
interesting solution... I stopped studing C++ after class... so i miss all the strange template stuff, the std library, and so on... good excuse for getting an upgraded!!! many thanks!!!
(it's still be interesting viewing the realloc version :P)
There are several threads that have full source to routines that create dynamically allocated 2D arrays.
Quote:
don't use delete for unlocating an area allocated by malloc/calloc/realloc and don't "free()" an area allocated by new because they manage memory in different ways!
What I meant by "what were your reasons for this requirement" is to see why you needed to create a multidimensional array using 'C' methods instead of just using vector<string>.
Regards,
Paul McKenzie
-
Re: allocating bidimensional matrix
Quote:
Originally Posted by
Paul McKenzie
What I meant by "what were your reasons for this requirement" is to see why you needed to create a multidimensional array using 'C' methods instead of just using vector<string>.
Regards,
Paul McKenzie
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!!
-
Re: allocating bidimensional matrix
sorry but what about reallocating matrix?
for example...
a matrix of an HWND array can be declared like that?
Code:
typedef std::vector<HWND> HWNDArray;
typedef std::vector<HWNDArray> HWNDMatrix;
if it's right... for adding a line i do
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);
anyway when i try to access to an element it crash...
-
Re: allocating bidimensional matrix
The error is simple -- the vector has size 0. You can't access an element of a vector that is out of bounds.
Code:
hCheckBox.reserve(hCheckBox.size() + 1);
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[hCheckBox.size()].push_back(foo);
There is no such element as hCheckBox[hCheckBox.size()]. That's why you get an error.
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
-
Re: allocating bidimensional matrix
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:
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;
}
Hope I helped :)
-
Re: allocating bidimensional matrix
Quote:
Originally Posted by
kolkoo
Well just for fun I decided to fix your reallocating issue
This code has a huge amount of memory leaks. Where do you deallocate the memory?
The solutions using C++ containers both work and have no memory leaks.
Regards,
Paul McKenzie
-
Re: allocating bidimensional matrix
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.
-
Re: allocating bidimensional matrix
Just for fun: realloc() can be used in place of free() by specifying a new size of 0.
-
Re: allocating bidimensional matrix
Quote:
Originally Posted by
Lindley
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!
-
Re: allocating bidimensional matrix
When you need a matrix of dynamic size which won't *change* size often, you should consider using boost::multi_array.