1

I have a dynamically populated array of strings in C++:

string** A;

it is populated like this:

A = new string*[size1];

and then:

for (unsigned int i = 0; i < size1; i++)
{
    A[i] = new string[size2];
    for (unsigned int j = 0; j < size2; j++)
    {
        A[i][j] = whatever[j];
    }
}

elsewhere, I want to find out the dimensions (size1 and size2). I tries using this:

sizeof(A[i]) / sizeof(A[i][0])

but it doesn't work. Any ideas ?

Thanks

nsteiner
  • 121
  • 1
  • 10
  • Is there a reason you're not using std::vector? – The Forest And The Trees Aug 11 '15 at 09:51
  • 1
    You have them already: `size1` and `size2`. You don't need to "get" them. And you can't get them from `A`, which is just a pointer. – juanchopanza Aug 11 '15 at 09:51
  • 1
    @juanchopanza I see where you're coming from, but this is quite a far-fetched duplicate. Won't help us convince people that C and C++ are not the same language. Edit : [C++ duplicate here](http://stackoverflow.com/questions/19361663/how-to-get-size-c-dynamic-array). – Quentin Aug 11 '15 at 10:02
  • 1
    @Quentin on the other hand that is a pretty horrible question – M.M Aug 11 '15 at 10:06
  • @MattMcNabb found it through [this one](http://stackoverflow.com/questions/22008755/how-to-get-size-of-dynamic-array-in-c). I'm not sure which one should be chosen. – Quentin Aug 11 '15 at 10:08
  • @Quentin personally I'd link to that last one of yours, and unlink that one from the horrible one :) (perhaps point the horrible one back at that one) – M.M Aug 11 '15 at 10:09
  • @Quentin Sorry, I actually intended to post a c++ duplicate. Thanks. I'll re-open, but someone else will have to close with the better duplicate. – juanchopanza Aug 11 '15 at 10:40

5 Answers5

3

When you allocate memory via new T[N], the value N is not stored anywhere . If you need to know it later, you will need to keep track of it in your code.

There are pre-existing classes for allocating memory that also remember the length that was allocated. In your code:

vector<vector<string>> A(size1, vector<string>(size2));
// (code to populate...)

then you can access A.size() to get size1, and A[0].size() to get size2.

If the dimensions are known at compile-time you may use array instead of vector.

M.M
  • 138,810
  • 21
  • 208
  • 365
1

It is very simple to find the size of a two dimensional (more exactly of one-dimensional dynamically allocated arrays) array. Just declare it like

std::vector<std::vector<std::string>> A;

and use

std::cout << A.size() << std::endl;

As for your approach then you have to store the sizes in some variables when the array is allocated.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

If you are learning C++, I would recommend that you learn Classes. With a class you can encapsulate int variables along with your 2D array that you can use to store the dimensions of your array. For example:

class 2Darray{
 string **array;
 int rows;
 int cols;
}

You can then get the dimensions of your 2Darray object anytime by reading these member variables.

vectors will do this for you behind the scenes but its good for you to learn how to do this.

dmbhatti
  • 314
  • 2
  • 11
0

You can't create an array just using pointer operator. Every array is basically a pointer with allocated memory. That's why compiler wants constant before creating array.

Basically; sizeof(A[i]) won't give you the size of array. Because sizeof() function will return the a pointers size which is points to A[i] location. sizeof(A[i]) / sizeof(A[i][1]) will probably give you 1 because you are basically doing sizeof(int)/sizeof(int*)

So you need to store the boundary yourself or use vectors. I would prefer vectors.

-2

Can't get array dimensions through pointer(s)

Amit
  • 1,836
  • 15
  • 24