what about just int A[a][b] ?
mikrosfoititis
Junior Poster in Training
74 posts since Nov 2011
Reputation Points: 18
Solved Threads: 11
Skill Endorsements: 1
what about just int A[a][b] ?
Unfortunately, that's not technically allowed by many compilers because a and b are not constants. Since they are not constants, they are not known and the array can't be allocated at compile time. I hear it's technically legal under newer versions of the standards, but not every compiler supports the newer standards yet.
@OP:
I think what you have will work on a compiler that follows the new standard, but I'm not sure. I do know though that your array needs to be declared as a pointer to pointer ("double pointer"), not a ("single") pointer.
To have a truly reliable (portable) implementation though, it's better to explicitly use dynamic allocation. To do so, you declare a "double pointer" to a dataType. Then you allocate to that "double pointer" a 1-dimensional array of "single" pointers to the same dataType. Then you allocate to each "single" pointer within the array a 1-dimensional array of that dataType (actual objects/instances, not pointers). Something like this:
int **myDynamicArray = new int *[a];
for (int i = 0; i < a; ++i) {
myDynamicArray[i] = new int[b];
}
Fbody
Posting Maven
2,929 posts since Oct 2009
Reputation Points: 833
Solved Threads: 394
Skill Endorsements: 5
I hear it's technically legal under newer standards, but not every compiler supports the newer standards yet.
Not in C++. The current C standard (C99) supports variable length arrays, but they're an abomination and shouldn't be used. C++ doesn't support variable length arrays in any form because the standard library handles all but the most niche of VLA use cases.
To have a truly reliable (portable) implementation, it's better to explicitly use dynamic allocation.
Or implicitly use dynamic allocation with std::vector. You're doing the same thing with fewer features and a greater chance of implementation bugs.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 55
Not in C++. The current C standard (C99) supports variable length arrays, but they're an abomination and shouldn't be used. C++ doesn't support variable length arrays in any form because the standard library handles all but the most niche of VLA use cases.
Or implicitly use dynamic allocation with std::vector. You're doing the same thing with fewer features and a greater chance of implementation bugs.
I kind-of get the impression this is a dynamic-allocation-related assignment, but I bow to your wisdom: :P :)
Fbody
Posting Maven
2,929 posts since Oct 2009
Reputation Points: 833
Solved Threads: 394
Skill Endorsements: 5
I kind-of get the impression this is a dynamic-allocation-related assignment
It's not obviously homework, but even if it were, we would be doing the student a disservice by not mentioning the better alternative for future projects.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 55
So can you please explain how to use this std::vector?
Well off-course not, this is not a homework. Well for some reason, i have been asked to use constant size for arrays, but i believe there is always a way around. So i am on to it. Just learned how to make it work for 1D array. Not for 2D, second subscript is the problem.
Well i am using VC++ 2010 Express Edition...
That still follows an older standard. The code that I posted earlier was written on VC++ 2008.
Here is some info on the Standard Template Library (the "STL"). The std::vector, as well as several other container templates, is part of the STL.
Fbody
Posting Maven
2,929 posts since Oct 2009
Reputation Points: 833
Solved Threads: 394
Skill Endorsements: 5
So can you please explain how to use this std::vector?
Certainly. Here's a rough facsimile of your code using std::vector:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int a, b;
cout << "Enter the size for the 2D array: ";
if (cin >> a >> b) {
vector<vector<int>> vec(a, vector<int>(b));
int k = 0;
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++)
vec[i][j] = k++;
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++)
cout << vec[i][j] << ' ';
cout << '\n';
}
}
}
The only real difference is in the constructor where you need to build up both dimensions. This is the proper way to do it if you already know the sizes, but you can grow the vector dynamically as you go if necessary, that's where the real power lies:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<vector<int>> vec;
string line;
cout << "Enter a table (EOF to finish):\n";
while (getline(cin, line)) {
// Insert a new row to the vector
vec.push_back(vector<int>());
istringstream iss(line);
int x;
while (iss >> x)
vec.back().push_back(x);
}
for (int i = 0; i < vec.size(); i++) {
for (int j = 0; j < vec[i].size(); j++)
cout << vec[i][j] << ' ';
cout << '\n';
}
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 55
Is this compilable? (i.e without the white space)
Yes, that particular annoyance was removed in C++11. The OP's compiler (Visual C++ 2010) supports the new feature.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 55
Question Answered as of 1 Year Ago by
Narue,
Fbody,
frogboy77
and 1 other