-1

how to allocate run time memory to an array of size[4][3]?

i.e int a[4][3]

If need is to allocate memory to an array at run time than how to allocate memory to 2D array or 3D array.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Satender346
  • 352
  • 1
  • 3
  • 15
  • simply i want to allocate memory to an array size of 4 row and 3 column. – Satender346 Oct 17 '13 at 08:15
  • 1
    Read [Dynamically allocating array explain](http://stackoverflow.com/a/19240932/1673391) – Grijesh Chauhan Oct 17 '13 at 08:16
  • 3
    It's probably better to pick C *or* C++. – CB Bailey Oct 17 '13 at 08:18
  • Read [Allocate memory 2d array in function C](http://stackoverflow.com/a/15062765/1673391) And for 3-D Read [Matrix of String or/ 3D char array](http://stackoverflow.com/questions/16522035/allocate-memory-to-char-in-c/16522223#16522223) – Grijesh Chauhan Oct 17 '13 at 08:18
  • I can assure you, adding `int hugeArray[1000000];` to the source-code doesn't increase the executable's size by 1 million * sizeof(int). Ergo - int a[4][3] is a run-time allocation. You could use int *myVar[4][5] = new int[4][3] to allocate from the heap, rather than the stack - perhaps this is what you mean? – enhzflep Oct 17 '13 at 08:19
  • possible duplicate of [How do I correctly set up, access, and free a multidimensional array in C?](http://stackoverflow.com/questions/12462615/how-do-i-correctly-set-up-access-and-free-a-multidimensional-array-in-c) – Lundin Oct 17 '13 at 08:20
  • 1
    You have two possible duplicates. One in C (but that won't work with the Visual Studio compiler) and one that is in C but can be adapted to C++. As you can see both are very different, and mixing in the variant with C++ it's even more difference. You need to decide on *one* language. – Some programmer dude Oct 17 '13 at 08:27
  • Besides, in C++ you have yet another possible solution, which is very different from both the duplicates: Vector of vectors. – Some programmer dude Oct 17 '13 at 08:30

8 Answers8

2

Editing the answer based on comments. Allocate separately for each dimension. For a 2D array a 2 level allocation is required.

*a = (int**)malloc(numberOfRows*sizeof(int*));
for(int i=0; i<numberOfRows; i++)
    {
    (*arr)[i] = (int*)malloc(numberOfColumns*sizeof(int));
    }
AdityaTS
  • 89
  • 11
1

The simplest way to allocate dynamically an array of type int[4][3] is the following

int ( *a )[3] = new int[4][3];

// some stuff using the array

delete []a;

Another way is to allocate several arrays. For example

int **a = new int * [4];

for ( size_t i = 0; i < 4; i++ ) a[i] = new int[3];

// some stuff using the array

for ( size_t i = 0; i < 4; i++ ) delete []a[i];

delete []a;

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

What have you tried. new int[4][3] is a perfectly valid expression, and the results can be assigned to a variable with the appropriate type:

int (*array2D)[3] = new int[4][3];

Having said that: I can't really think of a case where this would be appropriate. Practically speaking, anytime you need a 2 dimensional array, you should define a class which implements it (using std::vector<int> for the actual memory).

James Kanze
  • 150,581
  • 18
  • 184
  • 329
1

A pure C approach is the following:

int (*size)[4][3];

size = malloc(sizeof *size);

/* Verify size is not NULL */

/* Example of access */
(*size)[1][2] = 89;

/* Do something useful */

/* Deallocate */
free(size);

The benefit is that you consume less memory by not allocating intermediate pointers, you deal with a single block of memory and deallocation is simpler. This is especially important if you start to have more than 2 dimensions.

The drawback is that the access syntax is more complicated, as you need to dereference a pointer before being able to index.

  • Another significant benefit of this method ( `size = malloc(sizeof *size);` ) is that access can also be via pure array notation: `size[0][1][2]`. (which you do not show, but when used, is much easier to read.). Should you ever edit your answer, showing this access method under the existing one would be a good addition. – ryyker Dec 19 '19 at 14:20
0

Use calloc, i guess this will do.

int **p;
p=(int**)calloc(4,sizeof(int));
Raon
  • 1,266
  • 3
  • 12
  • 25
0

In C you can use pointer to pointer

AS @Lundin mentioned this is not 2D array. It is a lookup table using pointers to fragmented memory areas allocated all over the heap.

You need to allocate how many pointers you need and then allocate each pointer. you can allocate fixed size or varaible size depending on your requirement

  //step-1: pointer to row
  int **a = malloc(sizeof(int *) * MAX_NUMBER_OF_POINTERS);
  //step-2: for each rows
  for(i = 0; i < MAX_NUMBER_OF_POINTERS; i++){
    //if you want to allocate variable sizes read them here
    a[i] = malloc(sizeof(int) * MAX_SIZE_FOR_EACH_POINTER);  // where as if you use character pointer always allocate one byte extra for null character
  } 

Where as if you want to allocate char pointers avoid using sizeof(char) inside for loop. because sizeof(char) == 1 and do not cast malloc result.

see How to declare a 2d array in C++ using new

Community
  • 1
  • 1
Gangadhar
  • 10,248
  • 3
  • 31
  • 50
0

You could use std::vector<> since it is a templated container (meaning array elements can be whatever type you need). std::vector<> allows for dynamic memory usage (you can change the size of the vector<> whenever you need to..the memory is allocated and free'd automatically).

For example:

#include <iostream>
#include <vector>

using namespace std; // saves you from having to write std:: in front of everthing

int main()
{
    vector<int> vA;
    vA.resize(4*3); // allocate memory for 12 elements

    // Or, if you prefer working with arrays of arrays (vectors of vectors)
    vector<vector<int> > vB;
    vB.resize(4);
    for (int i = 0; i < vB.size(); ++i)
        vB[i].resize(3);

    // Now you can access the elements the same as you would for an array
    cout << "The last element is " << vB[3][2] << endl;
}
axon
  • 1,190
  • 6
  • 16
-2

You can use malloc() in c or new in c++ for dynamic memory allocation.

plasmixs
  • 156
  • 5