By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Two dimensional array problem

P: n/a
Hi,

I've got problems with 2D array.

I have a program that gets results from sql server 2000 and
puts the results in an std::string.
Here's a few example lines of code to help explain my problem.

<--- myprog.h --->
#define MAXROWS 200
#define MAXCOLUMNS 100

std::string pszResultValues[MAXROWS][MAXCOLUMNS];
<--- myprog.h --->

<--- myprog.cpp --->
long row=0,column=0;
pszResultValues[row][column]=_bstr_t(pszReceivedData);
<--- myprog.cpp --->

I loop through the results and add them to pszResultValues.
It works fine MAXROWS isn't bigger than 200 and MAXCOLUMNS
isn't bigger than 100. If I put MAXCOLUMNS value past 100
it fails. Is that some sort of array restriction or what ?
Is there a way to somehow define dynamic values to
MAXROWS and MAXCOLUMNS or is there some other solution.

I would post the actual code that I use, but it uses
companys own API etc stuff so I can't post them here.
I hope you understand my dilemma from the above.

Thanks in advance for all the help.

----
mkarja
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"mkarja" <mm**********@hotmail.com> wrote in message
news:75*************************@posting.google.co m...
Hi,

I've got problems with 2D array.

I have a program that gets results from sql server 2000 and
puts the results in an std::string.
Here's a few example lines of code to help explain my problem.

<--- myprog.h --->
#define MAXROWS 200
#define MAXCOLUMNS 100

std::string pszResultValues[MAXROWS][MAXCOLUMNS];
<--- myprog.h --->

<--- myprog.cpp --->
long row=0,column=0;
pszResultValues[row][column]=_bstr_t(pszReceivedData);
<--- myprog.cpp --->

I loop through the results and add them to pszResultValues.
It works fine MAXROWS isn't bigger than 200 and MAXCOLUMNS
isn't bigger than 100. If I put MAXCOLUMNS value past 100
it fails. Is that some sort of array restriction or what ?
Is there a way to somehow define dynamic values to
MAXROWS and MAXCOLUMNS or is there some other solution.

I would post the actual code that I use, but it uses
companys own API etc stuff so I can't post them here.
I hope you understand my dilemma from the above.

Thanks in advance for all the help.

----
mkarja


int maxrows = 4;

int maxcols = 1;

using std::vector;

using std::string;

vector< vector<string> > v(maxrows, vector<string>(maxcols));

v[yourCol][yourRow] = yourString;

You can now increase either the row or col dynamically as:

// increase by one row, X cols:

v.push_back(vector<string>(X));

// increase row X by 1 col:

v[X].push_back(string("hello"));

HTH,

Elias
Jul 22 '05 #2

P: n/a
In article <75*************************@posting.google.com> ,
mkarja <mm**********@hotmail.com> wrote:

<--- myprog.h --->
#define MAXROWS 200
#define MAXCOLUMNS 100

std::string pszResultValues[MAXROWS][MAXCOLUMNS];
<--- myprog.h --->

<--- myprog.cpp --->
long row=0,column=0;
pszResultValues[row][column]=_bstr_t(pszReceivedData);
<--- myprog.cpp --->

I loop through the results and add them to pszResultValues.
It works fine MAXROWS isn't bigger than 200 and MAXCOLUMNS
isn't bigger than 100. If I put MAXCOLUMNS value past 100
it fails. Is that some sort of array restriction or what ?
There's no fundamental restriction (in the C++ language), but your
compiler or operating system may have a limit on how memory you can
allocate for an "automatic" variable (often called "on the stack").
Is there a way to somehow define dynamic values to
MAXROWS and MAXCOLUMNS or is there some other solution.


Try using a vector of vectors instead of a 2d array. Vectors allocate
their data memory dynamically, i.e. not "on the stack".

std::vector<std::vector<std::string> > pszResultValues (MAXROWS,
std::vector<std::string>(MAXCOLUMNS));

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #3

P: n/a
Maybe you cross the memory limit?

Try using pointers... Not sure if they work for string, but I'm sure
they do for integers, etc.

example:

int **some_array = new int*[how_many_rows];
//how_many_rows can be a variable

for (int i = 0; i < how_many_rows; i++) {
some_array[i] = new int[how_many_columns];
//how_many_columns can be a variable as well
for (int j = 0; j < how_many_columns; j++)
//INITIALISE HERE
}

---

cmad
Jul 22 '05 #4

P: n/a
Sorry, didn't tell remind you that you have to deallocate the space...

and the end of your program do:

for (int k = 0; k < how_many_rows; k++)
delete [] some_array[k];

delete [] some_array;

---

cmad
Jul 22 '05 #5

P: n/a
Thanks, for the help.
But now there's another problem. I can't get the result of the
sql search in the vector.
It's a char pointer like this char* pszReceivedData=NULL;
It gives me error message that is about mile long.
error C2109: subscript requires array or pointer type
error C2109: subscript requires array or pointer type
error C2440: '=' : cannot convert from 'char *' to
'class std::vector<class std::vector<class std::basic_string<char,
struct std::char_traits<char>,class std::allocator<char> >,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > > >,class
std::allocator<class std::vector<class std::basic_string<char,
struct std::char_traits<char>,class std::allocator<char> >,
class std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > > > > >
(__thiscall CexecuteSearch::*)(void)'

What / how do I have to convert the result to make it work ?

----
mkarja
jt*******@presby.edu (Jon Bell) wrote in message news:<br**********@jtbell.presby.edu>...
In article <75*************************@posting.google.com> ,
mkarja <mm**********@hotmail.com> wrote:

<--- myprog.h --->
#define MAXROWS 200
#define MAXCOLUMNS 100

std::string pszResultValues[MAXROWS][MAXCOLUMNS];
<--- myprog.h --->

<--- myprog.cpp --->
long row=0,column=0;
pszResultValues[row][column]=_bstr_t(pszReceivedData);
<--- myprog.cpp --->

I loop through the results and add them to pszResultValues.
It works fine MAXROWS isn't bigger than 200 and MAXCOLUMNS
isn't bigger than 100. If I put MAXCOLUMNS value past 100
it fails. Is that some sort of array restriction or what ?


There's no fundamental restriction (in the C++ language), but your
compiler or operating system may have a limit on how memory you can
allocate for an "automatic" variable (often called "on the stack").
Is there a way to somehow define dynamic values to
MAXROWS and MAXCOLUMNS or is there some other solution.


Try using a vector of vectors instead of a 2d array. Vectors allocate
their data memory dynamically, i.e. not "on the stack".

std::vector<std::vector<std::string> > pszResultValues (MAXROWS,
std::vector<std::string>(MAXCOLUMNS));

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.