-4

I want to declare a 2D Array without an initial size. It keeps on giving me an error:

Error C2078: too many initializes.

I have tried to dynamically allocate my array but nothing worked out so far as I am not too familiar with dynamic allocation. My question is If there is a possible way to declare an Array without an initial size and if so what is the most efficient way to do it ?

scohe001
  • 15,110
  • 2
  • 31
  • 51
  • 2
    _"what is the most efficient way to do it ? "_ Use a [`std::vector`](http://en.cppreference.com/w/cpp/container/vector). – user0042 Aug 21 '17 at 20:32
  • 2
    Please include the code you have tried so far . – PrimeOfKnights Aug 21 '17 at 20:32
  • Try looking into `Vectors`. You can make one custom one up yourself if needed. – Jimenemex Aug 21 '17 at 20:32
  • @JustinJmnz And a customized `std::vector` is what exactly again? – user0042 Aug 21 '17 at 20:37
  • @user0042 Let me rephrase, you can `extend` upon `std::vector` if you wanted to add other functionality.. but you probably shouldn't inherit from it because it has no virtual destructor. But you certainly can make up your own implementation of the `vector` class. – Jimenemex Aug 21 '17 at 20:44
  • General rule of thumb is to prefer [Composition over inheritance](https://en.wikipedia.org/wiki/Composition_over_inheritance). It's pretty rare that you need (or want to) extend `std::vector`. – user4581301 Aug 21 '17 at 21:05
  • 1
    What sort of efficient? `Rule of thumb: Go with easiest to program (`vector` of `vectors`) until you've taken measurements and found the simple approach isn't fast enough. – user4581301 Aug 21 '17 at 21:15

2 Answers2

1

my question is If there is a possible way to declare an Array without an initial size and if so what is the most efficient way to do it ?

Sure, you could provide a vector of vectors to represent a 2D array (let's say of integer values):

 std::vector<std::vector<int>> my2DArray;

Well, regarding efficiency maybe performance and memory fragmentation wise it's better to wrap a 1D vector kept internally with an interface that allows 2D coordinate access.

That would require you to know and specify the dimension limits though.

So if you really want to keep a 2D structure without initial size the above mentioned vector of vectors is the way to go.

user0042
  • 7,917
  • 3
  • 24
  • 39
1

I wrote a simple program using pointers, new and delete functions. You can add more functionality to it.

#include <iostream>
using namespace std;
int main()
{
    int size;
    cout << "Input size of 2D array : ";
    cin >> size;

    int *ptr; // Declare Pointer

    ptr = new int[size*size]; // Allocate memory of all elements in 2D array

    for (int i = 0; i < size*size; i++) {
        *(ptr + i) = 0; // Initialize every element to 0
    }

    cout << "Printing the 2D Array" << endl << endl;

    int iterSize = 0;

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            cout << *(ptr + iterSize) << " ";
        }
        cout << endl;
    }

    delete [] ptr; // ptr memory is released

    return 0;
}

Here is the output initializing all elements to 0:

Initialized all elements to 0

Manjunath
  • 491
  • 2
  • 6
  • 17