M
masood.iqbal
The code example below shows the dynamic allocation of a 2D array. I
must admit that it took quite a while for me to get there (I already
have another posting to that effect), but I am glad that I finally got
it working. Now here's the problem:
I am able to get the 2D array dynamically allocated correctly as long
as I am doing it "in-line" (i.e. without invoking any function). The
moment I try to do it in another function, I get a core dump. Any help
will be appreciated.
Since this function is expected to update a pointer-to-pointer type, I
am actually passing the pointer-to-pointer-to-pointer type to the
function. What am I missing here?
You can see that the source code works correctly when I am perform 2D
array initialization "in-line" (i.e. by not invoking a function
call) simply by un-commenting the line
/* #define INLINE_INIT */
Masood
/******************************************************/
#include <stdio.h>
#include <stdlib.h>
/*#define INLINE_INIT*/
#define MAXROWS 3
#define MAXCOLS 5
void
buildTbl(int ***tblPtr, size_t numRows, size_t numCols)
{
*tblPtr = (int **)malloc(numRows*sizeof(int*));
/* C++ : *tblPtr = new (int*)[numRows]; */
for(size_t i = 0; i < numCols; i++)
*tblPtr = (int *)malloc(numCols*sizeof(int));
/* C++: *tblPtr = new (int)[numCols]; */
}
main()
{
int startVal = 5;
int **tbl;
#ifdef INLINE_INIT
tbl = (int **)malloc(MAXROWS*sizeof(int*));
/* C++ : tbl = new (int*)[MAXROWS]; */
for(size_t i = 0; i < MAXCOLS; i++)
tbl = (int *)malloc(MAXCOLS*sizeof(int));
/* C++: tbl = new (int)[MAXCOLS]; */
#else
buildTbl(&tbl, MAXROWS, MAXCOLS);
#endif
for(size_t row = 0; row < MAXROWS; row++)
for(size_t col = 0; col < MAXCOLS; col++)
tbl[row][col] = startVal++;
for(size_t row = 0; row < MAXROWS; row++)
for(size_t col = 0; col < MAXCOLS; col++)
printf("Row: %d, Col: %d => %d\n",
row, col, tbl[row][col]);
return 0;
}
must admit that it took quite a while for me to get there (I already
have another posting to that effect), but I am glad that I finally got
it working. Now here's the problem:
I am able to get the 2D array dynamically allocated correctly as long
as I am doing it "in-line" (i.e. without invoking any function). The
moment I try to do it in another function, I get a core dump. Any help
will be appreciated.
Since this function is expected to update a pointer-to-pointer type, I
am actually passing the pointer-to-pointer-to-pointer type to the
function. What am I missing here?
You can see that the source code works correctly when I am perform 2D
array initialization "in-line" (i.e. by not invoking a function
call) simply by un-commenting the line
/* #define INLINE_INIT */
Masood
/******************************************************/
#include <stdio.h>
#include <stdlib.h>
/*#define INLINE_INIT*/
#define MAXROWS 3
#define MAXCOLS 5
void
buildTbl(int ***tblPtr, size_t numRows, size_t numCols)
{
*tblPtr = (int **)malloc(numRows*sizeof(int*));
/* C++ : *tblPtr = new (int*)[numRows]; */
for(size_t i = 0; i < numCols; i++)
*tblPtr = (int *)malloc(numCols*sizeof(int));
/* C++: *tblPtr = new (int)[numCols]; */
}
main()
{
int startVal = 5;
int **tbl;
#ifdef INLINE_INIT
tbl = (int **)malloc(MAXROWS*sizeof(int*));
/* C++ : tbl = new (int*)[MAXROWS]; */
for(size_t i = 0; i < MAXCOLS; i++)
tbl = (int *)malloc(MAXCOLS*sizeof(int));
/* C++: tbl = new (int)[MAXCOLS]; */
#else
buildTbl(&tbl, MAXROWS, MAXCOLS);
#endif
for(size_t row = 0; row < MAXROWS; row++)
for(size_t col = 0; col < MAXCOLS; col++)
tbl[row][col] = startVal++;
for(size_t row = 0; row < MAXROWS; row++)
for(size_t col = 0; col < MAXCOLS; col++)
printf("Row: %d, Col: %d => %d\n",
row, col, tbl[row][col]);
return 0;
}