C ++-指向数组的指针-指针数组 [英] C++ -- Pointers to Arrays -- Arrays of Pointers

查看:115
本文介绍了C ++-指向数组的指针-指针数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到这已经引起了很多人的困惑,但是在阅读了这里的几篇文章以及cplusplus教程后,我的大脑仍然很混乱.

假设我在头文件中有以下变量-

int numberOfLinePoints;
D3DXVECTOR3* line;   //confused as to what it is

然后在实现C ++文件中,将它们初始化如下-

//both initialized in constructor
numberOfLinePoints = 25;
line = new D3DXVECTOR3[numPoints];   //array of pointers?

我的行变量现在代表什么?

据我从读取stackoverflow上的链接可以看出,它应该表示一个指针数组.然后,我阅读了以下内容...

(1) 初学者的指针

...其中讨论了(A)指针数组和(B)指向数组的指针.这让我再次感到困惑,因为他们似乎都工作类似.

我在一个单独的位置定义我的指针的事实,这似乎是我感到困惑的地方. 我是否纠正为D3DXVECTOR3对象的指针数组?

要完成-如果可变行包含有关一个线段的信息,我将如何创建线段数组?我目前有以下内容-

//HEADER FILE
int numberOfLineSegments;
D3DXVECTOR3** lineCollection;   //array of pointers - each of which
                                //points to an array of pointers?
//CPP FILE
numberOfLineSegments = 8;                            //constructor
for(i = 0; i < numberOfLineSegments; i++)            //initialization
{                                                    //and allocation  CORRECT?
   lineCollection[i] = new D3DXVECTOR*[numPoints];   //of memory for     Y/N
}                                                    //lineCollection

VOID createLineSegments(startPoint, endPoint) //could return array instead
{
//pseudo to generate one line segment
while != numberOfLinePoints
line[sentinel++] = interpolate(startPoint, endPoint, time_T)

//pseudo to generate array of line segments
while != numberOfLines
lineCollection[sentinel++] = line
}

非常感谢您的帮助.

解决方案

int numberOfLinePoints;
D3DXVECTOR3* line;   //confused as to what it is
//both initialized in constructor
numberOfLinePoints = 25;
line = new D3DXVECTOR3[numPoints];   //array of pointers?

lineD3DXVECTOR3的数组.但是,如果D3DVECTOR3本身是指针,它将是一个指针数组.由于我不太了解C ++ D3D标头,因此不确定.

D3DXVECTOR3** lineCollection;

是一个指针数组,每个指针很可能是指向一行的指针(即,D3DXVECTOR3的数组).

您有两个选择.从内存的角度来看,最好的方法是将lineCollection中的每个条目设置为仅指向相应的行.如果您知道行不会更改(也不会被释放),或者如果行确实更改了,您希望更改立即反映在集合中,则这是安全的.

另一种选择是为lineCollection中的每个条目创建一个新数组,并将每个line中的点复制到该新数组中.

没有正确的答案,这取决于您想要的功能.

I notice this has caused confusion for several people, but after reading a couple of posts on here and the cplusplus tutorial my brain is still scrambled.

Suppose I have the following variables in a header file -

int numberOfLinePoints;
D3DXVECTOR3* line;   //confused as to what it is

Then in the implementation C++ file I initialize them as follows -

//both initialized in constructor
numberOfLinePoints = 25;
line = new D3DXVECTOR3[numPoints];   //array of pointers?

What does my line variable now represent?

As far as I can tell from reading links on stackoverflow it should represent an array of pointers. I then read the following however...

(1) Pointers for Beginners

...where (A) arrays of pointers, and (B) pointers to arrays, are both discussed. This left me confused once again as they both seem to work similarly.

The fact that I define my pointers in a seperate location to where I allocate (correct?) them seems to be where my confusion stems from. Am I correct that this is an array of pointers to D3DXVECTOR3 objects?

To finish - if variable line holds information about one line segment, how would I create an array of line segments? I currently have the following -

//HEADER FILE
int numberOfLineSegments;
D3DXVECTOR3** lineCollection;   //array of pointers - each of which
                                //points to an array of pointers?
//CPP FILE
numberOfLineSegments = 8;                            //constructor
for(i = 0; i < numberOfLineSegments; i++)            //initialization
{                                                    //and allocation  CORRECT?
   lineCollection[i] = new D3DXVECTOR*[numPoints];   //of memory for     Y/N
}                                                    //lineCollection

VOID createLineSegments(startPoint, endPoint) //could return array instead
{
//pseudo to generate one line segment
while != numberOfLinePoints
line[sentinel++] = interpolate(startPoint, endPoint, time_T)

//pseudo to generate array of line segments
while != numberOfLines
lineCollection[sentinel++] = line
}

Any help is much appreciated.

解决方案

int numberOfLinePoints;
D3DXVECTOR3* line;   //confused as to what it is
//both initialized in constructor
numberOfLinePoints = 25;
line = new D3DXVECTOR3[numPoints];   //array of pointers?

line is an array of D3DXVECTOR3. It would be an array of pointers if D3DVECTOR3 is itself a pointer, however. Since I don't know the C++ D3D headers very well, I'm not sure.

D3DXVECTOR3** lineCollection;

Is an array of pointers, each pointer likely being a pointer to a line (that is, an array of D3DXVECTOR3).

You have two options. Memorywise, the best would be to set each entry in lineCollection to just point to the corresponding line. This is safe if you either know the lines aren't going to change (and aren't going to be freed), or if they do change you want the changes to be reflected immedaitely inside your collection.

The other option would be to create a new array for each entry in lineCollection, and copy the points from each line into this new array.

There is no correct answer, it depends on the functionality you want.

这篇关于C ++-指向数组的指针-指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆