Two dimensional array problem

M

mkarja

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.
 
L

lallous

mkarja said:
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.

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
 
J

Jon Bell

<--- 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));
 
C

Chris Mantoulidis

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 = 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
}
 
C

Chris Mantoulidis

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;
 
M

mkarja

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


<--- 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));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,405
Latest member
DavidCex

Latest Threads

Top