指向指针指针的指针问题 [英] Pointer-to-pointer-to-pointer question
问题描述
下面的代码示例显示了2D数组的动态分配。我b $ b必须承认我花了很长时间才能到达那里(我已经
还有另一个帖子),但我很高兴我终于得到了
它有效。现在问题就在这里:
我可以正确地动态分配2D数组,因为我正在做在...中线" (即没有调用任何功能)。
时刻我尝试在另一个函数中执行它,我获得了一个核心转储。任何帮助
将不胜感激。
由于此函数需要更新指针到指针类型,我
我实际上是将指针指向指针类型传递给
函数。我在这里缺少什么?
当我执行2D时,你可以看到源代码正常工作
数组初始化在线 (即通过不调用函数
调用)只需取消注释该行
/ * #define INLINE_INIT * /
>
Masood
/ ******************************* ******************* **** /
#include< stdio.h>
#include < stdlib.h>
/ * #define INLINE_INIT * /
#define MAXROWS 3
#定义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 [i] =(int *)malloc(numCols * sizeof(int));
/ * C ++:* tblPtr [i] = 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 [i] =(int *)malloc(MAXCOLS * sizeof (int));
/ * C ++:tbl [i] = 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 [行] [col]);
返回0;
}
< blockquote> ma**********@lycos.com 写道:
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 [i] =(int *)malloc(numCols * sizeof(int));
(* tblPtr)[i] = ...
注意x = malloc(n * sizeof * x);
优先于x =(type *)malloc(n * sizeof(type);
你应该检查malloc是否没有返回NULL。
你可以将buildTbl的返回类型更改为int **和
返回数组而不是通过参数。
我还记得吗东西好吗?很高兴看到你们大部分都是
还在这里!
Tobias
/ * C ++:* tblPtr [i ] = 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 [i] =(int *)malloc(MAXCOLS * sizeof( int));
/ * C ++:tbl [i] = new(int)[MAXCOLS]; * /
#else
buildTbl(& tbl,MAXROWS,MAXCOLS);
#endif
for(size_t row = 0; r ow< MAXROWS;行++)
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]);
返回0;
}
ma**********@lycos.com 写道:下面的代码示例显示了2D数组的动态分配。我必须承认,我花了很长时间才到达那里(我已经有了另一个帖子),但我很高兴我终于得到了它的工作。现在问题就在于:
我能够正确地动态分配2D阵列,因为我正在正在进行。 (即没有调用任何功能)。当我尝试在另一个函数中执行它时,我得到了一个核心转储。任何帮助将不胜感激。
由于此函数需要更新指针指针类型,我实际上是将指针传递给指针指针类型为
函数。我在这里错过了什么?
你可以迷失在C的错综复杂中。
一个诡计绕过它:使用本地指向指针类型>
变量来执行常规操作并将其值分配给对象
your your指针到指针指针类型论点指向。
- >基本相同的代码,一个额外的行。
(你在* tblPtr上执行所有操作,这实际上意味着
你也可以使用int ** tbl并放* tblPtr = tbl在最后
如果一切顺利的话)
如果你遵循Tobias的建议,那就更容易了。
你我可以看到源代码在我执行二维数组初始化在线时正常工作。 (即不通过调用函数
调用)只需取消注释该行
/ * #define INLINE_INIT * /
Masood
[snip:代码]
Tobias Oed已经提到了我能看到的一切。
关注他关于malloc的建议的每一点( )。
BTW:谢谢你提供一个很好的例子!
干杯
Michael
-
电子邮件:我的是/ at / gmx / dot / de地址。
ma ********** @ lycos.com 写道:
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 ++)
条件应为i< numRows。
* tblPtr [i] =(int *)malloc(numCols * sizeof(int));
这将访问内存如果i大于0,则不属于tblPtr。
将其更改为以下内容:
(* tblPtr)[i] = malloc(numCols * sizeof(int)) ;
检查malloc()的返回值也是个好主意。
Christian
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[i] = (int *)malloc(numCols*sizeof(int));
/* C++: *tblPtr[i] = 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[i] = (int *)malloc(MAXCOLS*sizeof(int));
/* C++: tbl[i] = 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;
}
ma**********@lycos.com wrote:
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[i] = (int *)malloc(numCols*sizeof(int));
(*tblPtr)[i] = ...
Note that x = malloc(n * sizeof *x);
is preferred to x = (type *) malloc(n * sizeof(type);
You should check that malloc doesn''t return NULL.
You could change the return type of buildTbl to int ** and
return the array that way instead of through the parameter.
Do I remember that stuff Ok? Nice to see most of you are
still here!
Tobias
/* C++: *tblPtr[i] = 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[i] = (int *)malloc(MAXCOLS*sizeof(int));
/* C++: tbl[i] = 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;
}
ma**********@lycos.com wrote: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 get lost with the intricacies of C.
One "trick" to get around it: Use a local "pointer-to-pointer type"
variable to do as you always did and assign its value to the object
your "pointer-to-pointer-to-pointer type" argument points to.
-> Essentially the same code, one additional line.
(You perform all operations on *tblPtr which essentially means that
you could also work with a int **tbl and put *tblPtr=tbl at the end
if everything worked out)
If you follow Tobias''s advice, it becomes even easier.
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
[snip: Code]
Tobias Oed has already mentioned everything I can see.
Follow _every_ point of his advice regarding malloc().
BTW: Thank you for providing a good minimal example!
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
ma**********@lycos.com wrote:
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++)
The condition should be i < numRows.
*tblPtr[i] = (int *)malloc(numCols*sizeof(int));
This will access memory not belonging to tblPtr if i is greater than 0.
Change it to the following:
(*tblPtr)[i] = malloc(numCols*sizeof(int));
It''s also a good idea to check the return value of malloc().
Christian
这篇关于指向指针指针的指针问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!