1

I am using the following code to create dynamic 2D array.

uint32_t** arrays = new uint32_t*[10]; 
uint32_t number = take input from console ;
arrays[0] = new uint32_t[number];
number = take input from console ;
arrays[1] = new uint32_t[number];
delete arrays[0] ;
number = take input from console ;
arrays[0] = new uint32_t[number] ;

Can any one help me how to know the size of the 2nd dimension without knowing the input values ? Means how to find the array size on arrays[0], arrays[1] and so on ?

alessandro
  • 1,681
  • 10
  • 33
  • 54
  • 1
    What are you trying to do? – Wutz Dec 10 '12 at 14:04
  • 2
    Use a `std::vector>` and use `std::vector::size()`. – hmjd Dec 10 '12 at 14:05
  • 2
    I recommend using a [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) of `std::vectors`. This will simplify quite a lot for you. – Some programmer dude Dec 10 '12 at 14:06
  • have a look here: http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-using-new , better use the approach `int *ary = new int[sizeX*sizeY]; // ary[i][j] is then rewritten as ary[i*sizeY+j]` for your problem (requesting array size from console is... bleh) – Najzero Dec 10 '12 at 14:09
  • @Najzero How does this apply to the question? – Šimon Tóth Dec 10 '12 at 14:10
  • ---------- How about use map of map with int key to simulate the 2d-array? – Wonson Apr 09 '13 at 08:20

3 Answers3

3

There is no way to determine the size of a memory block allocated by new without storing the size value.

EDIT: also, why not just use a vector< vector< uint32_t > > arrays;?

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
1

std::vector<std::valarray<uint32_t> > may also be an alternative. Assuming, that your console program will do some sort of calculation, the rather unknown std::valarray will be a good companion.

Be aware of the risk, that user provided size values are likely to lead to a std::bad_alloc exception, so you may put at least the allocations into a try/catch block.

Another solution would be to collect all user provided size values in a container and then instantiate another, single container for data:

//Disclaimer: below code has never been tested and is rather about how to do such stuff in general,
//regardless of the few bytes gained for sacrificing CPU cycles.

#include <vector>
#include <valarray>
#include <numeric>
#include <cassert>
#include <exception>

void app(void)
{
    std::vector<size_t> sizes;
    std::valarray<uint32_t> data;

    try
    {
        //collect N user defined sub-vector size values in 'sizes'
        //.
        //.
        //.
        size_t totalNumberOfValues = std::accumulate(sizes.begin(),sizes.end(),0u);
        data.resize(totalNumberOfValues);
    }
    catch(std::bad_alloc& e)
    {
        //handle error
    }

    //accessing the minor/sub-vectors using the size values requires accumulation of adjacent size values of preceding sub-vectors.    
    //of course, these offsets could also be pre-computed.

    size_t subVectorIndex /*...*/; //needs to be checked!
    assert(subVectorIndex < sizes.size());

    size_t subVectorSize = sizes[subVectorIndex];
    size_t subVectorOffset = std::accumulate(sizes.begin(),sizes.begin()+subVectorIndex,0u);

    //do NOT reallocate 'data' while twiddling around with pointers!
    uint32_t* subVectorData = &data[subVectorOffset];

    //well, using neat stuff like std::slice, std::gslice etc. is up to you now ;-)
}
Sam
  • 7,778
  • 1
  • 23
  • 49
0

There is no way to do that. For the sake of memory efficiency the size of the allocated memory block is not guaranteed to be stored anywhere. Consider using vectors instead of plain arrays for the 2nd dimension.

Alexander Chertov
  • 2,070
  • 13
  • 16