How to allocate memory for 2-D integer array in one line?
-
I have this code for allocating memory for 2-d int array.
int **p = (int **)malloc(MAXROW*sizeof(int *));
for(int i = 0; i < MAXROW; i++)
{p\[i\] = (int \*)malloc(MAXCOL\*sizeof(int));
}
How can i optimize this code in one line?
-
I have this code for allocating memory for 2-d int array.
int **p = (int **)malloc(MAXROW*sizeof(int *));
for(int i = 0; i < MAXROW; i++)
{p\[i\] = (int \*)malloc(MAXCOL\*sizeof(int));
}
How can i optimize this code in one line?
:confused:
Nilesh Hamane wrote:
How can i optimize this code in one line?
It is unclear to me what your are asking here. Do you mean by using just one dynamic memory allocation? What aspect of the situation are you trying to optimize - code size or speed; initial setup or use? I am presuming that you are using C and not C++. There are a variety of options that you can use. If this is really critical for your application, you will probably have to run some tests to see what is best in your situation. A second possible approach is to do one allocation of col * row ints and explicitly calculate the offset into this array for each access. A third approach is to keep your first allocation of row pointers to int, supplement it by a second allocation of col * row ints and set each row pointer to a subsequent group of col ints in the second block.
Please do not read this signature.
-
:confused:
Nilesh Hamane wrote:
How can i optimize this code in one line?
It is unclear to me what your are asking here. Do you mean by using just one dynamic memory allocation? What aspect of the situation are you trying to optimize - code size or speed; initial setup or use? I am presuming that you are using C and not C++. There are a variety of options that you can use. If this is really critical for your application, you will probably have to run some tests to see what is best in your situation. A second possible approach is to do one allocation of col * row ints and explicitly calculate the offset into this array for each access. A third approach is to keep your first allocation of row pointers to int, supplement it by a second allocation of col * row ints and set each row pointer to a subsequent group of col ints in the second block.
Please do not read this signature.
as Avi told.. here is how it goes.. //FIRST approacc
int* p[MAXROW];
//allocate col*row ints to base ptr
p[0] = (int*)malloc(MAXROW*MAXCOL*sizeof(int));//explicitly calculate the offset into this array
for(int i=1; i<MAXROW; ++i)
p[i] = p[i-1] + MAXCOL;//SECOND approach
//first allocation of row pointers to int
int **p = (int **)malloc(MAXROW*sizeof(int *));//second allocation of col*row ints
p[0] = (int *)malloc(MAXCOL*MAXROW*sizeof(int));//set each row pointer to a subsequent group of col ints in the second //block.
for(int i = 1; i<MAXROW; i++)
p[i] = p[i-1] + MAXCOL;both aproach seems better since all memory allocation is done at once..
-
I have this code for allocating memory for 2-d int array.
int **p = (int **)malloc(MAXROW*sizeof(int *));
for(int i = 0; i < MAXROW; i++)
{p\[i\] = (int \*)malloc(MAXCOL\*sizeof(int));
}
How can i optimize this code in one line?
what's wrong with
int * p = (int *) malloc(COLS * ROWS * sizeof(int));
?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I have this code for allocating memory for 2-d int array.
int **p = (int **)malloc(MAXROW*sizeof(int *));
for(int i = 0; i < MAXROW; i++)
{p\[i\] = (int \*)malloc(MAXCOL\*sizeof(int));
}
How can i optimize this code in one line?
Just make a function
void Make2DInt(int**& ppToCreate, int iWidth, int iHeight)
and then call it at one line... :)Check your definition of Irrationality[^] :) 1 - Avicenna 5 - Hubbard 3 - Own definition
-
I have this code for allocating memory for 2-d int array.
int **p = (int **)malloc(MAXROW*sizeof(int *));
for(int i = 0; i < MAXROW; i++)
{p\[i\] = (int \*)malloc(MAXCOL\*sizeof(int));
}
How can i optimize this code in one line?
-
:laugh:
Don't die, until you do.