1

In c++, I can create a 2D array with fixed number of columns, say 5, as follows:

char (*c)[5];

then I can allocate memory for rows as follows

c = new char[n][5];

where n can be any variable which can be assigned value even at run time. I would like to know whether and how can I dynamically allocate variable amount of memory to each row with this method. i.e. I want to use first statement as such but can modify the second statement.

2
  • 4
    Welcome to C++! Please throw away your C-style knowledge and indulge in std::vector and std::array! If you prefer, boost::multi_array is also an option! Commented Jan 9, 2012 at 22:24
  • You said C++, but you usually do stuff like this in C and not C++. Here is a nice link explaining how to make dynamic arrays using malloc. Commented Jan 9, 2012 at 22:47

3 Answers 3

4

Instead of a pointer to an array, you'd make a pointer to a pointer, to be filled with an array of pointers, each element of which is in turn to be filled with an array of chars:

char ** c = new char*[n];  // array of pointers, c points to first element

for (unsigned int i = 0; i != n; ++i)
    c[i] = new char[get_size_of_array(i)]; // array of chars, c[i] points to 1st element

A somewhat more C++ data structure would be a std::vector<std::string>.

As you noticed in the comment, dynamic arrays allocated with new[] cannot be resized, since there is no analogue of realloc in C++ (it doesn't make sense with the object model, if you think about it). Therefore, you should always prefer a proper container over any manual attempt at dynamic lifetime management.

In summary: Don't use new. Ever. Use appropriate dynamic containers.

Sign up to request clarification or add additional context in comments.

7 Comments

I want to know whether I can fix the number of columns as in char (*c)[5] and then dynamically allocate the number of rows and not the other way round.
@sidharthsharma: Yes, use std::vector<std::array<char, 5>>. Dynamic arrays allocated with new[] cannot be resized; there is no realloc-analogue in C++.
Kerrek, this is one of the things that puzzle me about stackexchange: how do you explain the fact that I answered slightly more than a minute earlier than you, and yet you get all the upvotes while I get nothing?
@MikeNakis: You have at least 50% of the upvotes I have, that's not nothing!
LOL! you just upvoted me! C-:= Here, take one, too. Now I only have 33%. Explain that now!
|
1

You need to declare c as follows: char** c; then, allocate the major array as follows: c = new char*[n]; and then, allocate each minor array as follows: c[i] = new char[m]

2 Comments

but that allocates a fixed number of rows n and not fixed number of columns. I just want to know whether something of that sort can be done or not.
It all depends on what you think of as a row and what you think of as a column. A two-dimensional array has two dimensions. One of them is the major, and the other is the minor. I have shown you how you can have a varying major, and a varying minor. It cannot get any more flexible than that. If you still have a problem with it, then you need to switch your understanding of what are columns and what are rows.
0
#include <iostream>
using namespace std;

main()
{
    int row,col,i,j;
    cout<<"Enter row and col\n";
    cin>>row>>col;

    int *a,(*p)[col]=new (int[row][col]);
    for(i=0;i<row;i++)
            for(j=0;j<col;j++)
                p[i][j]=i+j;

        for(i=0;i<row;i++)
            for(j=0;j<col;j++)
                cout<<i<<" "<<j<<" "<<p[i][j]<<endl;
                //printf("%d %d %d\n",i,j,p[i][j]);




}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.